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);
}