Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic String Input - using scanf("%as")

Tags:

c

scanf

I am trying to read input using scanf and storing into char * dynamically as specified by GCC manual, But it is giving a compile time error.

  char *string;
  if (scanf ("%as",&string) != 1){
    //some code
  }
  else{
   printf("%s\n", *string);
   free(string);
   //some code
  }
like image 672
N 1.1 Avatar asked Feb 24 '10 21:02

N 1.1


2 Answers

The a modifier to scanf won't work if you are compiling with the -std=c99 flag; make sure you aren't using that.

If you have at least version 2.7 of glibc, you can and should use the m modifier in place of a.

Also, it is your responsibility to free the buffer.

like image 74
Nietzche-jou Avatar answered Oct 18 '22 18:10

Nietzche-jou


Do you have GNU extensions enabled? Standard C doesn't have a modifier at all.

like image 43
Tronic Avatar answered Oct 18 '22 19:10

Tronic