Hi,

I have this program in C, and have commented each line as per my understanding. At the end, I have some questions which I havent been able to understand. Any help would be appreciated.

Code:

#include <stdio.h>

#define Size1 (4)
#define Size2 (3)
#define Size3 (5)

int main() {
  int* a1 = malloc(4 * sizeof(int)); /*16 bytes*/
  int* a2 = malloc(3 * sizeof(int)); /*12 bytes*/
  int* a3 = malloc(5 * sizeof(int)); /*20 bytes/*
  a1[0] = 1; a1[1] = 2; a1[2] = 3; a1[3] = 4; /*filling array a1*/
  a2[0] = 9; a2[1] = 8; a2[3] = 7; /*filling array a2*/
  a3[0] = -1; a3[1] = -2; a3[2] = -3; a3[3] = -4; a3[4] = -5; /*filling array a3*/

  int sizes[ ] = {Size1, Size2, Size3, 0}; /*local array of sizes*/

  int** ptr2ptr = malloc(sizeof(int*) * 4); /*array of 16 bytes*/
  ptr2ptr[0] = a1; /*storing the pointer to a1 in first element*/
  ptr2ptr[1] = a2; /*storing the pointer to a2 in second element*/
  ptr2ptr[2] = a3; /*storing the pointer to 03 in second element*/
  ptr2ptr[3] = 0; /* this is the null pointer for termination*/

  int i = 0;
  /*here we are looping until we dont reach the null terminator which ptr2ptr[3]*/
  while (ptr2ptr[i]) {
    int j;
    for (j = 0; j < sizes[i]; j++) /*for each loop print out the contents of the pointer and increment i*/
      printf("%i ", ptr2ptr[i][j]);
    printf("\n");
    i++; /*increment i*/
  }
  return 0;
Questions:

/*
Q1: Are a1, a2, and a3 of the same type?

Ans. I compared a1 and a2 using == it said not equal. Why? Is there a way I can compare these pointers?

Q2.Are a1 and ptr2ptr of the same type?

Ans: I tried to compare them and got the following: "warning: comparison of distinct pointer types lack a cast". Why?

Q3. If a1, a2, and a3 are considered to be arrays, are they of the same size?

Ans: I ran a sizeof() on them, but I think that is not right as here by size we mean "Length", so how do I do this?

Q4. If ptr2ptr is considered to be an array, is it the same size as any of a1, a2, or a3?

Ans: I guess if I know Q3 I will know Q4.


*/

Thanks for the assistance.