0

我正在编写一个程序,让用户从库存中添加或删除汽车。现在,我试图让用户选择汽车名称而不是明确说明名称,我创建了一个结构 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("\n\n\n1.Toyota \n2.Honda \n3.Hyundai \n4.Nissan \n5. Mitsubishi \n6. Volksvagon \n7. Acura \n8. Ford \n9. Dodge \n10. GNC   Exit\n");
    scanf("%d", &ch);
    strcpy(carname,criteriaselector.companyList[ch-1]);


    printf("\n\nEnter the car model: ");
    scanf("%s", carmodel);
    printf("\n\nEnter the car year: ");
    scanf("%s", caryear);
    printf("\n\nEnter the car type: ");
    scanf("%s", cartype);
    printf("\n\nEnter 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("\n\nLinked 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  Quantity\n");
        while(ptr != NULL){
            printf("\n%d.\t%s   %s   %s   %s   %d\n", 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("\n\n\n1. Insert \n2. Delete \n3. Display \n4. Exit\n");
        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("\n\nInvalid choice. Please try again. \n");

        }

    } while(1);


    return EXIT_SUCCESS;
}
4

2 回答 2

1

您不能将结构的定义与初始化混合。

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] = '\0' // changing a character inside a const string

但是,你可以这样做:

criteriaselector[1] = "some other string".

这可能不是你的意思。相反,我们可以说“定义一个指向常量字符串的常量指针数组”,如下所示:

const char * const criteriaselector[10] = {"Toyota", "Honda" ... }

这样,您将无法更改字符串的内容或指向的字符串。

我链接的关于该问题的评论更详细地解释了这一点。

于 2013-08-07T01:40:27.580 回答
0

您不能初始化结构内的变量。这是因为结构定义了类型而不是变量。criteriaselector您可以使用结构变量(在您的情况下)和./或->访问说明符在结构定义之外初始化结构成员。例如,

criteriaselector.companyList[0] = "福特";

于 2013-08-07T01:46:06.907 回答