delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/07/04/15:15:13.1

From: eglebbk AT phys DOT uva DOT nl (Evert Glebbeek)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Newbie question about strings in C ..
Date: Tue, 04 Jul 2000 19:02:19 GMT
Organization: Physics student, University of Amsterdam
Lines: 57
Distribution: world
Message-ID: <396230e0.2867154@news.wins.uva.nl>
References: <396119a7 DOT 31315113 AT news>
NNTP-Posting-Host: stol-117-149.uva.studentennet.nl
X-Trace: info.wins.uva.nl 962737274 10341 145.98.117.149 (4 Jul 2000 19:01:14 GMT)
X-Complaints-To: usenet AT science DOT uva DOT nl
NNTP-Posting-Date: Tue, 4 Jul 2000 19:01:14 +0000 (UTC)
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Newsgroup: comp.os.msdos.djgpp
 From: david DOT ah AT nospamhome DOT com (David Hamilton)
 On Mon, 03 Jul 2000 21:17:41 GMT

>I know that you can create an array like:
>char name[40];
>or a pointer like:
>char *name;
Careful there! name[40] creates an array of 40 chars, with name being
a pointer to it's first arguement. char *name tells the compiler that
you want to use a pointer to a (array of) char somewhere, but it
doesn't initialize the pointer to anything, so it points to an
undefined bit of memory.
>Is the following code ok?
No. :-)
>main()
>{
>char *name;
See the explanation above: you create the pointer, but points to an
undefined bit of memory.
>scanf(name);
AFAIK, this is incorrect. I'm also not sure what it's supposed to do.
>scanf("%s",name);
Provided name points to an array of char (a string), this should be
correct, however, I usually find it more convenient to use gets(name).
>My understanding of C says that this is ok since name points to the
>first character of the string and the string is terminated by \0 this
>should allocate just enough space for whatever is entered. 
No, it shouldn't. You have to allocate the string before using it.
>However, I have found this code to be..less than stable.
I can imagine...

> I know that you can use malloc() to allocate a certain number of
>bytes for use by a variable but I have no way to do this at time of
>execution. That is to say, varying the amount of space allocated based
>on what is needed.
You can't allocate a string on the fly using a standard library
function. Also, if you use scanf() or gets(), you can't specify how
large the string you've allocated is, so there's no way to keep your
user from entering characters until he overflows your string. I
usually deal with this by simply setting the string size redicilously
large so it will never be overwritten in normal useage (eg, a 256 byte
string when reading a 2 digit number).
Alternatively, the following might work (I haven't tested this!):

char the_string[LENGTH_OF_THE_STRING];
...
fread (the_string,1,LENGTH_OF_THE_STRING,stdin);

>If someone could example/show me the standard way of dealing with
>basic text input I would be very greatful.
Well, like I said earlier, I'm not sure there is a standard way, but
anyway, this is the way I do it.

Regards,

Evert Glebbeek

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019