Supposed to take in lines from stdin and print them in reverse order, but keep hitting a segfault in this loop. Any help would be appreciated!
...
char* poem[MAX_STRINGS][MAX_CHARS];
char* line[MAX_CHARS]
int lineCount = 0;
while(!feof(stdin)) {
fgets(line[0], MAX_CHARS, stdin);
strcpy(poem[lineCount][0], line[0]);
lineCount++;
}
Why won't this c code work right?
You've declared line as an array of pointers. From the looks of it, you want line to be just an array of characters. Same goes with your declaration of poem; you've declared it a two dimensional array of character pointer.
If you really do want line to be an array of pointers, then you need to malloc the space first. If you want to use line as an input buffer, then first declare it as
char line[MAX_CHARS]
AND
pass line, not line[0], to both fgets and strcpy.
Same goes for your declaration of poem. Pass poem[lineCount] and it should work if the declaration has is changed.
Reply:GOOGLE IT
Reply:Per my husband the programmer, also remove the two other [0] phrases in the strcpy line.
Reply:fgets(line[0], MAX_CHARS, stdin);
to
fgets(line, MAX_CHARS, stdin);
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment