So I'm trying to be able to store an array of characters, and it's giving me the error message above. Is the compiler being stupid or am I (I'm guessing latter of the two)?
1 2 3 4 5 6 7 8 9 10 11 12
printf("reading file\n");
char buf[maxString];
FILE * fr = fopen(fn, "r");
char * rc;
int i=0;
for (int i = 0; i < 63; i++) {
rc = fgets(buf, maxString, fr);
everything[i] = buf; //This is the line with the error.
}
fclose(fr);
printf("\nDone.\n");
goto mainMenu;
And I know I'm using goto, and I know it's a bad idea to use it, but I like using it.
my guess is that you define everything as
char everything[SomeBigNumber];
Then everything[i] is just one char. And you try to squeeze an array of char with 1024 elements into it. Try defining everything as an array of arrays of chars
you got this a bit wrong, see:
the name of an array is actually a "pointer" to the first element in the array.
so when you write this:
everything[i] = buf;
it's like saying:
*(everything+i) = buf;
now you see, different types.
buf is a pointer, and everything[i] is a character value.
the compiler can't convert from (char*) to (char).
i don't know your goal in this program, but if you want to copy a string to another, use strcpy() function.
read more about pointers and arrays.
hope that was useful.
That fixed the bug, thanks. Now, I'm finding I have another bug. I'm having the error: Thread 1: EXC_BAD_ACCESS (code=1, addres=0x0). It might be a problem with defining it. I've included that code as well.
and for the recommendations in writing programs:
they're really just recommendations, they're not a "must follow under all circumstances", you can always choose to ignore them, but i -myself- follow them 'cause they make programming a lot easier.
that is what you want, but that is not what you do.
your have char buf[maxString] and char everything[maxSize];
think about it
you are trying to assign to one of the spaces of everything the whole content of buf
if you want to add only 1 char at a time then you have to do it with a loop or something so that each letter from buf is passed to everything. Though i still think that it would be better to use strcpy.
Also fgets copyes a string from the fr, not one char at a time but maxString at a time. Hope you understand what im trying to say.