[英]understanding how to dynamically create an array of structure and access its elements
我需要將指向結構的指針的地址傳遞給函數,該函數將動態地為結構數組分配內存並填充值。
現在從我的調用方法,一旦我從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);
}
在main()中定義一個指向test
結構的指針:
struct test *testPtr;
要獲取該指針的地址,請使用&
address-of運算符:
&testPtr;
這將返回指針的地址,並具有類型struct test **
然后你可以將它傳遞給你的函數func1
,它執行正確的分配(盡管cast malloc()
通常被認為是不好的做法 - 我是否應該轉換malloc()
的結果? )。 除了那個func1()
看起來不錯......這條線......
*testobj = malloc(n*sizeof(struct test));
... 是正確的。 *testobj
取消引用您通過執行&testPtr
獲得的雙指針,並將新內存的地址存儲在指針中。 當你提領你的雙指針使用你也是正確的(*testobj)[i]
因為[]
的優先級高於*
您需要(如你做得正確)用方括號括提領,以確保您之前發生拿指數。
因此,當func1()
返回指針時, testPtr
現在應該指向您分配的n
test
結構的數組,並且可以使用testPtr[i].a
等進行訪問。
編輯:你的for循環應該成為
for(i=0;i<n;i++)
printf("%d %d", testobj[i].a, testobj[i].b);
你原來的for循環應該給你編譯錯誤? 在原始代碼中testobj
不是指針,因此不應該取消引用它。
所以總結答案在main()
聲明testobj
作為指針 ,然后作為testobj[n]
:)訪問數組元素
編輯:正如埃里克指出的那樣,刪除n=5;
來自func1()
。 我認為你的意思是*n=5
或者作為某種調試步驟...你可能意味着使用n
作為函數的輸入來說明你想要在結構數組中有多少個對象。 初始化n
或者可能重新定義func1()
void func1(int n,struct test **testobj) // n is no longer a poitner, just a number
在聲明步驟本身中創建指向結構的指針數組,並將其簡單地傳遞給函數
struct test *testobj[10];
func1(&n,testobj);
這會將整個指針數組傳遞給函數
您的代碼對我來說非常好。 只編輯應該讓它好 - 是
代替
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);
}
您要求的版本並不完全清楚,但其中一個應該涵蓋它:
/* 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.