Mail Archives: djgpp/2000/07/03/17:30:29
From: | david DOT ah AT nospamhome DOT com (David Hamilton)
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Newbie question about strings in C ..
|
Message-ID: | <396119a7.31315113@news>
|
X-Newsreader: | Forte Free Agent 1.21/32.243
|
Lines: | 30
|
Date: | Mon, 03 Jul 2000 21:17:41 GMT
|
NNTP-Posting-Host: | 24.17.217.8
|
X-Complaints-To: | abuse AT home DOT net
|
X-Trace: | news1.rdc1.tn.home.com 962659061 24.17.217.8 (Mon, 03 Jul 2000 14:17:41 PDT)
|
NNTP-Posting-Date: | Mon, 03 Jul 2000 14:17:41 PDT
|
Organization: | @Home Network
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
Hello,
I am trying to figure out what the standard way for dealing with
string input is. I know that you can create an array like:
char name[40];
or a pointer like:
char *name;
Is the following code ok?
main()
{
char *name;
scanf(name);
scanf("%s",name);
printf("%s,"name);
return 0;
}
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.
However, I have found this code to be..less than stable.
What part am I missing?
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.
If someone could example/show me the standard way of dealing with
basic text input I would be very greatful.
Thanks in advance,
David H.
- Raw text -