Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to create an array of strings but I keep getting an error.
Can you help me figure out what's wrong with this code?
int size;
scanf("%d",&size);
char** arr;
arr=(char**)malloc(sizeof(char*)*size);
You can simply use array of n number of pointers to char. Then use a loop to allocate space for those.
int n, size;
scanf("%d %d", &n, &size);
char *arr[n];
for( int i = 0; i < n; ++i ){
arr[i] = malloc( size * sizeof(char) );
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Somehow my program outputs the same size no matter how long the array gets, do you know what i did wrong?
char charArray[] = "STRING";
int size = sizeof(charArray) / 2 - 1;
printf("%d", size);
Output: 3
(i have to create a program which finds a string in another string thats why i am substracting 1 at the end to find the length of the word i want to find)
If you just want to get the length of your string, you could use strlen from the string library, of implement your own one:
size_t my_strlen(const char *str)
{
size_t i = 0;
while (str[i] != '\0')
i++;
return (i);
}
with this function, my_strlen("STRING") will return 6.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to fill an array with integers 0-100.
Here's my code:
int main()
{
int a[100];
for(int i = 0; i < a; i++)
{
a[i] = i;
printf("\n%d\n", a[i]);
}
}
I get this error:
comparison between pointer and integer (int and int) over the for line.
I can't figure it out. Any thoughts?
The cancel condition in your for loop is wrong. i<a You are comparing an int i variable with the pointer a that points to the memory location of the array. You would need to calculate the length of the array:
for(int i=0; i<sizeof(a)/sizeof(int); i++) {
But you could have found this solution in this 9 year old answer
Yes you are comparing pointer with an integer. This is why the error.
for(size_t i=0; i<sizeof(a)/sizeof(a[0]); i++) {
is what you wanted it to be and can be done in C language. Remember that sizeof operator results in value in size_t.
If you are not aware what that sizeof is doing - it is basically total number of bytes that the array object has - divided by each element's size - resulting in number of elements.
In case you are thinking from where did that pointer come?
Note one thing here a is converted into pointer (array decaying)to the first element - so it is nothing other than a pointer to the first element.
This should work for you,
int main()
{
int a[100];
int n = sizeof(a) / sizeof(a[0]); //Get the size of the array
for(int i=0; i<n; i++) { // loop through each elements of the array
a[i] = i;
printf("\n%d\n", a[i]);
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I encountered a weird bug in this code:
int *a = (int*) malloc(N*sizeof(int)); // allocate array
int i;
for (i=2; i<=N; i++)
{
a[i] = i;
}
int *b = (int*) malloc(N*sizeof(int));
memcpy(b, a, N*sizeof(b));
If I were to print out array a, output = 2,3,...,19 0
Whereas the expected output should have been 2,3,...,19,20.
Copying the array onto b strangely affected the last element.
An array of N elements has valid indexes 0, 1, ..., N - 1. Your final loop round accesses a[N], which is out of bounds and has undefined behaviour.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Ive got a problem with this code, im trying to add up array1 with array2.
I enter the numbers for array2 by Command line parameters.
When i enter 10 numbers it is working but when i add less than 10 I get an Memory access error.
My question is now: how do i fill up the missing array fields with the number 0? For example : I enter 9 numbers and the 10th field should be 0.
You are not checking how many command line arguments are passed, and when you index into the command line argument array, you will get an out-of-bounds error.
In you addiren function, you should take advantage of the argc that is passed and used that in your for loop limit.
#include <stdio.h>
#include <stdlib.h>
int addiren(int argc, char**argv){
int array_one[10] = {0,1,1,2,3,5,8,13,21,35};
int array_two[10] = {0}; //Quick way to set the array to all zeros
int array_three[10] = {0};
//Set array_two with your cmd-line args, notice the use of argc
for(int i = 1; i<argc && i<=10; i++){
array_two[i-1] = atoi(argv[i]);
}
//Add array_one with array_two to array_three
for(int i = 0; i<10; i++){
array_three[i] = array_one[i]+array_two[i];
}
//Return an int since that's what the function return type requires
return 0;
}
Hope this helps!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
Why am I getting Segmentation fault? Could you give me some understadable explanation? Thanks in advance.
#include <stdio.h>
int main()
{
int i,j;
char* ips[1000];
char ip[15] = "192.34.132.52";
char port[4] = "4003";
for (i = 0; i < 10; i++) {
sprintf(ips[i], "%s:%d", ip, port);
}
for (j = 0; j < 10; j++) {
printf("[%d] = %s\n", j, ips[j]);
}
return 0;
}
You didn't allocate the memory for ips[i], sprintf doesn't do it for you. Add a line in the first for loop, before the sprintf:
ips[i] = malloc(sizeof(ip)+sizeof(port)+2);
EDIT: as huseyin tugrul buyukisik noted, port isn't big enough to hold 4 characters and a null terminator. And you should use the %s modifier for it as port is a string as well.