Thread: Struct and malloc array

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    Struct and malloc array

    I want my code to be able to ask for user how many planets the user wants to add to the array of planets, it asks the user how many in the beginning and store it as "numberofinputs" then use that to be the array size using malloc.

    But it is giving me the error: request for member name( and dist, and decription) in something not in structure or union.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    typedef struct{
      char name[128];
      double dist;
      char description[1024];
    } planet_t;
    int numberofinputs;
    
    
    int main() {
    //creating the array
    printf("How many courses: ");
    scanf("%d", &numberofinputs);
    int *solarsys=malloc(numberofinputs*sizeof(int));
    
    
      int i;
      char name[128];
      printf("Enter planet name to add: ");
      scanf("%s",name);
      
      int have_planet = 0;
      if(!have_planet){
    
        for (i=0;i<numberofinputs; i++){
        strcpy(solarsys[i].name, name);
        printf("Enter distance and description: ");
        scanf(" %lf ", &solarsys[i].dist);
        gets(solarsys[i].description);
        }
      }
    
    
      for(i=0; i<9; i++){
        printf("%s %f %s\n",
               solarsys[i].name,
               solarsys[i].dist,
               solarsys[i].description);
      }
    
    
    }
    Last edited by Lina_inverse; 10-06-2012 at 06:55 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    solarsys is a pointer to int. It needs to be pointer to planet_t. The argument of malloc() also needs to be numberofinputs*sizeof(planet_t).

    C compilers are required to complain if you treat an int as if it was something else.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    What type is solarsys? It's not a planet_t.

    Also, how much space are you allocating for solarsys?

    Edit: drat, beat by a few seconds again.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    Thanks for the help, it worked but why did a warning about "gets" come out?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Warnings about gets() probably reflect the fact that gets() is unsafe (there is no way to prevent it from tromping memory if the user enters too many characters). Using gets() is considered bad practice, to the extent that it is deprecated in the latest C standard (i.e. scheduled for removal completely from C).

    Look up the fgets() function for a safer alternative.

    Quote Originally Posted by christop View Post
    Edit: drat, beat by a few seconds again.
    I wasn't aware there was a competition. But, if there is ... <evil chuckle>
    Last edited by grumpy; 10-06-2012 at 07:18 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Lina_inverse View Post
    Thanks for the help, it worked but why did a warning about "gets" come out?
    There is no way for gets to restrict the input length, that's the warning. Use fgets instead.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    so i have made some changes to the code, the user now can enter all the information in one single line. but when i try to print out the info the user entered, it only displays: 0.00000 for all the entries..

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    typedef struct{
      char name[128];
      double dist;
      char description[1024];
    } planet_t;
    int numberofinputs;
    
    int main() {
    //creating the array
    printf("How many courses: ");
    scanf("%d", &numberofinputs);
    planet_t *solarsys=malloc(numberofinputs*sizeof(planet_t));
    
      int i;
      char name[128];
      char description[1024];
      int dist;
    
      int have_planet = 0;
      if(!have_planet){
        for(i=0;i<numberofinputs;i++){
        printf("Enter the following: name distance discription: ");
        scanf("%s", name);
        scanf(" %lf ", &solarsys[numberofinputs].dist);
        gets(solarsys[numberofinputs].description);
      }}
    
      for(i=0; i<numberofinputs; i++){
        printf("%s %lf %s\n",
               solarsys[i].name,
               solarsys[i].dist,
               solarsys[i].description);
      }
    
    }
    


    anyone see how to fix it?

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You are iterating from 0 to numberofinputs, 'i' is the current index. But you always use numberofinputs as index instead of i with gets. Also use fgets.

    Code:
    fgets(solarsys[i].description, 1024, stdin);
    But preferably, define the max length as a constant instead of a magic number and use it everywhere.

    Code:
    #define DESCRIPTIONLENGTH 1024;
    fgets(solarsys[i].description, DESCRIPTIONLENGTH, stdin);
    Last edited by Subsonics; 10-06-2012 at 07:41 PM.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    thanks for the fgets info. But Im still not too sure how to fix the printing out information.. [i] should be used since im printing solarsys[1], solarsys[2],.. and on. right?

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    it just prints out 0's
    Struct and malloc array-1-jpg

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    edit: o i see what u mean with the [i].. thanks!
    Last edited by Lina_inverse; 10-06-2012 at 08:02 PM.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    There are some other issues there. You don't need your local variables apart from int i, you can scan directly to your struct. Also has_planet is always 0 so the test if(!has_planet) will always be true.

    Code:
        int i;
    
        for(i=0;i<numberofinputs;i++) {
            printf("Enter the following: name distance discription: ");
            scanf("%s", solarsys[i].name);
            scanf(" %lf ", &solarsys[i].dist);
            fgets(solarsys[i].description, 1024, stdin);
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct and Malloc Help
    By VinrVanyali in forum C Programming
    Replies: 9
    Last Post: 11-14-2011, 11:36 PM
  2. Malloc the struct
    By farukyaz in forum C Programming
    Replies: 4
    Last Post: 10-17-2011, 04:24 AM
  3. malloc struct node array
    By p595285902 in forum C Programming
    Replies: 5
    Last Post: 03-23-2011, 09:26 AM
  4. relationship between struct array & malloc
    By baraq in forum C Programming
    Replies: 5
    Last Post: 11-07-2010, 11:20 PM
  5. Struct array malloc and realloc
    By pseudonoma in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 09:16 PM