【发布时间】:2014-09-03 10:02:44
【问题描述】:
我目前正在使用 borland C 进行编码,但遇到了结构取消引用的问题。 当前->值= x;给出了 Lvalue required 错误。当“值”为字符时,这不会发生。无论如何将 x 的值分配给 current->value?
#include<stdio.h>
#include<conio.h>
char x[16];
FILE *fin;
struct node {
char value[16];
struct node *next,*prev;
};
struct node *current;
void main(){
fin = fopen("tokens.ctr","r");
current = (struct node*) malloc(sizeof(struct node));
fscanf(fin,"%s",&x);
current->value = x;
}
【问题讨论】:
-
current->value = x;current->value 是一个数组。您不能 assign 给数组。您只能逐个元素地复制,或者使用 strcpy() 或 memcpy()。另外:conio.h>是非标准标头,main()应该返回 int,而不是 void。 -
使用
strcpy,而不是赋值。另请注意,current是一个野指针。 -
你不能像这样给数组赋值,使用
for循环单独赋值或者使用memcpy。 -
你应该使用
strcpy。 -
试过 memcpy 和 strcpy 都成功了。谢谢!
标签: c dereference