c - 在 C 中为 char 数组赋值不起作用

标签 c arrays

我想为我的 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/

相关文章:

c - 在 C 中通过 mpi 发送已知大小的字符串

c - C中打印字符数组错误

ios - 错误 : generic parameter 'T' could not be inferred in swift

ios - 如何在 NSUserDefaults 中设置数组的数组?

c - c中的scanf好像需要双输入?

c - MPI:程序工作取决于进程数

C 初始化矩阵

c++ - 如何从 std::array<T, N>::pointer 成员/依赖类型中推断出数组大小?

java - 如何在 Java 中从数组 (int[]) 创建 ArrayList (ArrayList<Integer>)

c - 将十六进制字符串转换为 ASCII 字符串的 GLib API?