Hi,
I can't figure out what is wrong with the code after trying to fix it for the last six hours i decided to give, please i need help =( big time
I'm trying to make a dynamic array of structures then printing the output of the elements but I haven't successed in either storing the elements of the structure in the array nor printing it
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 128
typedef char String[MAX];
typedef struct
{
String name;
float s;
} ST;
int main()
{
int size, Index;
ST *p, *tmpa, *tmp;
ST *dArray;
//create block
//check return value of malloc
tmpa= (ST *) malloc (1*sizeof(ST));
if(tmpa==NULL)
{
printf("Error\n");
exit(1);
}
else
{p=tmpa;}
size=-1;
printf("Enter information (\"exit\") to exit\n");
printf("What is the name :");
scanf("%s",(*p).name);
if(strcmp((*p).name,"exit")==0)
{ exit(0); }
while(strcmp((*p).name,"exit")!=0)
{
printf("What is the size:");
scanf("%f",&(*p).s);
size++;
(p[size]).name=(*p).name;
p[size].s=(*p).s;
tmp=(ST *)realloc(p,(size+1)*sizeof(ST));
//check return value from tmp
if (tmp==NULL)
{
printf("Error\n");
exit(1);
}
else
{p=tmp;}
printf("Enter information (\"exit\") to exit\n");
printf("What is the name :");
scanf("%s",(*p).name);
}//end of while loop
printf("The following new data:\n");
for (Index = 0; Index <= size; Index++)
{
printf("%s has size %.2f \n",p[Index].name,p[Index].s);
}
free(p);
return 0;
}