2012-04-29 81 views
1

結構如下,內部結構體位於外部結構體內部。這兩個結構都是動態分配的。當我嘗試通過引用來訪問內部結構來更改地址值時,會出現問題。在C中訪問動態分配的嵌套結構體

例如:

typedef struct{ 
    char address[32]; 
}inner; 

typedef struct{ 
    struct inner innerStruct; 
}outer; 

main(){ 

    int i = 0; 
    outer* outerArray; 
    outer* outerReference; 
    inner* innerReference; 

    /* create 20 outer structs */ 
    outerArray = malloc(20 * sizeof(outerArray*)); 

    /* for each outer struct, dynamically allocate 10 inner structs */ 

    for(i = 0; i < 10; i++) 
    { 
     outerReference = outerArray + i; 
     outerReference->innerStruct = malloc(10 * sizeof(outerReference->innerStruct); 
    } 

} 

如何訪問outerArray[3][innerStruct[4],第四外結構的第五內部結構,並改變它的地址值?

+2

該代碼無法編譯(出於多種原因)。除了別的之外,你似乎正在將指針與對象實例混同起來。 –

+0

爲什麼downvote?這似乎是某人學習探究結構,指針和內存分配錯綜複雜的一個完全合法的問題。 –

回答

2

如果您要使用malloc(),則聲明innerStructinner *,而不是struct inner

正如您聲明的那樣,創建outer時分配了inner的內存。

還請注意,由於您已使用typedef,因此在聲明該類型的變量時,不需要struct關鍵字。

這是你的代碼的修正版本,編譯和運行:

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

typedef struct { 
    char address[32]; // 32 chars are allocated when an inner is created 
} inner; 

typedef struct { 
    inner innerStruct; // innerStruct is allocated when an outer is created 
} outer; 

typedef struct { 
    inner *innerStruct; // innerStruct must be allocated explicitly 
} outer2; 

int main(int argc, char *argv[]) { 
    int i = 0; 
    outer *outerArray; 
    outer2 *outer2Array; 

    outer *outerReference; 
    outer2 *outer2Reference; 

    /* create 20 outer structs (should check for out-of-mem error) */ 
    outerArray = malloc(20 * sizeof(outer)); 

    for (i = 0; i < 10; ++i) { 
    outerReference = outerArray + i; // ptr to i'th outer 
    // Note: innerStruct.address bcz it's a structure 
    sprintf(outerReference->innerStruct.address, "outer struct %d", i); 
    } 

    /* create 20 outer2 structs */ 
    outer2Array = malloc(20 * sizeof(outer2)); 

    /* for each outer struct, dynamically allocate 10 inner structs */ 
    for (i = 0; i < 10; ++i) { 
    outer2Reference = outer2Array + i; 
    outer2Reference->innerStruct = malloc(sizeof(inner)); 
    // Note: innerStruct->address bcz it's a pointer 
    sprintf(outer2Reference->innerStruct->address, "outer2 struct %d", i); 
    } 

    /* print all the data and free malloc'ed memory */ 
    for (i = 0; i < 10; ++i) { 
    printf("outer: %-20s, outer2: %-20s\n", 
     outerArray[i].innerStruct.address, 
     outer2Array[i].innerStruct->address); 
     free(outer2Array[i].innerStruct); 
    } 
    free(outer2Array); 
    free(outerArray); 
    return 0; 
} 
+0

請上帝不要'void main'這是很糟糕的原因很多。 http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1043284376&answer=1044841143 – lukecampbell

+0

@lukecampbell:很好,趕快,謝謝。更正,另一個很好的參考:http://users.aber.ac.uk/auj/voidmain.cgi –

+0

我覺得這將打印出每個外部結構的第一個內部結構的地址。不會有2個循環?第一個循環通過外部結構和嵌套循環通過每個外部結構的內部結構? – user1177044

0

打印第四外部結構的第五內strucuture成員address,將數據複製到它並打印一遍:

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

typedef struct{ 
    char address[32]; 
}inner; 

typedef struct{ 
    inner* innerStruct; 
}outer; 

int main() 
{ 
    int i = 0; 
    outer* outerArray; 
    outer* outerReference; 
    /* inner* innerReference; */ /* unused */ 

    /* create 20 outer structs */ 
    outerArray = malloc(20 * sizeof(*outerArray)); 

    /* for each outer struct, dynamically allocate 10 inner structs */ 

    for(i = 0; i < 10; i++) 
    { 
     outerReference = outerArray + i; 
     outerReference->innerStruct = malloc(10 * sizeof(*outerReference->innerStruct)); 
    } 

    printf("address: '%s'\n", outerArray[3].innerStruct[4].address); 

    strcpy(outerArray[3].innerStruct[4].address, "<emtpy>"); 

    printf("address: '%s'\n", outerArray[3].innerStruct[4].address); 

    return 0; 
} 

下次您發佈代碼時,請如此友好並進行編譯。