c - 警告 : X may be used uninitialized in this function

标签 c warnings

我正在编写自定义“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/

相关文章:

coding-style - 除了 "treat warnings as errors"和修复内存泄漏,作为编码标准的一部分,我们还应该实现哪些其他想法?

c - 单个主机的阻塞与非阻塞 UDP 套接字

java - 我如何设置我的环境来构建我自己的 ril 库?

odoo - 如何在 onchange 方法内结合警告消息和更新字段值?在奥多 9

Java 泛型和 SuppressWarnings

php - PHP默认如何确定POST方法超过1000个变量?

计算二叉树中的特定节点,其中节点是 c 中的字符

C:尝试从文件中读取浮点字符

c - 尝试在结构 : why do I get an error, 中使用 typedef,这首先是个好主意吗?

c++ - 警告 :"pointless comparison of unsigned integer with zero"cuda