0

I'd like to store the pointers of two arrays (*a and *b) created by malloc in a function.

For example, if &a[0]=0x0061FEC8 and &b[0]=0x007700FE, then the array to store these two pointers should be c[0]=0x0061FEC8 and c[1]=0x007700FE

void storePointers(int **ptrArray){
    // create two arrays a and b by malloc
    int *a = (int *)malloc(5*sizeof(int));  // a stores 5 integers
    int *b = (int *)malloc(10*sizeof(int)); // b stores 10 integers

    // create an array to store the pointers of a and b
    *ptrArray = (int **)malloc(2*sizeof(int*));
    (*ptrArray)[0] = a;
    (*ptrArray)[1] = b;
}

int main(){
    int *mArray = NULL;
    
    storePointers(&mArray);

    // these two lines should print 0x0061FEC8 and 0x007700FE
    printf("mArray[0]: %p\n", mArray[0]);
    printf("mArray[1]: %p\n", mArray[1]);
    
    return 0;
}

This program actually worked. But the compiler displayed a warning message:

warning: assignment to 'int' from 'int *' makes integer from pointer without a cast [-Wint-conversion]
     (*ptrArray)[0] = a;
warning: assignment to 'int' from 'int *' makes integer from pointer without a cast [-Wint-conversion]
     (*ptrArray)[1] = b;

I assume int is common so the compiler fixed the problem by itself so that my program ran properly? I have another similar program, but it uses struct. So instead of a warning, I get an error of

Error: incompatible types when assigning to type 'myStruct' from type 'myStruct *'

I would like to know the root cause and solution to get rid of the warning and ultimately the error in my struct program.

1
  • you have one less level of indirection than you need in several places Commented Oct 31, 2020 at 6:03

1 Answer 1

1

If an array is int * then an array of arrays is int ** and if you want to return an array of arrays as an out parameter, then you need a pointer to that -- int ***. So you need to change the type of mArray as well as the ptrArray parameter:

void storePointers(int ***ptrArray){
    // create two arrays a and b by malloc
    int *a = (int *)malloc(5*sizeof(int));  // a stores 5 integers
    int *b = (int *)malloc(10*sizeof(int)); // b stores 10 integers

    // create an array to store the pointers of a and b
    *ptrArray = (int **)malloc(2*sizeof(int*));
    (*ptrArray)[0] = a;
    (*ptrArray)[1] = b;
}

int main(){
    int **mArray = NULL;
    
    storePointers(&mArray);

    // these two lines should print 0x0061FEC8 and 0x007700FE
    printf("mArray[0]: %p\n", mArray[0]);
    printf("mArray[1]: %p\n", mArray[1]);
    
    return 0;
}

That should then work if you change the type from int to something else.

Sign up to request clarification or add additional context in comments.

1 Comment

another option is to use the return value of the funtion for returning a value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.