scanf函数动态分配 [英] scanf dynamic allocation

查看:119
本文介绍了scanf函数动态分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论出于何种原因如下code打印(空):

For whatever reason the following code prints (null):

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char *foo; 
    scanf("%ms", &foo);
    printf("%s", foo);
    free(foo);
}

我试图动态字符串分配内存,但正如我刚才所说我的程序只是输出(空)。我摸索,通过getche和realloc制作功能,但它似乎毫无意义差不多,由于这样的事实,我也不得不计划,如果用户输入退格,标签等会发生什么。但正如我所说,这只是一个工作周围,​​我宁愿知道为什么上面的code是不工作...

I'm trying to allocate memory for a string dynamically but as I said earlier my program simply outputs (null). I worked around this by making a function using getche and realloc but it seems almost pointless due to the fact that I also had to program what would happen if the user entered backspace, tab, etc.. But as I said that is just a work around and I would rather know why the above code is not working...

其他信息:

我现在用的是PellesÇIDE v7.00,并与C11标准编译

I am using the Pelles C IDE v7.00 and compiling with the C11 standard

推荐答案

我没有看到%M 中的Draft C11标准(关于的fscanf 部分)。我建议你​​避免它,并调用的malloc()就像在C99。

I am not seeing %m in section 7.21.6.2 of the Draft C11 standard (the section on fscanf). I suggest that you avoid it and call malloc() as you would in C99.

这篇关于scanf函数动态分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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