Hi. I'm working with dynamic arrays to construct a histogram. I've got an array of pointers to my histogram struct and I try to malloc() and then realloc() the array as necessary. Here's some of the code:
Code:
typedef struct{
double val;
int freq;
}hPoint;
...
...
...
hPoint **momentBin;
momentBin = (void *)malloc(sizeof(hPoint *) * 10);int maxIndex =10;
...
found=0;
for(k=0;k<maxIndex;k++)
{
if(momentBin[k]==NULL)
break;
test =(momentBin[k])->val;
if (test==bN)
{
found =1;
(momentBin[k])->freq++;
break;
};
}
if(found ==0)
{
probCounter++;
//increase the size of the array
if (probCounter>=maxIndex)
{
hPoint **newArr = realloc(momentBin, maxIndex*2);
if (newArr==NULL)
{
printf("Reallocation Error");exit(0);
}
else momentBin = newArr;
};
hPoint *temp =(hPoint *) malloc(sizeof(hPoint));
temp->val=bN;
temp->freq=1;
momentBin[probCounter] = temp;
This would be great, except when I loop through my array, there aren't any structs. Your help is greatly appreciated.
Thanks