C:警告:X可以在这个函数中使用未初始化 [英] C: warning: X may be used uninitialized in this function

查看:263
本文介绍了C:警告:X可以在这个函数中使用未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要澄清,这个问题是关于一门功课。但它不涉及到运动本身,所以我希望我不会遇到任何阻力在这里回答我的问题。基本上,运动是有关创建自己的结构载体,并使用它,而不是多数民众赞成的问题。因为我很新,我不明白为什么我得到一个警告:一可用于初始化此处

这是我的vector.h文件

 的#ifndef VECTOR_H
#定义VECTOR_Htypedef结构向量{
    int类型的;
    INT B:
    INT℃;
}向量;#ENDIF / * * VECTOR_ /

该警告这里就行单>一种= 12

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&MATH.H GT;
#包括vector.h诠释主要(无效){
    矢量*之一;
    单>一种= 12;
    单> B = 13;
    单> C = -11;
}


解决方案

有一个尚未分配那么点至未predictable位置。您应该将其放置在栈上:

 载体之一;
one.a = 12;
one.b = 13;
one.c = -11

或它的动态分配内存:

 矢量*一=的malloc(sizeof的(*的))
单>一种= 12;
单> B = 13;
单> C = -11
免费(之一);

请注意在这种情况下使用免费的。一般情况下,你需要只有一个调用免费的malloc

的每次调用

To clarify, this question is about a homework. But it is not related to the exercise itself, so I hope I do not encounter any resistance here to answer my question. Basically, the exercise is about creating a own struct "vector" and use it, but thats not the problem. Since I'm very new, I do not understand why I'm getting a "Warning: "one" may be used uninitialized" here.

This is my vector.h file

#ifndef VECTOR_H
#define VECTOR_H

typedef struct Vector{
    int a;
    int b;
    int c;
}Vector;

#endif /* VECTOR_ */

the warning comes here on line one->a = 12

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "vector.h"

int main(void){
    Vector* one;
    one->a = 12;
    one->b = 13;
    one->c = -11;
}

解决方案

one has not been assigned so points to an unpredictable location. You should either place it on the stack:

Vector one;
one.a = 12;
one.b = 13;
one.c = -11

or dynamically allocate memory for it:

Vector* one = malloc(sizeof(*one))
one->a = 12;
one->b = 13;
one->c = -11
free(one);

Note the use of free in this case. In general, you'll need exactly one call to free for each call made to malloc.

这篇关于C:警告:X可以在这个函数中使用未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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