c - 结构体指针 结构体指针

标签 c

我无法让它工作! 抛出段错误。

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    size_t NbOfElmts; 

    float  * Weights; 
    int    * Areas;

} ListOfCoeff;

typedef struct
{
    size_t NbOfElmts;  

    ListOfCoeff * Elmts;


} SetOfListsOfCoeff;



int main( int argc, const char* argv[] ){

   SetOfListsOfCoeff * theList;

   int N = 10;
   float *A = malloc( N * sizeof(*A) );

   for ( int i = 0; i < N; i++) 
        A[ i ] = i;


   for ( int i = 0; i < N; i++ )
       ( (theList[ i ].Elmts)->Weights)[ i ] = A[ i ];


   for ( int i = 0; i < N; i++) 
        printf("\n List = %f", ( (theList[ i ].Elmts)->Weights)[ i ] );

return 0;


}

该行的问题:

( (theList[ i ].Elmts)->Weights)[ i ] = A[ i ];

最佳答案

您尚未为 theList 分配任何内存。

关于c - 结构体指针 结构体指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27298352/

相关文章:

c - malloc() : smallbin double linked list corrupted

c - 检测 macOS 上的用户事件

c - 为什么这个函数会出现段错误?

c - 如何在 C/C++ 中对具有多个相似值的枚举进行 switch()?

整个程序中__TIME__的常量值

c - -funsafe-math-optimizations 中包含哪些优化?

c - 为什么结构的前向声明不起作用?

c - 如何在 C 中正确转义字符串中的字符以获得符合 JSON 的格式?

c - 警告 : Implicit declaration error

c++ - C++ 中 Double 类型值的减法