scanf正在使用未初始化的变量;C [英] scanf is using an uninitialized variable; C

查看:65
本文介绍了scanf正在使用未初始化的变量;C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这里只是一个愚蠢的错误,但是我无法弄清楚.这是我的代码的一部分:

I'm sure there just a silly mistake here, however, I can't figure it out. This is part of my code:

char *moving;
scanf("%s", moving);

当我用gcc编译时,它表示以下内容:

When I compile it with gcc, it says the following:

newmatrix.c:38:7: warning: ‘moving’ is used uninitialized in this function [-Wuninitialized]

第38行是scanf

Line 38 is the scanf

我该如何解决?谢谢

推荐答案

在使用前为移动分配内存.使用 malloc().

Allocate memory for moving before using it. Use malloc().

移动 char 类型的指针.在将字符串存储在 moving 中之前,您需要为其分配内存.

moving is pointer of char type. Before storing the string in moving, you need to allocate memory for it.

char *moving;
moving = malloc(100); 
scanf("%s", moving);

OR

只需将 char * moving 更改为 char moving [256] .

也可以使用 fgets()代替 scanf().

这篇关于scanf正在使用未初始化的变量;C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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