1
I have a struct Time
that has a vector char name[30]
, the problem starts when I try to copy a value to that string with the function strcpy()
, follows the code of stuct
:
typedef struct Time{
char name[30];
int pnts;
int vit, emp, der;
int gf, gt;
struct Time *next;
struct Time *prev;
}time;
and here the function that implements thetrcpy()
:
void addTime(campeonato *c, char name[]){
time t;
strcpy(t.name, name);
t.next = NULL;
t.pnts = t.vit = t.emp = t.der = t.gt = t.gf = 0;
if(c->first == NULL){
c->first = &t;
t.prev = NULL;
} else {
time *p = c->first;
while(p->next != NULL){
p = p->next;
}
p->next = &t;
t.prev = p;
}
}
To main()
is like this:
campeonato c;
createCamp(&c, "Brasileirao");
addTime(&c, "Palmeiras");
time *t = c.first;
printf("%s", t->name);
return 0;
The value of print for t->name
is always garbage.
Do a [mcve]. Maybe even discover the problem yourself, if not, at least we will have more chance to help you. In the current form does not have all information and it is very difficult to answer the question. Maybe the problem is in
campeonato
.– Maniero
To make this programming safer, since char name has a set size, I would use strncpy instead of strcpy to ensure that the array overflow does not occur.
– osmarcf
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.
– Maniero