I understand the creation of the array (int** intptrArray[5]) and the rand() number part. However, I don't understand the sentence in the middle - I know I need to allocate the array first and then allocate each pointer in the array to an int but I'm not sure how to go about it and I've been stuck for hours on this. Do I need to use malloc here and how would I do that?
It seems you need to declare in the file scope a variable of the pointer type int ** like
int **intptrArray;
then in a function (for example in main
) you need to allocate an array of 5
elements of the type int *
like
intptrArray = malloc( 5 * sizeof( int * ) );
Then in a loop each element of the array should be initialized by a pointer to dynamically allocate memory for one object of the type int
.
And then using the function rand
you need to assign values to these integers.
For example
for ( size_t i = 0; i < 5; i++ )
{
intptrArray[i] = malloc( sizeof( int ) );
*intptrArray[i] = rand();
}