0

I have this function in which i am trying to assign values to the members of the structure.

  void Add(NodeDef **Head, char *Data){        
       ptrHead=(struct Node **)malloc(sizeof(struct Node));
         (*Head)->Data=*(Data);
           (*Head)->NextNode=NULL;
}

I am calling this fuction like that

for (i = 0; i < 5; i++)
    AddToLinkedList( &Head, iData[i].name);

iData is member of a structure which stores data as strings (char iData[50]).

Now i am getting an error like

error: assignment to expression with array type
  (*Head)->Data=*(Data);
G.S Abhaypal
  • 362
  • 1
  • 6
  • 17
  • 1
    Please [see why not to cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family in `C`. – Sourav Ghosh Aug 04 '15 at 10:18
  • 1
    Can you show the declaration of `iData[]`. My guess is that you wanna copy the string in `iData[i].name` to `(*Head)->Data`, but thats not how you copy strings. – Haris Aug 04 '15 at 10:21
  • what alternative can be used instead of `malloc()` ? – G.S Abhaypal Aug 04 '15 at 10:31

1 Answers1

1

The error message the type of struct member Data ( in (*Head)->Data=*(Data); is array and you are assigning an char value to it.

Since you pass name, what you most likely wanted to use is strcpy() there:

     strcpy((*Head)->Data, Data);

Or if you want to store only a single char then you need to change the type of Data in the structure to char from char[N].

Also, see what's wrong with casting malloc's return value?

P.P
  • 117,907
  • 20
  • 175
  • 238