分配给类型时不兼容的类型

时间:2013-03-06 18:10:19

标签: c

我正在尝试创建一个表,并且表的条目是结构类型。 我从类型'TableRow'分配类型'SortTableRows'时收到错误“不兼容的类型”。

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

typedef struct TableRow
{
    int  startingValue;
    int entries[100];
}TableRow;

typedef TableRow SortTableRows[20]; //Table Containing entries of type TableRow

SortTableRows* SortTableRowsPtr;

int main()
{

    TableRow *tableRow;
    tableRow = malloc(sizeof(TableRow));
    tableRow = NULL;
    SortTableRowsPtr[2]=*tableRow; //Error
    return 0;
}

2 个答案:

答案 0 :(得分:3)

我认为您的意思是使用(*SortTableRowsPtr)[2],即分配到TableRow数组中SortTableRows类型的第三个数组条目。

或者SortTableRowsPtr应该是一个包含20个TableRow指针的数组?

typedef TableRow (*SortTableRows)[20];

答案 1 :(得分:0)

以下怎么样?您将tableRow分配给SortTableRows数组中的第3行。

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

typedef struct TableRow
{
    int  startingValue;
    int entries[100];
}TableRow_t;

int main()
{

    TableRow_t SortTableRows[20];
    TableRow_t *tableRow = NULL;

    /* Put some data into the 3rd row */
    SortTableRows[2].startingValue = 2;
    for (i = 0; i < 100; ++i) {
            SortTableRows[2].entries[i] = i;
    }

    tableRow = &(SortTableRows[2]);

    printf("%d: %d, %d, %d\n", 
                    tableRow->startingValue,
                    tableRow->entries[0],
                    tableRow->entries[1],
                    tableRow->entries[2]);

    return 0;
}