這裏是一塊代碼,我不明白數組的指針,更用C
#include "malloc.h"
/*some a type A and type for pointers to A*/
typedef struct a
{
unsigned long x;
} A, *PA;
/*some a type B and type for pointers to B*/
typedef struct b
{
unsigned char length;
/*array of pointers of type A variables*/
PA * x;
} B, *PB;
void test(unsigned char length, PB b)
{
/*we can set length in B correctly*/
b->length=length;
/*we can also allocate memory for the array of pointers*/
b->x=(PA *)malloc(length*sizeof(PA));
/*but we can't set pointers in x*/
while(length!=0)
b->x[length--]=0; /*it just would not work*/
}
int main()
{
B b;
test(4, &b);
return 0;
}
任何人都可以在概念上闡述對我,爲什麼我們不能在數組x組指針測試()?
這不起作用?含義,它不會編譯?或者沒有按預期行事? – 2011-04-06 14:41:37
'malloc.h'不是標準的C頭。使用'stdlib.h'。 – 2011-04-06 14:41:38
我不認爲這是關於編譯器或頭文件。我使用VS2010編碼,項目類型爲空控制檯。 – 2011-04-06 14:49:49