mafioso mafioso - 1 year ago 81
C Question

Assigning value to char array in C doesn't work

I want to assign a value to my

char *
like that:

char *text;
struct node *ptr = head;

//start from the beginning
while(ptr != NULL)
text = ptr->key + ";" + ptr->data + ";\n";
ptr = ptr->next;
fprinft(f, text);

The key value is a
and the data value an

I get the following error:

Error: invalid operands to binary + (have ‘int’ and ‘char *’) text = ptr->key + ";" + ptr->data + ";\n";

Does anyone know how to fix that problem?

Answer Source

As the concatenation of ptr->key, ";", ptr->data, ";\n" does not need to exist after the loop, just print it to the file. @user3386109

// Not needed
// char *text;

struct node *ptr = head;

while(ptr != NULL) {
   //           v--v----- printf specifiers for a pointer to a string and int 
   fprinft(f, "%s;%d;\n", ptr->key, ptr->data);
   ptr = ptr->next;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download