strcpy() in struct string

Asked

Viewed 1,567 times

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.

  • 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.

  • 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.

1 answer

2

I made a minimal, complete and verifiable example. I didn’t try to make it cute or improve the other code problems, but this one is solved. The team object is being created locally in the function addTime(), therefore in the stack, when you leave the function, you no longer have access to the object, so you cannot access it. The correct thing is, or allocate the object in main() and pass a pointer to it, or allocate in the heap to survive the end of the function. More or less like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Time{
  char name[30];
  int pnts;
  int vit, emp, der;
  int gf, gt;
  struct Time *next;
  struct Time *prev;
} time;

typedef struct campeonato { time *first; } campeonato;

void addTime(campeonato *c, char name[]) {
    time *t = malloc(sizeof(time));
    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;
    }
}

int main(void) {
    campeonato c = { .first = NULL };
    addTime(&c, "Santos");
    time *t = c.first;
    printf("%s", t->name);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.