简体   繁体   English

如何将结构数组的地址传递给函数?

[英]How do I pass an address of array of structs to a function?

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. 我检查过,地址与main()中的地址相同,但有效值只是随机的。 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. 我检查了一下,并且p_s与&s相同,但是出于某种原因,p_s [stol]-> novy始终类似于-3782126。 btw stol is between 0 and 13 btw stol在0到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. 问题是,仅当p_s的索引为0时才有效,即p_s [0]-> novy正常工作,但是p_s [1]给出调用堆栈,但它不知道地址。 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. 只需将&s更改为s即可传递数组。 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). 您将收到错误error: invalid type argument of '->'因为p_s的类型为STOL* ,这意味着p_s[stol]的类型为STOL (不是指针)。 The left side operand of -> should be a pointer. ->的左侧操作数应为指针。 You can do: 你可以做:

if(!p_s[stol].novy) instead. if(!p_s[stol].novy)代替。

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. 因为我有s[STOLY] ,结构数组,所以s本身就是一个指针。 Therefore, when I wanted to pass it to the zadaj_stol() function as a pointer, it should just be s . 因此,当我想将其作为指针传递给zadaj_stol()函数时,它应该只是s Then in zadaj_stol() function s was a pointer but s[stol] was a structure, therefore use the . 然后在zadaj_stol()函数中, s是指针,而s[stol]是结构,因此使用. notation. 符号。 Then to pass it on as a pointer again, I used &s[stol] . 然后再次将它作为指针传递,我使用了&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. 因为我有一个数组s[14] ,所以s是指向数组开头的指针, s[whatever]是一个值(在我的情况下为结构),而&s[whatever]是该结构的地址/指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM