Fix char formatting error - Linux system C program

Viewed 47

I am trying to get rid of the error in my program. I'm not sure what is wrong with the array.

Here is the sample code:

typedef struct listNoNodes
{
 char FirstName[50];
 char LastName[60];
 int ID;
 int age;
 struct listNoNodes* next;
} listNoNodes;


listNoNodes* listNodes(char FirstName[], char LastName[], int ID, int age,
listNoNodes* next)
{
 listNoNodes* tmp = (listNoNodes*)malloc(sizeof(listNoNodes));
 tmp->FirstName[50] = FirstName;
 tmp->LastName[60] = LastName;
 tmp->ID = ID;
 tmp->age = age;
 tmp->next = next;
 return tmp;
};

Here is the error

lab3.c: In function ‘listNodes’:
lab3.c:21:21: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   21 |  tmp->FirstName[50] = FirstName;
      |                     ^
lab3.c:22:20: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   22 |  tmp->LastName[60] = LastName;
1 Answers

These two lines are wrong:

tmp->FirstName[50] = FirstName;
tmp->LastName[60] = LastName;

You are trying to assign char* pointers to single char elements.

You need to copy the contents of the arrays instead, eg:

strncpy(tmp->FirstName, FirstName, 50);
strncpy(tmp->LastName, LastName, 60);
Related