简体   繁体   中英

Dynamic String Input - using scanf(“%as”)

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
  }

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.

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

I've had limited experience with GCC, but I've never seen a %a modifier for scanf . Have you tried replacing the %a with %s in the third line you provided?

'Dynamic String Input' with scanf("%as") will work if the -ansi or -std=c89 flag is enabled.
Compile using gcc -ansi

Or else you can use scanf("%ms")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM