简体   繁体   中英

Double pointer assigning char array value to char array, of structure using char pointer

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);

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM