在C中动态分配Struct数组

时间:2016-07-09 03:43:35

标签: c data-structures struct

我搜索了很多主题。 但它们都没有真正符合我想要的答案。

我想动态分配一个struct类型的数组。

但是,我失败并得到了错误 “错误3错误C2109:下标在编译期间需要数组或指针类型”

这是我的代码:

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

typedef struct Term{
    int row;
    int column;
    int value;
};

int main(void){
    int size;
    printf("Enter how many non-zero value you want to transpose:");
    scanf_s("%d",&size);
    struct Term *termA = (struct Term *)malloc((sizeof(struct Term))*(size+1));
    termA[0].value = size;      
}

在获得一些帮助后,提供了正确的代码,如下所示:

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

typedef struct Term{
    int row;
    int column;
    int value;
};

int main(void){
    int size;
    struct Term *termA;
    printf("Enter how many non-zero value you want to transpose:");
    scanf_s("%d",&size);
    termA = (struct Term *)malloc((sizeof(struct Term))*(size+1));
    termA[0].value = size;      
}

0 个答案:

没有答案
相关问题