I am writing a custom “vector” struct. 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 happens 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;
}
onehas not been assigned so points to an unpredictable location. You should either place it on the stack:or dynamically allocate memory for it:
Note the use of
freein this case. In general, you’ll need exactly one call tofreefor each call made tomalloc.