Re: a newbie question.
- From: Falcon Kirtaran <falconnews@xxxxxxxxxxxxxxxx>
- Date: Wed, 12 Mar 2008 03:47:35 -0600
Mike wrote:
Hi
#include<stdio.h>
main()
{
char s[]="Taiwan University.";
float a;
scanf("%f",&a);
printf("%f",a);
printf("input string:");
gets(s);
puts("%s",s);
}
After compiled by Visual Studio, the above program can input a.
But after it shows a, then I cannot enter s string.
Why?
Mike
Just to add to the other posts, your string literal "Taiwan University." is of type const char *. You should not assign anything to it, because it might be stored in a place in memory to which your program cannot write, at which point it will crash (this idea can be summarized easily: never cast away the constness of string literals). Have a look at malloc() and free() instead, or just declare an array of char, eg. char s[81].
If you're feeling a bit overwhelmed with all these replies, being a newbie, note that strings are much more complicated than other variables in C.
--
--Falcon Kirtaran
.
- Follow-Ups:
- Re: a newbie question.
- From: Richard Bos
- Re: a newbie question.
- From: Ben Bacarisse
- Re: a newbie question.
- References:
- a newbie question.
- From: Mike
- a newbie question.
- Prev by Date: Haurex Challenger Black/Blue Chronograph GMT Mens Watch OA305UNN Replica
- Next by Date: Using env variables in tcl script
- Previous by thread: Re: a newbie question.
- Next by thread: Re: a newbie question.
- Index(es):
Relevant Pages
|