我想为我的 char *
分配一个值,如下所示:
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);
}
键值是 char[]
,数据值是 int
。
我收到以下错误:
Error: invalid operands to binary + (have ‘int’ and ‘char *’) text = ptr->key + ";" + ptr->data + ";\n";
有谁知道如何解决这个问题吗?
最佳答案
作为 ptr->key
、";"
、ptr->data
、";\n"的串联
不需要在循环后存在,只需将其打印到文件中即可。 @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;
}
关于c - 在 C 中为 char 数组赋值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39986426/