strings

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
 
G

Gordon Burditt

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.

Read that as: BEFORE fgets gets called, s had darn well better point
to where the string should be read in.

DO NOT call functions with uninitialized pointer variables as parameters.
but does that mean fgets allocated memory
itself for the string?

No. The above call to fgets() does NOT change the value (in this
case, uninitialized) of s.
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?

If you are lucky, in Osama bin Laden's groin. If you are unlucky,
in YOUR groin. OUCH!
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).

A waste of space is an improvement over "smegmentation fault --
core dumped" or the Blue Screen of Death. If you pass pointers,
it is your responsibility to make sure that they actually point
somewhere (and to a sufficiently large buffer). One approach is
to call malloc() to allocate storage. Another is to point it at a
character array declared locally.
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).

Gordon L. Burditt
 
D

Default User

slurper said:
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.

No it won't. It points at some unknown memory location, possibly
invalid, possibly a trap. The program tried to write to that location,
causing undefined behavior.
but does that mean fgets allocated
memory itself for the string?
No.

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?

If you are unlucky, it writes it to some area of memory currently
unused. If you are lucky, you get a system error.
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

Writing to a pointer (or accessing it at all) is undefined behavior.

What is it you want to do?




Brian
 
S

slurper

tx man, that was a clear and concise explanation
when you come from other languages, you take too much for granted when
starting to program in c.

Gordon Burditt said:
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.

Read that as: BEFORE fgets gets called, s had darn well better point
to where the string should be read in.

DO NOT call functions with uninitialized pointer variables as parameters.
but does that mean fgets allocated memory
itself for the string?

No. The above call to fgets() does NOT change the value (in this
case, uninitialized) of s.
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?

If you are lucky, in Osama bin Laden's groin. If you are unlucky,
in YOUR groin. OUCH!
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).

A waste of space is an improvement over "smegmentation fault --
core dumped" or the Blue Screen of Death. If you pass pointers,
it is your responsibility to make sure that they actually point
somewhere (and to a sufficiently large buffer). One approach is
to call malloc() to allocate storage. Another is to point it at a
character array declared locally.
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).

Gordon L. Burditt
 
K

Kenneth Brody

slurper said:
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?
[...]

You've got the meaning of the buffer pointer wrong. It does _not_ "set
the pointer to the string that was read". Rather, it puts the string
where the pointer says to put it.

Since s is uninitialized, as you noted, fgets will (attempt to) put the
string at whatever address happens to be in s. If you're lucky, you will
crash the program immediately, as it tries to write to memory that doesn't
belong to the program. If you're unlucky, s will happen to point to valid
memory, and who-knows-what will be overwritten, perhaps to raise its ugly
head only long after the fgets returns. If you're really unlucky, you may
overwrite critical parts of the O/S and wipe out your hard drive.

Or, maybe all that will happen is that demons will fly out your nose.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top