[英]How do I pass an address of array of structs to a function?
所以我有這個問題。 我初始化結構數組的一個參數,然后在另一個函數中檢查它。 我檢查過,地址與main()中的地址相同,但有效值只是隨機的。 我不知道為什么,幫助!
int i;
STOL s[STOLY];
char choice[MAXSTRING] = "jupiiiii", ***p;
/*if ((p = (char ***) malloc(MAXSTRING * sizeof(char **))) == NULL)
exit(-5);*/
for (i = 0; i < STOLY; i++)
s[i].novy = 0;
while (strcmp(choice, "stop"))
{
puts("Casnik alebo bar?");
/* *p = nacitaj_choice();
choice = *p;*/
nacitaj_choice(choice);
free(p);
if (strcmp(choice, "casnik") == 0)
zadaj_stol(0, &s); /*HERE I SEND THE ADDRESS*/
if (strcmp(choice, "bar") == 0)
zadaj_stol(1, &s);
}
我想在另一個功能中檢查新兵
void zadaj_stol(int typ, STOL *p_s[STOLY])
{
int stol;
printf("Stol cislo: ");
stol = (cislo_stola() - 1);
if (!p_s[stol]->novy) /*HERE IS THE PROBLEM*/
reset_stol(p_s[stol]);
zadaj_udaj(typ, p_s[stol]);
vypis_stol(p_s[stol]);
}
我檢查了一下,並且p_s與&s相同,但是出於某種原因,p_s [stol]-> novy始終類似於-3782126。 btw stol在0到13之間
因為我還不能回答我的問題,所以這里是我想出的部分解決方案。 問題是,僅當p_s的索引為0時才有效,即p_s [0]-> novy正常工作,但是p_s [1]給出調用堆棧,但它不知道地址。 我不知道為什么。
int main()
{
int i;
STOL s[STOLY], **p_s;
if ((p_s = (STOL **) malloc(sizeof(STOL))) == NULL)
return -5;
*p_s = s;
char choice[MAXSTRING] = "jupiiiii";
for (i = 0; i < STOLY; i++)
s[i].novy = 0;
while (strcmp(choice, "stop"))
{
puts("Casnik alebo bar?");
nacitaj_choice(choice);
if (strcmp(choice, "casnik") == 0)
zadaj_stol(0, p_s);
if (strcmp(choice, "bar") == 0)
zadaj_stol(1, p_s);
}
return 0;
}
void zadaj_stol(int typ, STOL **p_s)
{
int stol;
printf("Stol cislo: ");
stol = (cislo_stola() - 1);
if (!p_s[stol]->novy)
reset_stol(p_s[stol]);
zadaj_udaj(typ, p_s[stol]);
vypis_stol(p_s[stol]);
}
改變這個
void zadaj_stol(int typ, STOL *p_s[STOLY])
{
int stol;
printf("Stol cislo: ");
stol = (cislo_stola() - 1);
if (!p_s[stol]->novy) /*HERE IS THE PROBLEM*/
reset_stol(p_s[stol]);
zadaj_udaj(typ, p_s[stol]);
vypis_stol(p_s[stol]);
}
成為這個:
void zadaj_stol(int typ, STOL (*p_s)[STOLY])
{
int stol;
printf("Stol cislo: ");
stol = (cislo_stola() - 1);
if (!(*p_s)[stol].novy)
reset_stol((*p_s)[stol]);
zadaj_udaj(typ, (*p_s)[stol]);
vypis_stol((*p_s)[stol]);
}
只需在調用函數s
位置將&s
更改為s
,然后從函數的參數列表中刪除[STOLY]
。
只需將&s
更改為s
即可傳遞數組。 您將收到錯誤error: invalid type argument of '->'
因為p_s
的類型為STOL*
,這意味着p_s[stol]
的類型為STOL
(不是指針)。 ->
的左側操作數應為指針。 你可以做:
if(!p_s[stol].novy)
代替。
是的,過了一會兒我解決了。 萬一將來有人遇到同樣的問題,這里就是解決方案。 因為我有s[STOLY]
,結構數組,所以s
本身就是一個指針。 因此,當我想將其作為指針傳遞給zadaj_stol()
函數時,它應該只是s
。 然后在zadaj_stol()
函數中, s
是指針,而s[stol]
是結構,因此使用.
符號。 然后再次將它作為指針傳遞,我使用了&s[stol]
。 這是示例代碼(此后我做了很多更改,但是原理適用。
int main()
{
int i, io;
STOL s[STOLY];
char choice[MAXSTRING] = "jupiiiii"; /*random string*/
for (i = 0; i < STOLY; i++) {
s[i].novy = 1;
s[i].ucet = 0;
}
while (strcmp(choice, "stop"))
{
puts("\n**********************************\n");
puts("Vstup alebo vystup? (Pre navod napis help, pre ukoncenie stop)");
nacitaj_choice(choice);
if ((strcmp(choice, "vstup")) == 0) {
io = 0;
typ_vstup(s, io);
}
else if ((strcmp(choice, "vystup")) == 0) {
io = 1;
typ_vstup(s, io);
}
else if ((strcmp(choice, "help")) == 0)
help();
}
ziskaj_obrat(s);
getchar();
return 0;
}
以及在這樣的功能中的用途
void zadaj_stol(int typ, STOL p_s[], int io) /*nacita cislo_stola, ak potrebne zresetuje ho, a bud vola zadaj_udaj alebo vypis_stol*/
{
int stol;
printf("Stol cislo: ");
stol = (cislo_stola() - 1);
if (p_s[stol].novy)
reset_stol(&p_s[stol]);
if (!io)
zadaj_udaj(typ, &p_s[stol]);
else
vypis_stol(typ, &p_s[stol]);
}
重要的是要意識到什么是價值,什么是指針。 因為我有一個數組s[14]
,所以s
是指向數組開頭的指針, s[whatever]
是一個值(在我的情況下為結構),而&s[whatever]
是該結構的地址/指針。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.