我正在编写自定义“vector ”结构。我不明白为什么我会收到 Warning: "one"may be used uninitialized
here。
这是我的vector.h文件
#ifndef VECTOR_H
#define VECTOR_H
typedef struct Vector{
int a;
int b;
int c;
}Vector;
#endif /* VECTOR_ */
警告发生在 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
尚未分配,因此指向不可预测的位置。您应该将它放在堆栈上:
Vector one;
one.a = 12;
one.b = 13;
one.c = -11
或者为它动态分配内存:
Vector* one = malloc(sizeof(*one))
one->a = 12;
one->b = 13;
one->c = -11
free(one);
注意在这种情况下使用free
。通常,每次调用 malloc
时,您只需要调用一次 free
。
关于c - 警告 : X may be used uninitialized in this function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12958931/