Given a pointer integer array **ptr with size N, and an integer array *array with size (N+1)*N/2. Please use malloc function to allocate memory to **ptr and *array. The *array is an ascending sequence of number. Each pointer in array **ptr shall point to one element of *array. And the elements pointed by the **ptr are also ascending.
For example, when n = 5, the size of **ptr will be 5, and the size of *array will be 15. The first pointer of **ptr is *ptr[0] which points to array[0], and the second pointer of **ptr is *ptr[1] which points to array[1], and the third pointer of **ptr is *ptr[2] which points to array[3], and the third pointer of **ptr is *ptr[3] which points to array[6].
main.c
#include <stdio.h>
#include <stdlib.h>
#include "function.h"
int main() {
int *array;
int **ptr;
int N;
int offset;
scanf("%d %d",&N, &offset);
malloc_array(&array, (1+N)*N/2);
ptr = malloc_ptr(N);
int i;
for(i = 0; i < (1+N)*N/2; i++){
array[i] = i;
}
pointer_ptr_to_array(array,ptr,N);
for(i = 0; i < N; i++){
printf("%d\n",*(ptr[i]+(offset)));
}
free(ptr);
free(array);
return 0;
}
function.h
int** malloc_ptr(int array_size); void malloc_array(int **array, int array_size); void pointer_ptr_to_array(int *array, int **ptr,int N);
The first line is size of **ptr
The second line is offset
offset < size <10000
Print each pointer of **ptr + offset
Note that you need to print a newline character ‘\n’ after each number, that is, just one number will be shown in each line of the output.