【发布时间】:2016-06-26 13:35:54
【问题描述】:
我在使用 char 数组时遇到问题。
我的想法是使用scanf 将输入存储在char 数组中,然后将该数组的内容存储在struct 中。
下面的代码可能更容易理解:
struct ListaCategoria {
int ident;
char data[MAX];
struct ListaCategoria* next;
};
struct ListaCategoria* headCat;
void inserirCat(){
int x;
char arr[MAX];
printf("Identificacao : ");
scanf("%d", &x);
printf("Designacao : ");
scanf("%c[^\n]", &arr);
struct ListaCategoria* temp = (struct ListaCategoria*) malloc(sizeof(struct ListaCategoria));
(*temp).ident = x;
(*temp).data = arr; //this is the line that's giving some trouble can someone explain me why?
}
【问题讨论】:
-
scanf("%c[^\n]", &arr);将在 C 中调用 未定义的行为,因为在需要char*的地方传递了char(*)[MAX]。 -
你的意思是“objective C”开发iOS应用常用的语言吧?
-
temp->data和temp->ident会更短