Thread: Assigning values to struct strings

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Assigning values to struct strings

    i have written a code it is my college assignment for tomorrow, the program runs ok but it just wont work

    the code is a long one 400... lines ill just take the neccessary parts if needed i can upload the full code

    my struct :
    Code:
    // the struct which defines each cars properties.
    struct car{
           char *plate;
           char *brand;
           char *model;
           char *color;
           char *status;
           int productionyear;
           int price;
    };// Struct ends
    the part where i call the mistaken function

    Code:
    Car gallery[20];
      // we initialize the cars before everything else
      initialize(gallery);
      
      // the main loop
      while(selection2 == 1)
      {
      system("CLS");// clears the screen incase of previous restarts
      // Welcome Prints/Main Loop
      printf("Welcome to my gallery please make your selection : \n\n");
      printf("\n1 - Add a new car\n2 - Update an information of a selected car\n3 - Rent a Car\n4 - Taking back a car\n5 - Searching an available car\n\nEnter your selection : ");
      //Selection of main loop
      scanf("%d",&selection);
      
      if(selection == 1){
                   carcounter++;// when we add a new car we make sure the size of the loop there                                                    (check addcar())
                   addcar(gallery,carcounter)
    and the whole function that causes my error

    Code:
    void addcar(Car *aCar,int i){
        // the string variables in order for scanf to read the string properly with also a size of 20
        char *plateA[20];
        char brandA[20];
        char modelA[20];
        char colorA[20];
        char statusA[20];
        // end of variables
        // read plateA and assign to struct
        printf("Enter plate of the car : ");
        scanf("%s",&plateA);
        aCar[i].plate = plateA;
        // end plate sscan
        // read brandA and assign to struct
        printf("Enter the brand of the car : ");
        scanf("%s",&brandA);
        aCar[i].brand = brandA;
        // end brand scan
        // read modelA and assign to struct
        printf("Enter the model of the car : ");
        scanf("%s",modelA);
        aCar[i].model = modelA;
        // end model scan
        // read colorA and assign to struct
        printf("Enter the color of the car : ");
        scanf("%s",colorA);
        aCar[i].color = colorA;
        // end color scan
        // read statusA and assign to struct
        printf("Enter the status of the car : ");
        scanf("%s",statusA);
        aCar[i].status = statusA;
        // end status scan
        // start production year scan not neccesarily need a local variable as it wont have any
        // difference from the variable in our struct
        printf("Enter the production year of the car : ");
        scanf("%d",&aCar[i].productionyear);
        // end production year scan
        // start price scan
        printf("Enter the price of the car : ");
        scanf("%d",&aCar[i].price);
        // nice success message
        printf("Car has been added : \n");
        // briefing of the car added
        printf("%s , %s , %s , %s , %s , %d , %d",aCar[i].plate,aCar[i].brand,aCar[i].model,aCar[i].color,aCar[i].status,aCar[i].productionyear,aCar[i].price);
    }
    i can even attach the zip of the whole project if anyone is willing to help

    thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The struct as you've defined it doesn't allocate any memory for those strings. Just pointers.
    Plus you can't assign a string with '='. You must use strcpy().

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    Quote Originally Posted by nonoob View Post
    The struct as you've defined it doesn't allocate any memory for those strings. Just pointers.
    Plus you can't assign a string with '='. You must use strcpy().
    thank you for your reply, i have understood strcpy part but for the memory thing what do you suggest me to do
    because when i do it like this lets say char plate[20]

    Code:
    char plate[20]
    still doesnt work maybe i went wrong somewhere else

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    That's right. All the struct elements need to be char arrays. Then you do this:
    Code:
    scanf(" %s", modelA);
    strcpy(aCar[i].model, modelA);
    I would suggest put a space in the scanf format specifier like I did to soak up any new-lines.
    Also you have extra '&' in front of plateA, brandA.
    The declaration char *plateA[20]; is wrong. Get rid of '*'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning values to struct array or linked list problem?
    By kalamram123 in forum C Programming
    Replies: 1
    Last Post: 04-07-2011, 12:38 PM
  2. Replies: 1
    Last Post: 05-31-2009, 04:02 PM
  3. Re-Assigning Values To Array
    By Windu102 in forum C Programming
    Replies: 5
    Last Post: 08-21-2008, 09:57 AM
  4. assigning values
    By alex_6169 in forum C Programming
    Replies: 7
    Last Post: 05-15-2006, 02:16 AM
  5. Assigning pictures to hex values
    By firemaker in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2005, 10:43 AM