Thread: Struct and malloc array

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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