我正在编写一个程序,让用户从库存中添加或删除汽车。马上我试图让用户选择一个汽车名称,而不是显式地声明名称,我创建了一个结构CriteriaSelector,其中数组名称companyList将存储公司的值,当我让用户选择汽车名称时,它将在特定索引处从companyList复制一个字符串到名为carname的char数组中,然后将其复制到CarData对象中的carname char数组中。
问题是,每当我编译下面的代码时,我都会在第27行的"companyList"之前出现错误,说预期的说明符限定符列表,我不知道该怎么办?有人能帮忙吗?
#include <stdio.h>
#include <stdlib.h>
#define MAX_WORD_LENGTH 20
typedef struct cardata{
char carname[MAX_WORD_LENGTH];
char carmodel[MAX_WORD_LENGTH];
char caryear[MAX_WORD_LENGTH];
char cartype[MAX_WORD_LENGTH];
int quantity;
}CarData;
struct node{
CarData data;
struct node *next;
struct node *prev;
}*start=NULL;
typedef struct criteriaselector{
const char *companyList[10];
companyList[0] = "Toyota"; <-- This is where error is happening!!(Line 27)
companyList[1] = "Honda";
companyList[2] = "Hyundai";
companyList[3] = "Nissan";
companyList[4] = "Mitsubishi";
companyList[5] = "Volksvagon";
companyList[6] = "Acura";
companyList[7] = "Ford";
companyList[8] = "Dodge"
companyList[9] = "GMC";
}CriteriaSelector;
void insert_first(){
struct node *ptr;
CriteriaSelector criteriaselector;
char carname[MAX_WORD_LENGTH];
char carmodel[MAX_WORD_LENGTH];
char caryear[MAX_WORD_LENGTH];
char cartype[MAX_WORD_LENGTH];
int carQuantity;
int ch;
printf("nChoose your car");
printf("nnn1.Toyota n2.Honda n3.Hyundai n4.Nissan n5. Mitsubishi n6. Volksvagon n7. Acura n8. Ford n9. Dodge n10. GNC Exitn");
scanf("%d", &ch);
strcpy(carname,criteriaselector.companyList[ch-1]);
printf("nnEnter the car model: ");
scanf("%s", carmodel);
printf("nnEnter the car year: ");
scanf("%s", caryear);
printf("nnEnter the car type: ");
scanf("%s", cartype);
printf("nnEnter the quantity of models: ");
scanf("%d", &carQuantity);
if(start==NULL){
start=(struct node *)malloc(sizeof(struct node));
strcpy(start->data.carname,carname);
strcpy(start->data.carmodel,carmodel);
strcpy(start->data.caryear,caryear);
strcpy(start->data.cartype,cartype);
start->data.quantity=carQuantity;
start->prev=NULL;
start->next=NULL;
}else{
ptr=start;
start=(struct node *)malloc(sizeof(struct node));
strcpy(start->data.carname,carname);
strcpy(start->data.carmodel,carmodel);
strcpy(start->data.caryear,caryear);
strcpy(start->data.cartype,cartype);
start->data.quantity=carQuantity;
start->next=ptr;
}
}
void delete_first(){
struct node *ptr;
char carname[MAX_WORD_LENGTH];
char carmodel[MAX_WORD_LENGTH];
char caryear[MAX_WORD_LENGTH];
char cartype[MAX_WORD_LENGTH];
char modelNumber[MAX_WORD_LENGTH];
int carQuantity;
if(start==NULL){
printf("nnLinked list is empty.n");
}else{
ptr=start;
printf("nThe car for which the entry is removed is %s n",ptr->data.carname);
strcpy(start->data.carname,carname);
strcpy(start->data.carmodel,carmodel);
strcpy(start->data.caryear,caryear);
strcpy(start->data.cartype,cartype);
start->data.quantity=carQuantity;
start=start->next;
free(ptr);
}
}
void display()
{
struct node *ptr=start;
int i=1;
if(ptr == NULL){
printf("nLinklist is empty.n");
}else{
printf("nSr. No Make Model Year Type Quantityn");
while(ptr != NULL){
printf("n%d.t%s %s %s %s %dn", i,ptr->data.carname,ptr->data.carmodel,ptr->data.caryear,ptr->data.cartype,ptr->data.quantity);
ptr = ptr->next;
i++;
}
}
}
int main(void)
{
int ch;
do
{
printf("nnn1. Insert n2. Delete n3. Display n4. Exitn");
printf("nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_first();
break;
case 2:
delete_first();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("nnInvalid choice. Please try again. n");
}
} while(1);
return EXIT_SUCCESS;
}
不能将结构的定义与初始化混合使用。
typedef struct criteriaselector{
const char *companyList[10];
companyList[0] = "Toyota"; // This bit needs to be elsewhere.
相反,在声明结构的实例后,您将需要填充值。例如:
typedef struct criteriaselector{
const char *companyList[10];
}
.....
void some_function() {
CriteriaSelector criteriaselector;
criteriaselector.companyList[0] = "Toyota"
.... etc
然而,看起来你打算做一个常数,而不是描述一个结构。您可以执行以下操作,而不是整个结构定义:
const char *criteriaselector[10] = {"Toyota", "Honda" ... }
在全球范围内的某个地方。这将声明一个常量字符串的"全局"数组。
顺便说一句,const
在这里的工作方式有点微妙。这个声明说"定义一个指向常量字符的指针数组"。这意味着您不能更改字符串本身,但可以更改指针。所以,你不能做:
criteriaselector[1][0] = ' ' // changing a character inside a const string
但是,你可以做到:
criteriaselector[1] = "some other string".
这可能不是你的意思。相反,我们可以说"定义一个指向常量字符串的常量指针数组",如下所示:
const char * const criteriaselector[10] = {"Toyota", "Honda" ... }
这样,您就无法更改字符串的内容,也无法更改指向的字符串
我联系到的关于这个问题的评论更详细地解释了这一点。
不能初始化结构中的变量。这是因为结构定义的是类型而不是变量。您可以使用结构变量(在您的情况下为criteriaselector
)和.
或->
访问说明符在结构定义之外初始化结构成员。例如,
criteriaselecter.companyList[0]="福特";