Thread: HELP with structs that contain arrays

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    1

    HELP with structs that contain arrays

    new to this forum sorry if i did anything wrong just looking for some help.

    im writing a program that reads in from a file a certain number of products and a certain number of months that those products were sold for.....ive created the following struct

    Code:
    
    typedef struct{
          int ID;
          char *name;
          float *unit_prices;
          int *units_sold;
    }product;
    
    void read_data(product info[], int *Num_Products, int *Months);
     /*this reads in the data from the file and assigns Num_Products and months 
    its value which then dynamically creates space to hold the rest of the information from the file*/
    
    
    int main(void)
    {
       int Num_Products;   
       int Months;
     
       product *info;
    
       info = (product *) malloc(sizeof(product) * Num_Products);
    
       info->unit_prices = (float*) malloc(sizeof(float) * Months);
       /*this is were my problem is.....i figured this would create space         
      for months inside each struct that i create but i realized its only       
      creating space for unit_prices inside struct info[0] not the other  
      structs created thats when i tried to use a for statement*/
    
       for(index = 0; index < Num_Products;  index++)
       {
            for(index2 = 0; index2 < Months; index2++)
            { 
                  (info + index) -> unit_prices[index2] = (float*)  
                      malloc(sizeof(float) * Months);
            }
        }
    any hints or help on how i should approach this is greatly appricated thanks
    Last edited by Ken Fitlike; 06-08-2006 at 05:20 PM. Reason: reduced horizontal extent of code comment

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Months is a pointer. You need to be dereferencing it, or simply don't use a pointer.
    Code:
    struct foo
    {
        size_t s;
        int *someints;
    };
    
    ...
    
    struct foo *f;
    size_t x;
    
    f = malloc( sizeof *f ); /* allocate one structure... */
    f->s = 10; /* We want 10 ints... */
    f->someints = malloc( sizeof( *(f->someints) ) * f->s ); /* Allocate space for 10 ints... */
    
    for( x = 0; x < f->size; x++ ) /* For each int... */
         f->someints[ x ] = (x + 1) * (x + 1); /* Fill it with something... */
    That looks about right.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocatable arrays in CLASSes and STRUCTS
    By simone.marras in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2009, 10:50 AM
  2. Replies: 7
    Last Post: 06-04-2008, 10:39 PM
  3. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  4. arrays in structs
    By *DEAD* in forum C Programming
    Replies: 3
    Last Post: 11-25-2006, 07:35 AM
  5. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 07:38 AM