2011-04-02 45 views
2

我有這種複雜的結構的thingie:C和動態結構元件訪問

#include <stdlib.h> 

typedef struct { 
    int x; 
    int y; 
} SUB; 

typedef struct { 
    int a; 
    SUB *z; 
} STRUCT; 

#define NUM 5 

int main(void) 
{ 
    STRUCT *example; 
    int i; 

    example = malloc(sizeof(STRUCT)); 

    example->z = malloc(NUM * sizeof(SUB)); 

    for(i = 0; i < NUM; ++i) { 
     /* how do I access variable in certain struct of array of z's */ 
    } 

    return 0; 
} 

example是動態分配的結構和zexample內部被動態分配SUB結構的陣列。

如何在結構的某個元素z中訪問某個變量?

我一直在嘗試這樣的事情:example->z[i].x但它似乎沒有工作。

目前我使用這個破舊尋找workaraound:

SUB *ptr = example->z; 
int i; 

for(i = 0; i < amount_of_z_structs; ++i) { 
    /* do something with 'ptr->x' and 'ptr->y' */ 
    ptr += sizeof(SUB); 
} 
+0

我沒有看到你的代碼中的任何'z'。你可以解釋嗎? – SuperSaiyan 2011-04-02 02:45:13

+0

真是一團糟。 XrM,請清除'z'和'b'。 – karlphillip 2011-04-02 02:55:11

+0

其他人已經與他們搞砸了,然後也刪除了他的答案。 – XrM 2011-04-02 02:56:12

回答

1

你的問題是不是你說的是。張貼您的代碼提供了一個編譯錯誤:

error: request for member ‘z’ in something not a structure or union 

在該行

example.z = malloc(sizeof(STRUCT)); 

,因爲你的意思是寫example->z,因爲example是一個指向STRUCT,不是STRUCT

從那裏,你可以訪問example->z[i].x正如你所說。該語法一直很好。

例如:

/* your declarations here */ 

example = malloc(sizeof(STRUCT)); 
example->z = malloc(NUM * sizeof(SUB)); 

for(i = 0; i < NUM; ++i) { 
    example->z[i].x = i; 
    example->z[i].y = -i; 
    printf("%d %d\n", example->z[i].x, example->z[i].y); 
} 

/* output: 
0 0 
1 -1 
2 -2 
3 -3 
4 -4 
*/ 
+0

是的,我的錯誤,我錯過了它的例子。即使使用-Wall選項啓用,我的實際程序也不會給出任何編譯錯誤。 valgrind也給我一份清晰的論文。 – XrM 2011-04-02 03:19:49

+0

@Xrm:我的程序正好給出*我期望的輸出。我不知道你的問題是什麼 - 它不在這裏。 – Cascabel 2011-04-02 03:39:06

+0

我用我的Windows Dev-C++ IDE和它的打印垃圾進行了編譯。當我用'GCC 4.4'在我的unix盒子上編譯相同的代碼時,它可以工作。與Dev-C++捆綁在一起的舊GCC版本有可能會破壞。 – XrM 2011-04-02 03:48:12

0

當你有一個指針指向指針你往往最終運行到優先級的問題。我不記得這是否是一個,但你可以嘗試(example->b)[i].x

+0

與'example-> z [i] .x'相同。注意:'b'改爲'z'。 – XrM 2011-04-02 03:05:38

0

嘗試這種情況:

int my_x = example[3].z[2].x; 
  • 上面的代碼將首先訪問的示例[3](示例中數組的第四個元素)。
  • 一旦你得到那個特定的元素,它的內容就可以像使用普通對象一樣自動訪問。
  • 然後,您可以從該元素訪問z [2]。請注意,example[3]是一個元素,因此您可以使用.來訪問其成員;如果它是一個數組,你可以將它作爲一個數組來訪問。
  • 所以到目前爲止,example[3].z[2]是內部的example陣列的一個元件的SUB陣列的一個元件。
  • 現在您可以使用上面顯示的方式簡單訪問會員x

typedef struct { 
    int x; 
    int y; 
} SUB; 

typedef struct { 
    int a; 
    SUB *z; 
} STRUCT; 

STRUCT *example; 

int main() { 
    example = malloc(sizeof(STRUCT)*10); //array of 10; 
    int i=0,j=0; 
    for (;i<10;i++){ 
     example[i].a = i; 
     example[i].z = malloc(sizeof(SUB)*5); 
     for (j=0; j<5; j++) 
      example[i].z[j].x = example[i].z[j].y = j; 
    } 
    //access example[3] and access z[2] inside it. And finally access 'x' 
    int my_x = example[3].z[2].x; 
    printf("%d",my_x); 

    for (i=0;i<10;i++){ 
     printf("%d |\n",example[i].a); 
     //example[i].z = malloc(sizeof(SUB)*5); 
     for (j=0; j<5; j++) 
      printf("%d %d\n",example[i].z[j].x,example[i].z[j].y); 
     free(example[i].z); 
    } 
    free(example); 
} 

+0

不是特別有用的答案。如果所有東西都被分配來完成這個工作,'example-> z [2] .x'也可以工作,就像example [0] .z [2] .x'一樣。 – Cascabel 2011-04-02 03:12:59

+0

我從來沒有否認你說過的話。他的語法沒有錯。只有合乎邏輯的方式。 :-) – SuperSaiyan 2011-04-02 03:31:42

+0

@Thrustmaster:邏輯也不一定有問題。指針並不總是指向數組 - 它們也可以是單個對象。 – Cascabel 2011-04-02 03:45:35

0

首先,你的第二個malloc是錯誤的; example是一個指針,所以這樣的:

example.z = malloc(NUM * sizeof(SUB)); 

應該是這樣的:

example->z = malloc(NUM * sizeof(SUB)); 

然後在你的循環,可以說這樣的事情:

example->z[i].x = i; 
example->z[i].y = i; 

你也想擁有這附近的文件頂部:

#include <stdlib.h> 
+0

已經固定'.' - >' - >'。 2.我知道'stdlib.h'是需要的。 – XrM 2011-04-02 03:39:57

0

在「寒酸處理方法」,你寫道:

SUB *ptr = example->z; 
int i; 

for(i = 0; i < amount_of_z_structs; ++i) { 
    /* do something with 'ptr->x' and 'ptr->y' */ 
    ptr += sizeof(SUB); 
} 

這裏的問題是,C由物體的大小縮放指針指向的,所以當你添加1至SUB指針,該值是通過先進sizeof(SUB)。所以,你只需要:

SUB *ptr = example->z; 
int i; 

for (i = 0; i < NUM; ++i) { 
    ptr->x = ptr->y = 0; 
    ptr++; 
} 

當然,正如其他人所說,你也可以做(假設C99):

for (int i = 0; i < NUM; ++i) 
    example->z[i].x = example->z[i].y = 0; 
0
#include<stdio.h> 
#include<stdlib.h> 
#include<conio.h> 

#define NUM 5 

typedef struct 
{ 
    int x; 
    int y; 
}SUB; 

typedef struct 
{ 
    int a; 
    SUB* z; 
}STRUCT; 

void main(void) 
{ 
    clrscr(); 
    printf("Sample problem..\n\n"); 

    STRUCT* example; 
    int i; 

    example = (STRUCT*)malloc(sizeof(STRUCT)); 
    example->z = (SUB*)malloc(NUM * sizeof(SUB)); 

    for(i = 0; i < NUM; i++) 
    { 
     example->z[i].x = i +1; 
     example->z[i].y = (example->z[i].x)+1; 
     printf("i = %d: x:%d y:%d\n", i, example->z[i].x, example->z[i].y); 
    } 
} 
+0

可能你剛剛沒有訪問該結構... – 2011-04-02 09:18:17