I'm trying to write a function in C, which returns the pointer to an array. Each element of this array is a pointer to the initialized structure, and in each structure there's also a pointer to an array.
struct queueRecord;
typedef struct queueRecord* queue;
queue createQueue(int maxSize){
queue newQueue = malloc(sizeof(struct queueRecord));
newQueue->array = malloc(sizeof(int)*maxSize);
newQueue->capacity=maxSize;
newQueue->front=0;
newQueue->rear=0;
return newQueue;
}
This just works fine as well as in the following function:
queue* initQArr(int maxSize){
queue arr[10];
for (int i=0; i<10; i++) {
arr[i]=createQueue(maxSize);
}
return arr;
}
when I pass the parameter 10 in it and set a breakpoint at return, everything seems good (excuse me I can't post images, the debug information is like following):
arr(queue[10])
[0]=(queue)0x100105480
capacity=(int)10
size=(int)0
front=(int)0
rear=(int)0
array=(int*)0x1001054a0
[1]=(queue)0x1001054d0
...
...
[9]=(queue)0x100105760
However!When I invoke this function and return to a new array, I got an array with only 5 elements:
queue *digitsQArr = initQArr(length);
int digit = 0;
(debug information as following):
digitQArr = (queue*)0x7fff5fbff6d0
*digitsArr=(queue)0x100105480
capacity=(int)10
size=(int)0
front=(int)0
rear=(int)0
array=(int*)0x1001054a0
[1]=(queue)0x1001054d0
...
...
[4]=(queue)0x1001055d0