C:结构指针的动态数组上的不兼容类型

时间:2013-05-10 14:09:37

标签: c pointers malloc dynamic-arrays

我需要帮助来声明一个动态的指针数组。 阅读一些文章和类似的问题,我尝试为我的项目做,但仍然冻结。

我正在尝试做什么:

typedef struct recArmario Armario, *pArmario;

struct recArmario {
    int ID;
    pArmario next;
    pArmario prev;
    pCorredor parent;
};

pArmario auxArmarioTest1 = malloc(sizeof(Armario));
auxArmarioTest1->ID = 1;
pArmario auxArmarioTest2 = malloc(sizeof(Armario));
auxArmarioTest2->ID = 2;

(…)

//Dynamic array of pointer Armario:
pArmario arrArmariosPointers = malloc(sizeof(pArmario) * maxCorredores);
//Here is my doubt
*(arrArmariosPointers + idCorredor) = auxArmarioTest1;

Xcode说:

Assigning to 'struct recArmario' from incompatible type 'pArmario' (aka 'struct recArmario *'); dereference with *

我不明白,“分配给'struct recArmario'”?! 我宣布一个pArmario数组,我想是的。

*(arrArmariosPointers + idCorredor)相当于arrArmariosPointers[idCorredor],对吧?

更新1

感谢您的回答! 我更新了我的样本,但仍有问题。

pArmario auxArmarioTest1 = malloc(sizeof(Armario));
auxArmarioTest1->ID = 1;
pArmario auxArmarioTest2 = malloc(sizeof(Armario));
auxArmarioTest2->ID = 2;

//Dynamic array of pointer Armario:
pArmario arrArmariosPointers = malloc(sizeof(pArmario) * 5);
//Test
pArmario auxA;
auxA = arrArmariosPointers + 0; //I know, it's useless :)
auxA = auxArmarioTest1;
auxA = arrArmariosPointers + 1;
auxA = auxArmarioTest2;

printf("\nA%d",(arrArmariosPointers+0)->ID);
printf("\nA%d",(arrArmariosPointers+1)->ID);

free(auxArmarioTest1);
free(auxArmarioTest2);

我得到的结果是:

A0
A0

我又做错了什么?

更新2

忘记我的更新1,这是愚蠢的。我只是在改变auxA

pArmario* arrArmariosPointers = malloc(sizeof(pArmario) * 5);

arrArmariosPointers[0] = auxArmarioTest1;
arrArmariosPointers[1] = auxArmarioTest2;

printf("\nA%d",arrArmariosPointers[0]->ID);
printf("\nA%d",arrArmariosPointers[1]->ID);

谢谢大家。

3 个答案:

答案 0 :(得分:3)

*(arrArmariosPointers + idCorredor) = auxArmarioTest1;

此处arrArmariosPointers + idCorredorpArmariostruct recArmario *,取消引用它会为您提供struct recArmario,然后您将其分配给auxArmarioTest1 struct recArmario * }。简而言之,您正在指定一个结构的指针,因此错误

对于您的更新1,这可能是您想要实现的目标吗?

pArmario * arrArmariosPointers = malloc(sizeof(pArmario) * 5);
arrArmariosPointers[0] = auxArmarioTest1;
arrArmariosPointers[1] = auxArmarioTest2;

printf("\nA%d",(arrArmariosPointers[0])->ID);
printf("\nA%d",(arrArmariosPointers[1])->ID);

答案 1 :(得分:1)

动态数组变量的类型是错误的。你错过了一个指针:

pArmario* arrArmariosPointers = malloc(sizeof(pArmario) * maxCorredores);

答案 2 :(得分:1)

此:

pArmario auxArmarioTest1 

是指向struct recArmario的指针,这个:

pArmario arrArmariosPointers

是完全相同的事情。因此,如果您取消引用指向struct的指针,则表示您有struct

因此这段代码是:

*(arrArmariosPointers + idCorredor) = auxArmarioTest1;
^      ^                   ^                   ^
|      |                   |                   |
| array of pointers  +   some offset           |
|   to structs          into the array         |
|                                              |
dereference the result, so now we can          |
assign a struct                                |
                                        but here's a problem
                                       we're trying to give it a pointer
                                       to a struct.

所以问题的答案是肯定的:
*(arrArmariosPointers + idCorredor)相当于arrArmariosPointers[idCorredor]

但你真正想要的是:

pArmario* arrArmariosPointers = malloc(sizeof(pArmario) * maxCorredores);
   ^    ^
   |    |
   |    +-- pointer to (array of)
   pointers to structs