动态字符串输入 - 使用scanf函数(QUOT;%为") [英] Dynamic String Input - using scanf("%as")

查看:150
本文介绍了动态字符串输入 - 使用scanf函数(QUOT;%为")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用读取输入 scanf函数并存储到的char * 动态,由GCC手动指定的,但它给一个编译时错误

 的char *字符串;
  如果(scanf的(%作为,&放大器;!串)= 1){
    //一些code
  }
  其他{
   的printf(%S \\ n,*字符串);
   免费(字符串);
   //一些code
  }


解决方案

A 修改器的 scanf函数的将无法正常工作,如果编译与 -std = C99 标志;确保你没有使用。

如果您有至少版本的glibc 2.7,您可以并且应该使用 M 改性剂代替 A

此外,它是你的责任,以释放缓冲区。

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.

这篇关于动态字符串输入 - 使用scanf函数(QUOT;%为")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆