This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: scanf, linux and solaris



> Hi, I've been trying to scan in a string using scanf. The following code works fine on Solaris but breaks on Linux(tested on two different boxes) with a seg fault. Currently it breaks when the username is longer than 10 chars, on a previous attempt before rebooting it would break when the username was longer than 6 chars. 
> Any help would be appreciated.
> 
> #include <stdio.h>
> 
> main(){
> char* username;
> printf("Username:");
> scanf("%s", &username);
> }

I think you will have to allocate memory with malloc() before reading
anything into the memory pointed to by username. 

regards

Torleif Sandnes


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]