malloc for struct and pointer in C

前端 未结 8 2003
忘了有多久
忘了有多久 2020-12-04 04:57

Suppose I want to define a structure representing length of the vector and its values as:

struct Vector{
    double* x;
    int n;
};

Now,

8条回答
  •  难免孤独
    2020-12-04 05:31

    The first time around, you allocate memory for Vector, which means the variables x,n.

    However x doesn't yet point to anything useful.

    So that is why second allocation is needed as well.

提交回复
热议问题