Reputation: 21
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. 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;
}
Upvotes: 1
Views: 109
Reputation: 1466
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. 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;
}
Upvotes: 0
Reputation: 19864
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.
In the function you try to access ff[1]
so you see a crash.
You can do something like what @Joachim as suggested
Upvotes: 0
Reputation: 409166
Because you're not passing a pointer to the array to listartarefas
function. 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).
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 e.g.
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);
Upvotes: 3