简体   繁体   English

无法打印struct

[英]Can't print struct

I'm trying to print the members of a struct called Task but it's only printing the first member and then it gives me a segmentation fault. 我正在尝试打印一个名为Task的结构的成员,但它只打印第一个成员,然后它给我一个分段错误。 Could somebody help me? 有人能帮帮我吗?

This is my code: 这是我的代码:

void listartarefas(Task *ff)
{
 int i;
 for(i=0;i<=1;i++)
   {
    if(ff[i].tipo == 1)
      { 
        printf("Agendada:\n");
        printf("%d\n",ff[i].identf);
        printf("%s\n",(ff[i].path));
        printf("%d-%d-%d\n",(ff[i].ano),(ff[i].mes),(ff[i].dia));
        printf("%d:%d:%d\n",(ff[i].hora),(ff[i].minuto),(ff[i].segundo));
       }
    else
       {
        printf("Executada:\n");
        printf("%d\n",ff[i].identf);
        printf("%s\n",(ff[i].path));
        printf("%d-%d-%d\n",(ff[i].ano),(ff[i].mes),(ff[i].dia));
        printf("%d:%d:%d\n",(ff[i].hora),(ff[i].minuto),(ff[i].segundo));
        }  
   }
}

int main()
{
 Task tf={2,1,"home/fsm/mieti/projB/Makefile",17,20,00,1,5,22};
 Task tt={3,0,"home/fsm/mieti/projB/Makefile",17,22,34,1,4,44};

 Task *ff[]={&tf,&tt};

 listartarefas(*ff);
 return 0;
 }

Because you're not passing a pointer to the array to listartarefas function. 因为您没有将指向数组的指针传递给listartarefas函数。 instead you dereference the array, which gives you the first element only, which is a pointer to a single structure, and you loop like it was two, leading to [ undefined behavior }( http://en.wikipedia.org/wiki/Undefined_behavior ). 相反,你取消引用数组,它只给你第一个元素,它是一个指向单个结构的指针,你像它一样循环,导致[ 未定义的行为 }( http://en.wikipedia.org/wiki/ Undefined_behavior )。

There are a few weird things in your code, like you having an array of pointer, instead of simply an array of structures, which means you either have to change your function to accept an array of pointer or to change the array from an array of pointers to an array of structures. 你的代码中有一些奇怪的东西,比如你有一个指针数组,而不仅仅是一个结构数组,这意味着你要么必须改变你的函数来接受一个指针数组,要么改变一个数组中的数组指向结构数组的指针。

My recommendation is to use a array of structures, and just pass it as is, because arrays naturally decays to pointers. 我的建议是使用一个结构数组,并按原样传递它,因为数组自然地衰减到指针。 So do eg 所以,例如

Task ff[] = {
    {2,1,"home/fsm/mieti/projB/Makefile",17,20,00,1,5,22},
    {3,0,"home/fsm/mieti/projB/Makefile",17,22,34,1,4,44}
};

listartarefas(ff);
 listartarefas(*ff);

So this call is equivalent to 所以这个电话相当于

listartarefas(ff[0]);

Now what you are passing is the first element of the array ff which is the pointer to structure. 现在你传递的是数组ff的第一个元素,它是结构的指针。 In the function you try to access ff[1] so you see a crash. 在该函数中,您尝试访问ff[1]以便看到崩溃。

You can do something like what @Joachim as suggested 您可以像建议的@Joachim那样做

The problem is here 问题出在这里

listartarefas(*ff);

You are passing the indirection of *ff[], which is the first element of the array that you intend to pass. 您正在传递* ff []的间接,这是您要传递的数组的第一个元素。 You might want to do this instead: 您可能想要这样做:

void listartarefas(Task **ff)
{
 int i;
 for(i=0;i<=1;i++)
   {
    if(ff[i]->tipo == 1)
      {
        printf("Agendada:\n");
        printf("%d\n",ff[i]->identf);
        printf("%s\n",(ff[i]->path));
        printf("%d-%d-%d\n",(ff[i]->ano),(ff[i]->mes),(ff[i]->dia));
        printf("%d:%d:%d\n",(ff[i]->hora),(ff[i]->minuto),(ff[i]->segundo));
       }
    else
       {
        printf("Executada:\n");
        printf("%d\n",ff[i]->identf);
        printf("%s\n",(ff[i]->path));
        printf("%d-%d-%d\n",(ff[i]->ano),(ff[i]->mes),(ff[i]->dia));
        printf("%d:%d:%d\n",(ff[i]->hora),(ff[i]->minuto),(ff[i]->segundo));
        }
   }
}

int main()
{
    Task tf={2,1,"home/fsm/mieti/projB/Makefile",17,20,00,1,5,22};
    Task tt={3,0,"home/fsm/mieti/projB/Makefile",17,22,34,1,4,44};

    Task *ff[]={&tf,&tt};

    listartarefas(ff);
    return 0;
}

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

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