简体   繁体   中英

How do I copy a value of array of strings to char array?

I am writing a program that will let user add or delete the car from inventory.Right now, I am trying to get user to choose a car name instead of explicitly stating name and I created a struct CriteriaSelector in which array name companyList will store value of companies and when I let user choose car name it will copy a string from companyList at a particular index into the char array called carname which will then be copied into carname char array in the CarData object.

The thing is whenever I compile the below code, I get the error saying expected specifier qualifier list before 'companyList' at line 27 and I don't know what to do? Can anybody help?

#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;
}

You can't mix definitions of a struct with initialisation.

typedef struct criteriaselector{

   const char *companyList[10];
   companyList[0] = "Toyota"; // This bit needs to be elsewhere.

Instead, after you declare an instance of the struct, you'll then need to fill in the values. For example:

typedef struct criteriaselector{
   const char *companyList[10];
}
.....

void some_function() {
    CriteriaSelector criteriaselector;
    criteriaselector.companyList[0] = "Toyota"
     .... etc

However, it looks like you were intending to make a constant, rather than describe a structure. Instead of the whole struct definition, you can do:

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

somewhere in global scope. This will declare a "global" array of constant strings.


As an aside, there's a little subtlety with the way that const works here. This declaration says "define an array of pointers to constant chars". This means that you can't change the strings themselves, but you can change the pointers. So, you can't do:

criteriaselector[1][0] = '\0' // changing a character inside a const string

But, you can do:

criteriaselector[1] = "some other string".

This probably isn't what you meant. Instead, we can say " define an array of constant pointers to constant strings ", like this:

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

That way, you won't be able to change the contents of the string, or which strings are pointed to.

The comments on that question that I linked explain this in a bit more detail.

You cannot initialize variables inside a structure. This is because a struct defines a type and not a variable. You can initialize a structure members outside the structure definition using a structure variable ( criteriaselector in your case) and . or -> access specifiers. For eg.,

criteriaselector.companyList[0] = "Ford";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM