-4

I have a struct element and I want to copy the element data (in all fields) to another element.

So if this is the struct

struct Node{
  char* name;
  char type;
  char* path;
};

and this is my copy function :

struct Node Node_cp(struct Node member)
{
  struct Node NewMember;
  NewMember=malloc(sizeof(struct Node))
  //**Here i get error : Assigning to struct Node from incompatible type void* (why ?)**

  NewMember.name=member.name;
  NewMember.type=member.type;
  NewMember.path=member.path;

  return NewMember;
}

- My Questions :

  1. why do i get this error on the malloc ?
  2. is this the right way to do it ? I'm going
  3. should i use malloc on newMember.path=malloc(sizeof(member.path)) and in all other fields as well ?

    editing to explain why my question is different from using typedef in struct because the system think this is a duplicate question : Well as i understand in C this are totally different questions. I asked about copying an element and not on typedef :<

Thanks

erans
  • 13
  • 3

2 Answers2

1

malloc returns a pointer to space in memory. You are trying to assign it to a non-pointer type. NewMember already has space allocated to it (in the scope of this function). To "fix" it, change the type of NewMember.

struct Node * NewMember = malloc(sizeof(struct Node));

Side note: you're going to want this function to return a pointer, or you should pass it two pointers to struct Node, where one parameter is src and one is dest, similar to the concept of memcpy. I mention this because once NewMember leaves scope, accessing the returned value is undefined behavior.

You will encounter issues with the latter part of the function as well--be sure to revise it and search how to properly copy things like strings in C.

Kcvin
  • 5,073
  • 2
  • 32
  • 54
  • that's not the only issue... – Jean-François Fabre Dec 13 '17 at 20:15
  • I didn't even read the second half of the function -_- – Kcvin Dec 13 '17 at 20:17
  • Thanks for the answer. If i return a pointer instead i can access all his fields normally when i'm out of the function scope ? – erans Dec 13 '17 at 20:19
  • Yes, but you have to remember to `free` pointers when you're done with them. – Kcvin Dec 13 '17 at 20:21
  • Great thanks. now i'm more confident in what im doing :) this basically answered my question here. the explanation that malloc returns a pointer was a great help. – erans Dec 13 '17 at 20:29
  • 1
    Just in case you're learning C now with an eye on learning C++ later... [Don't do that, learn C++ straight away.](https://www.youtube.com/watch?v=YnWhqhNdYyk&t=428s) – DevSolar Dec 13 '17 at 20:30
  • No.. I'm learning C as part of my degree. I just need to get used to it after java :( – erans Dec 13 '17 at 20:35
0

You should declare the newMember instance of your struct as a pointer, because the returning type of malloc() is of type void* .....then you should assign a pointer to a pointer.

By the way you'll need to access the structure members by -> operator instead of the dot (e.g NewMember->name = member.name;)

Also, i think you should consider passing a constant pointer to the function, it's better if we are talking limiting memory consumption.

so the function prototype could become as :

struct Node Node_cp(struct Node* member);

And finally, you have to free the memory you have allocated dynamically using : free(NewMember);

Hamza Mogni
  • 672
  • 1
  • 7
  • 21