Assigning to 'char' from incompatible type 'char[1024]'

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.
can you post the definition of array "everything" ?
1
2
#define maxSize 999
char everything[maxSize];
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.

1
2
3
4
5
6
7
8
printf("Writing file.\n");
        FILE * fw = fopen(fn, "w");
        for(int i = 0; i < 63; i++) {
            fputs(&everything[i], fw);
        }
        fclose(fw);
        printf("Done.\n");
        goto mainMenu;


Thank you again for the suggestion.
And Rechard3, I'm trying to store a copy when I run the program multiple times.
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.
is that error in line 4??
Not, it's in Thread 1 (I'm using Xcode).
ok, i suspect that the file is not opened by your program.
to make sure, plz replace line 4 with this:
1
2
3
4
5
FILE * fw;
if ((fw = fopen("fn","w"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}

compile your program and give it a go, if you see the message:
"Cannot open file."

then that's the problem.

if the problem is another one, then i'm afraid i'm unable to help.
Is that when I define the file, or the original code?
Last edited on
Just remove this line:
FILE * fw = fopen(fn, "w");

and place the 5 lines i mentioned in his place.
No, that didn't work.

Still, thank you for helping.
If what you want is to copy the whole content of buf tu the everything then you need to use the strcpy function like here http://www.cplusplus.com/reference/cstring/strcpy/?kw=strcpy
No, buf gets one char at a time.
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.
Topic archived. No new replies allowed.