Segmentation Fault - declare and init array in C

后端 未结 3 1532
渐次进展
渐次进展 2021-01-28 10:12

I am very new to C. coming from Python, Java and C# worlds. This might be a stupid question but I am getting segmentation fault:

// struct for storing matrices
t         


        
3条回答
  •  时光说笑
    2021-01-28 10:16

    You need to allocate memory for A.elts to point to. You can do this with malloc. What you are doing is coping the constant array you specified into whatever address elts happens to point to (it is uninitialized).

    You can also point A.elts to the constant array like so:

    float *myFloats = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
    A.elts = myFloats;
    

提交回复
热议问题