Day#48 'C' Coding Challenge

Day#48 'C' Coding Challenge

Predict the Correct answer for the code below:

#include<stdio.h>
int main()
{
  int arr[][3]={1,2,3,4,5,6};
  int(*ptr)[3]=arr;
  printf("%d %d\n",(*ptr)[1],(*ptr)[2]);
  ++ptr;
  printf("%d %d\n",(*ptr)[1],(*ptr)[2]);
  return 0;

-----------------------------------------------------------

Explanation:

ptr is Pointer to an Array of 3 Integers
arr is an 2D Array of 2 Rows and 3 Columns
Initially pointer ptr is pointing to First Row....So (*ptr)[1] and (*ptr)[2] gives 2,3
After Incrementing the Pointer....It will point to the Second Row
Therefore (*ptr)[1] and (*ptr)[2] will give 5,6

Post a Comment

0 Comments