Re: a newbie question.



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
.



Relevant Pages

  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... And now a question about something else: why do you use floating ... use then to copy a float into a char *1. ... binary representation doesn't resemble a string like "123.46343" ...
    (comp.lang.c)
  • Re: can anyone help me in correcting this code?
    ... char decodeLower ... float percent(int m, int n) ... string is just an array of characters terminated by '\0'. ...
    (comp.lang.c)
  • Re: Unwanted rounding
    ... I recommend double rather than float. ... char cf; ... and then search the string for the ...
    (comp.lang.c)
  • Re: converting float to string
    ... > Can anyone tell me how to convert a float to string for use with a ... > TextOut() function..Thanks.... ... char s; ...
    (microsoft.public.vc.language)