Thread: allocating space dynamically for an array of structures

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    allocating space dynamically for an array of structures

    Hi all I have been given the task of using malloc to allocate space dynamically for a structure of arrays, that must have the integer elements execution_time and process_number and a float element called average_time. Set up an array of structures and show how to access individual elements of the structures within the array. Then finally, use malloc to allocate space dynamically for a structure of the type structure exec_time. I think I am nearly there but I am receiving some errors.

    Which are:
    Question1.c: In function ‘main’:
    Question1.c:21: error: incompatible types in assignment

    Code:
             #include <stdlib.h>
             #include <stdio.h>
             typedef struct exec_time
        {
            int et;                
            int pn;                
            float at;            
        }time; 
    
          int qty, qty2, qty3, i;
         int getnumber();
         void printnumber();
         time pts[4] = {{4,8,0.75}, {5,10,0.00457}, {27,75,3.4}}; 
    
    int main()
    {
            time *numbers;
            getnumber();
            numbers = malloc( qty * sizeof(int) ); 
            if(numbers != NULL) 
    {
                   for(i=0; i<qty ; i++) numbers[i] = i+1;
                   for(i=0; i<qty ; i++) 
                   printf("%d ", numbers[i]);
                   printf("\n");
                           free(numbers); /*free allocated memory*/
                           printnumber();
                           return 0;
    }
               else {
                        printf("\nMemory allocation failed - not enough memory.\n");
                        return 1;}
    }
    
    
    int getnumber()
    {
         printf("\nHow many ints would you like to store for et? ");
         scanf("%d", &qty);
         printf("\nHow many ints would you like to store for pn? ");
          scanf("%d", &qty2);
         printf("\nHow many floats would you like to store for at? ");
         scanf("%d", &qty3);
                    qty = qty + qty2 + qty3; /*adds to total number of integers to be used with malloc and sizeof*/
                    return qty;
    }
    
    void printnumber() /*simply prints the value of the structure*/
    {
          printf("\n\nCode et= execution time, pn =process number, at = average time\n");
          printf("Variable 'time' at element 0 et: %d pn: %d at: %f\n",pts[0].et, pts[0].pn, pts[0].at);
          printf("Variable 'time' at element 1 et: %d pn: %d at: %f\n",pts[1].et, pts[1].pn, pts[1].at);
          printf("Variable 'time' at element 2 et: %d pn: %d at: %f at:\n\n",pts[2].et, pts[2].pn, pts[2].at);
    }
    Last edited by cus; 01-08-2009 at 09:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2008, 10:25 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. dynamically allocating a 2dim array
    By agerealm in forum C++ Programming
    Replies: 14
    Last Post: 03-10-2004, 02:40 PM
  4. 2d array of structures
    By spudtheimpaler in forum C Programming
    Replies: 2
    Last Post: 03-01-2004, 03:17 PM
  5. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 03:59 PM