Beginner question: Malloc on array of pointers to structs

H

hal

Hi,

I'm trying to make an array of pointers to 'TwoCounts' structs, where
the size of the array is arraySize. Right now I'm just mallocing
enough space for all the pointers to the structs, and mallocing space
for the pointer 'countPtr' in each struct, but do I need to do
anything else? Thanks.

typedef struct TwoCounts {
int *countPtr;
int count;
} TwoCounts;

int j;
TwoCounts *twoCountsArray;
TwoCounts *tempPtr;

twoCountsArray = (TwoCounts *) malloc(arraySize * sizeof(TwoCounts
*));
for (j=0;j<arraySize;j++){
//get location of each pointer in the array
tempPtr = (twoCountsArray + arraySize * sizeof(TwoCounts *));
//initialize the countPtr
tempPtr->countPtr = (TwoCounts *) malloc (sizeof(int));
*(tempPtr->countPtr) = NULL;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top