2013-06-25 36 views
4

我需要將指針的地址傳遞給一個結構到一個函數,該函數將動態地爲結構數組分配內存並填充值。瞭解如何動態創建一個結構數組並訪問其元素

現在從我的調用方法,一旦我從func1返回,我應該能夠遍歷結構數組並顯示結構變量的值。

有人可以解釋如何將指針地址傳遞給結構,也迭代通過動態創建的結構數組?

我的示例代碼如下所示:

struct test { 
    int a; 
    int b; 
}; 

void func1(int *n,struct test **testobj) 

{ 
    n=5; 
    *testobj = (struct test*) malloc(n*sizeof(struct test)); 
    for(i=0;i<n;i++) 
    { 
     (*testobj)[i].a=1; 
     (*testobj)[i].b=2; 
    } 
} 

int main() 
{ 
    struct test testobj;int n; 
    func1(&n,&testobj); 
    for(i=0;i<n;i++) 
    { 
     printf("%d %d",(*testobj)[i].a,*testobj)[i].b); 
    } 
    free(testobj); 
} 

回答

0

創建指針數組在聲明步驟本身的結構和簡單地將它傳遞給函數

struct test *testobj[10]; 
func1(&n,testobj); 

這通過指針全陣列到函數

+0

這不是被要求什麼。 –

3

在main()中定義一個指向test結構的指針:

struct test *testPtr; 

要採取指針的地址使用&地址運算符:

&testPtr; 

這將返回指針的地址,且類型struct test **

然後就可以進入這個你功能func1,它做正確的分配(雖然鑄造malloc()通常被認爲是不好的做法 - Do I cast the result of malloc?)。除此之外func1()看起來不錯...線...

*testobj = malloc(n*sizeof(struct test)); 

...是正確的。 *testobj取消引用您通過執行&testPtr獲得的雙指針,並將新內存的地址存儲在指針中。當您使用(*testobj)[i]取消引用雙指針時,您也是正確的,因爲[]的優先級高於您需要的*(正如您已經正確完成的那樣),並使用括號括起取消引用,以確保在採用索引之前發生這種情況。

因此,當func1()返回指針testPtr現在應該指向分配,並且可以使用testPtr[i].a

編輯訪問ntest結構的陣列:您的循環應該成爲

for(i=0;i<n;i++) 
    printf("%d %d", testobj[i].a, testobj[i].b); 

你原來的循環應該給你編譯錯誤?在原始代碼testobj不是一個指針,因此解引用它不應該是可能的。

所以摘要答案是main()聲明testobj作爲指針然後訪問數組元素作爲testobj[n] :)

編輯:埃裏克已經指出的那樣,從func1()除去n=5;。我認爲你的意思是*n=5也許是某種調試步驟......你可能的意思是使用n作爲函數的輸入來說明你的結構數組中有多少個對象。無論是初始化n或者是重新定義func1()

void func1(int n,struct test **testobj) // n is no longer a poitner, just a number 
+0

是的,好點!謝謝:) – Jimbo

+0

還公道點,謝謝反饋 – Jimbo

+0

啊!這是我從循環內部訪問結構元素,現在已經搞亂了。現在清楚了!是的,在這段代碼中,n是無關緊要的。我需要它來達到我的調試目的!非常感謝:) – soba

0

出現您的代碼非常好的給我。 應該讓它罰款is--

代替

struct test testobj; 

只能編輯把下面的代碼

struct test *testobj; 

並保留其餘的,因爲它是..!

這裏是我們需要的,這裏的記憶是一樣需要

#include <stdlib.h> 
#include <stdio.h> 
struct tests { 
    int a; 
    int b; 
}; 

void func1(int *n,struct tests **testobj) 

{ 
    int i; 
    *n=5; 
    *testobj = (struct tests*) malloc((*n)*sizeof(struct tests)); 
    for(i=0;i<(*n);i++) 
    { 
     (*testobj)[i].a=1; 
     (*testobj)[i].b=2; 
    } 
} 

int main() 
{ 
    int i; 
    struct tests *testobj;int n; 
    func1(&n,&testobj); 
    for(i=0;i<(n);i++) 
    { 
     printf("%d %d",(testobj)[i].a,testobj[i].b); 
    } 
    free(testobj); 
} 
0

這是不完全清楚你問的是哪個版本的調用函數分配的工作版本,但這些人應覆蓋它:

/* allocate some number of tests. 
* 
* out_n: out parameter with array count 
* returns: an array of tests 
*/ 
struct test* allocate_some_tests(int *out_n) { 
    int n = 5; /* hardcoded, random or otherwise unknown to caller */ 
    *out_n = n 
    struct test *t = malloc(n * sizeof(*t)); 
    while (n--) { 
     t[n].a = 1; 
     t[n].b = 2; 
    } 
    return t; 
} 

/* allocate a specific number of tests. 
* 
* n: in parameter with desired array count 
* returns: an array of tests 
*/ 
struct test* allocate_n_tests(int n) { 
    struct test *t = malloc(n * sizeof(*t)); 
    while (n--) { 
     t[n].a = 1; 
     t[n].b = 2; 
    } 
    return t; 
} 

請注意,您可以只回報分配的數組,你並不需要一個指針到指針在這裏。

至於打電話給他們,並遍歷結果:

void print_tests(struct test *t, int n) { 
    for (; n--; t++) 
     printf("{%d, %d}\n", t->a, t->b); 
} 

int main() 
{ 
    int count1; /* I don't know how many yet */ 
    struct test *array1 = allocate_some_tests(&count1); 
    print_tests(array1, count1); 

    int count2 = 3; /* I choose the number */ 
    struct test *array2 = allocate_n_tests(count2); 
    print_tests(array2, count2); 
} 
相關問題