1

I've tried to create a function that duplicate a structure into a pointer, but here is the issue, there is a char tab into the structure and I can't assign my original value to the new structure.

The function :

Planete *dupliquer(Planete *p){
  Planete *res;  
  res = (Planete*) malloc(sizeof(Planete));
  if(res == NULL){
    printf("Erreur d'allocation...\n");
    exit(1);
  }
  res->nomplanete = p->nomplanete;
  res->rayon = p->rayon;

  return res;
}

And here is the compilator error :

 error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’
   res->nomplanete = p->nomplanete;
                   ^

Can you please help me, It will be very nice. Thanks for your support !

Arun A S
  • 6,421
  • 4
  • 29
  • 43
oktomus
  • 566
  • 7
  • 28
  • 4
    `memcpy(res, p, sizeof(Planete))` – tux3 Mar 02 '15 at 15:34
  • 3
    "duplicate a structure into a pointer" Huh? – Lundin Mar 02 '15 at 15:35
  • 1
    Thanks but I have to use the malloc function, do you have any solution for my error ? – oktomus Mar 02 '15 at 15:36
  • 1
    @tux3 has already given a simple solution, but for future reference note that your problem is caused by trying to use `=` to assign char * strings (you would need to use strcpy or similar for this). Also note that [you should not cast the result of malloc in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Paul R Mar 02 '15 at 15:36
  • 1
    Don't cast the return value of `malloc()` it's not necessary. – Iharob Al Asimi Mar 02 '15 at 15:37
  • I create a Planete, the I duplicate it into a pointer I created and allocate into memory – oktomus Mar 02 '15 at 15:37
  • I don't really understand the cast behind the malloc, this is my teacher instruction ^^ – oktomus Mar 02 '15 at 15:45
  • 1
    @KevinMasson Your teacher is bad, tell them to go [read this](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Lundin Mar 02 '15 at 15:45
  • Ahah @Lundin, unfortunatelly, i can't :p – oktomus Mar 02 '15 at 15:47
  • 2
    Well, this is a major problem in the whole software industry: way too many crappy teachers and crappy books. – Lundin Mar 02 '15 at 15:49
  • I shown it to my teacher and he didn't understand why it works x) Thank you :) – oktomus Mar 02 '15 at 15:51
  • Your teacher is probably confused between C and C++ - in C you should not use a cast with malloc, in C++ you have to (but then you shouldn't be using malloc in C++ anyway). – Paul R Mar 02 '15 at 22:33

2 Answers2

2

It looks like you do not need a separate malloc for nomplanete, because it is an array. In situations like that you should use strcpy, like this:

strcpy(res->nomplanete, p->nomplanete);

If all members of Planete are primitives or arrays, you could simply memcpy the entire thing, like this:

res = malloc(sizeof(Planete)); // No need to cast
if(res == NULL){
    printf("Erreur d'allocation...\n");
    exit(1);
}
memcpy(res, p, sizeof(Planete));

If nomplanete were a pointer, you would have to do a separate malloc or use strdup:

res->nomplanete = malloc(1+strlen(p->nomplanete));
strcpy(res->nomplanete, p->nomplanete);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • If "nomplanete" was an array as member of the struct, then memcpy() on the whole struct would have worked just fine. – Lundin Mar 02 '15 at 15:40
0

It appears that "nomplanete" is a character array member of the "Planete" struct. In that case, simply write the function as:

Planete* dupliquer (const Planete* p)
{
  Planete* res;  
  res = malloc(sizeof(Planete));
  if(res == NULL){
    printf("Erreur d'allocation...\n");
    exit(1);
  }
  memcpy(res, p, sizeof(Planete));

  return res;
}
Lundin
  • 195,001
  • 40
  • 254
  • 396