So I have this problem. I initialize one parameter of my array of structs, then I want to check it in another function. I checked, the address is the same as in the main(), but the vaule is just random. I don't know why, HELP!
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);
}
and I want to check the novy in another function
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]);
}
I checked, and p_s is the same as &s, but for some reason p_s[stol]->novy is always something like -3782126. btw stol is between 0 and 13
Because I can't answer my question yet, here is the partial solution that I've figured out. Problem is, it only works if the index of p_s is 0, ie p_s[0]->novy works fine, but p_s[1] give call stack, it doesn't know the address. I'm not sure why.
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]);
}
Change this
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]);
}
be become this:
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]
。
Just change &s
to s
to pass the array. You are getting the error error: invalid type argument of '->'
because p_s
is of type STOL*
, which means p_s[stol]
is of type STOL
(not a pointer). The left side operand of ->
should be a pointer. You can do:
if(!p_s[stol].novy)
instead.
So yeah I solved it after a while. Just in case anyone has the same problem in the future, here is the solution. Because I had s[STOLY]
, array of structures, s
on its own is a pointer. Therefore, when I wanted to pass it to the zadaj_stol()
function as a pointer, it should just be s
. Then in zadaj_stol()
function s
was a pointer but s[stol]
was a structure, therefore use the .
notation. Then to pass it on as a pointer again, I used &s[stol]
. Here is the sample code (I've changed a lot since then, but the principle applies.
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;
}
and the use that in function like this
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]);
}
it's important to realise what is a value, and what is a pointer. Because I had an array s[14]
, s
is a pointer to the beginning of the array, s[whatever]
is a value (structure in my case), and &s[whatever]
is an address/pointer to the structure.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.