S
slurper
i still don't get strings in c entirely
suppose
FILE *stream; int n;
char *s;
(suppose file is open...)
fgets (s, 1024, stream)
what i don't understand is: i defined a pointer to a char (only the pointer, no memory allocated, i think). when fgets gets called, s will point to the string read. but does that mean fgets allocated memory itself for the string? s is only a pointer to a char, but there is no memory allocated yet by me for all of the string. where does the string end up?
all examples i found were like this
FILE *stream; int n;
char s[1024];
(suppose file is open...)
fgets (s, 1024, stream)
but if the lines are all much smaller, 1024 characters might be a waste of space (allthough maybe it's not significant, but i want to know what happens in the "char *s"-case). char s[1024] means s is a pointer to a block of memory of 1024 bytes and the s-pointer to this block cannot be changed (at least, it's how i see it).
if i understand this, i think a lot will become clearer
tx
suppose
FILE *stream; int n;
char *s;
(suppose file is open...)
fgets (s, 1024, stream)
what i don't understand is: i defined a pointer to a char (only the pointer, no memory allocated, i think). when fgets gets called, s will point to the string read. but does that mean fgets allocated memory itself for the string? s is only a pointer to a char, but there is no memory allocated yet by me for all of the string. where does the string end up?
all examples i found were like this
FILE *stream; int n;
char s[1024];
(suppose file is open...)
fgets (s, 1024, stream)
but if the lines are all much smaller, 1024 characters might be a waste of space (allthough maybe it's not significant, but i want to know what happens in the "char *s"-case). char s[1024] means s is a pointer to a block of memory of 1024 bytes and the s-pointer to this block cannot be changed (at least, it's how i see it).
if i understand this, i think a lot will become clearer
tx