1

Hi i was trying to pass 2d array of integer using single pointer.I came to know that i must typecast array before passing to the function

Can anyone please explain why we need to typecast before pass my code is below?

 #include <stdio.h>
  void print(int *arr, int m, int n)
 {
       int i, j;
       for (i = 0; i < m; i++)
       for (j = 0; j < n; j++)
       printf("%d ", *((arr+i*n) + j));
 }

   int main()
 {
      int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
      int m = 3, n = 3;
      print((int *)arr, m, n); // here why i need to typecast and pass?
      return 0;
 }
David Heffernan
  • 572,264
  • 40
  • 974
  • 1,389
Sumit Kumar
  • 25
  • 1
  • 7

3 Answers3

1

Because arr without the cast decays into int (*)[3] (pointer to a block of 3 ints) and your function is expecting int * (pointer to 1 int)..

David Ranieri
  • 36,077
  • 6
  • 44
  • 85
  • sorry what is int (*)[3] is it pointer to an array? please can u explain in detail ? plz even i am unable to figure out how my function is expecting int *? – Sumit Kumar Aug 14 '14 at 06:10
  • You can't pass an array by value to a function, it decays into a pointer, `int arr[3]` decays into `int *` and `int arr[][3]` decays into `int (*arr)[3]`, your function needs to know the size of the last dimension of your array, `arr + 1` = `arr` + 4 bytes when `arr` = `int *`, and `arr + 1` = `arr` + 12 bytes when `arr` = `int (*arr)[3]`, excuse my poor english – David Ranieri Aug 14 '14 at 06:23
1

Since there is no conversion between int (*)[3] and int*, you must employ a cast to force it.

Just pass a 2D array to your function :

 void print(int a[][3], int m, int n)
 {
       int i, j;
       for (i = 0; i < m; i++)
       for (j = 0; j < n; j++)
       printf("%d ", a[i][j]); 
 }
quantdev
  • 22,595
  • 5
  • 47
  • 84
0

Generally, typecasting will change the properties of a variable. In main function, arr is a 2D array but print function treats arr as a pointer.So typecasting is required

Example for property change:

In main function, if you increment the arr + 1 12 bytes will get incremented, but in case of print function arr + 1 will increment only 4 bytes.

David Ranieri
  • 36,077
  • 6
  • 44
  • 85
mahendiran.b
  • 1,299
  • 7
  • 13
  • @SumitKumar The assumptions of the size of an `int` on your platform aside, the "math" in this answer would be considerably less confusing if it referred to *elements* rather than *bytes*. In `main`, `arr` is an array of arrays. Each outer-dimension "element" is an array of three `int`. In your function, `arr` is a simple pointer-to-int. Each "element" is a single `int`. – WhozCraig Aug 14 '14 at 06:12
  • In print function, arr is a integer pointer. Pointer size is 4 bytes. So when you increment arr+1, 4 bytes will get incremented. – mahendiran.b Aug 14 '14 at 06:12
  • @mahendiran.b that last comment is outright wrong. The "pointer size" has nothing to do with the pointer math and the stride of *bytes* therein. The pointer *type* is what dictates the byte-stride, not the size of a pointer. And how do you *know* `int` is 4-bytes on the OP's platform? – WhozCraig Aug 14 '14 at 06:14
  • @WhozCraig sorry for that. Size of INTEGER is 4 bytes. So integer pointer is getting incremented 4 bytes. – mahendiran.b Aug 14 '14 at 06:23