473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Passing 2D array to a function using single pointer .

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void print(int *arr, int m, int n)
  3. {
  4. int i, j;
  5. for (i = 0; i < m; i++)
  6. for (j = 0; j < n; j++)
  7. printf("%d ", *((arr+i*n) + j));
  8. }
  9. int main()
  10. {
  11. int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  12. int m = 3, n = 3;
  13.  
  14. print((int *)arr, m, n);
  15. return 0;
  16. }
  17.  
Why can't I use *(*(arr+i)+j) notation inside printf in print function .Although it's a pointer to an integer still I am operating on the address of first element of array , so what's wrong in this ?
Apr 8 '17 #1

✓ answered by hackr

1) When both dimensions are available globally (either as a macro or as a global constant).

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. const int M = 3;
  3. const int N = 3;
  4.  
  5. void print(int arr[M][N])
  6. {
  7.     int i, j;
  8.     for (i = 0; i < M; i++)
  9.       for (j = 0; j < N; j++)
  10.         printf("%d ", arr[i][j]);
  11. }
  12.  
  13. int main()
  14. {
  15.     int arr[][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  16.     print(arr);
  17.     return 0;
  18. }
Run on IDE
Output:
Expand|Select|Wrap|Line Numbers
  1. 1 2 3 4 5 6 7 8 9
2) When only second dimension is available globally (either as a macro or as a global constant).

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. const int N = 3;
  3.  
  4. void print(int arr[][N], int m)
  5. {
  6.     int i, j;
  7.     for (i = 0; i < m; i++)
  8.       for (j = 0; j < N; j++)
  9.         printf("%d ", arr[i][j]);
  10. }
  11.  
  12. int main()
  13. {
  14.     int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  15.     print(arr, 3);
  16.     return 0;
  17. }
Run on IDE
Output:

Expand|Select|Wrap|Line Numbers
  1. 1 2 3 4 5 6 7 8 9
The above method is fine if second dimension is fixed and is not user specified. The following methods handle cases when second dimension can also change.

3) If compiler is C99 compatible
From C99, C language supports variable sized arrays to be passed simply by specifying the variable dimensions (See this for an example run)

Expand|Select|Wrap|Line Numbers
  1. // The following program works only if your compiler is C99 compatible.
  2. #include <stdio.h>
  3.  
  4. // n must be passed before the 2D array
  5. void print(int m, int n, int arr[][n])
  6. {
  7.     int i, j;
  8.     for (i = 0; i < m; i++)
  9.       for (j = 0; j < n; j++)
  10.         printf("%d ", arr[i][j]);
  11. }
  12.  
  13. int main()
  14. {
  15.     int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  16.     int m = 3, n = 3;
  17.     print(m, n, arr);
  18.     return 0;
  19. }
Run on IDE
Output on a C99 compatible compiler:

Expand|Select|Wrap|Line Numbers
  1. 1 2 3 4 5 6 7 8 9
If compiler is not C99 compatible, then we can use one of the following methods to pass a variable sized 2D array.

4) Using a single pointer
In this method, we must typecast the 2D array when passing to function.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void print(int *arr, int m, int n)
  3. {
  4.     int i, j;
  5.     for (i = 0; i < m; i++)
  6.       for (j = 0; j < n; j++)
  7.         printf("%d ", *((arr+i*n) + j));
  8. }
  9.  
  10. int main()
  11. {
  12.     int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  13.     int m = 3, n = 3;
  14.  
  15.     // We can also use "print(&arr[0][0], m, n);"
  16.     print((int *)arr, m, n);
  17.     return 0;
  18. }
Run on IDE
Output:

Expand|Select|Wrap|Line Numbers
  1. 1 2 3 4 5 6 7 8 9

9 3269
weaknessforcats
9,208 Expert Mod 8TB
What's not working?

I ran your code and got 1 2 3 4 5 6 7 8 9 displayed, which is correct.
Apr 8 '17 #2
If I am using *(*(arr+i)+j) instead of *((arr+i*n) + j) , then I am unable to get the correct OP , it shows error :
invalid type argument of 'unary *'

I am unable to get this .
Apr 8 '17 #3
If I am using *(*(arr+i)+j) instead of *((arr+i*n) + j) , then I am unable to get the correct OP , it shows error :
invalid type argument of 'unary *'

I am unable to get this .
Apr 8 '17 #4
If I am using *(*(arr+i)+j) instead of *((arr+i*n) + j) , then I am unable to get the correct OP , it shows error :
invalid type argument of 'unary *'

I am unable to get this .
Apr 8 '17 #5
weaknessforcats
9,208 Expert Mod 8TB
You can't use this:

*(*(arr+i)+j)

because arr is an address so arr + i is also an address. But
*(arr + i) is an int. This makes *(arr+i) + j an int. Now you use another dereferencing * on an int so you get an error.
Apr 8 '17 #6
Thanks , I got the point
Apr 9 '17 #7
hackr
3
1) When both dimensions are available globally (either as a macro or as a global constant).

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. const int M = 3;
  3. const int N = 3;
  4.  
  5. void print(int arr[M][N])
  6. {
  7.     int i, j;
  8.     for (i = 0; i < M; i++)
  9.       for (j = 0; j < N; j++)
  10.         printf("%d ", arr[i][j]);
  11. }
  12.  
  13. int main()
  14. {
  15.     int arr[][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  16.     print(arr);
  17.     return 0;
  18. }
Run on IDE
Output:
Expand|Select|Wrap|Line Numbers
  1. 1 2 3 4 5 6 7 8 9
2) When only second dimension is available globally (either as a macro or as a global constant).

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. const int N = 3;
  3.  
  4. void print(int arr[][N], int m)
  5. {
  6.     int i, j;
  7.     for (i = 0; i < m; i++)
  8.       for (j = 0; j < N; j++)
  9.         printf("%d ", arr[i][j]);
  10. }
  11.  
  12. int main()
  13. {
  14.     int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  15.     print(arr, 3);
  16.     return 0;
  17. }
Run on IDE
Output:

Expand|Select|Wrap|Line Numbers
  1. 1 2 3 4 5 6 7 8 9
The above method is fine if second dimension is fixed and is not user specified. The following methods handle cases when second dimension can also change.

3) If compiler is C99 compatible
From C99, C language supports variable sized arrays to be passed simply by specifying the variable dimensions (See this for an example run)

Expand|Select|Wrap|Line Numbers
  1. // The following program works only if your compiler is C99 compatible.
  2. #include <stdio.h>
  3.  
  4. // n must be passed before the 2D array
  5. void print(int m, int n, int arr[][n])
  6. {
  7.     int i, j;
  8.     for (i = 0; i < m; i++)
  9.       for (j = 0; j < n; j++)
  10.         printf("%d ", arr[i][j]);
  11. }
  12.  
  13. int main()
  14. {
  15.     int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  16.     int m = 3, n = 3;
  17.     print(m, n, arr);
  18.     return 0;
  19. }
Run on IDE
Output on a C99 compatible compiler:

Expand|Select|Wrap|Line Numbers
  1. 1 2 3 4 5 6 7 8 9
If compiler is not C99 compatible, then we can use one of the following methods to pass a variable sized 2D array.

4) Using a single pointer
In this method, we must typecast the 2D array when passing to function.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void print(int *arr, int m, int n)
  3. {
  4.     int i, j;
  5.     for (i = 0; i < m; i++)
  6.       for (j = 0; j < n; j++)
  7.         printf("%d ", *((arr+i*n) + j));
  8. }
  9.  
  10. int main()
  11. {
  12.     int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  13.     int m = 3, n = 3;
  14.  
  15.     // We can also use "print(&arr[0][0], m, n);"
  16.     print((int *)arr, m, n);
  17.     return 0;
  18. }
Run on IDE
Output:

Expand|Select|Wrap|Line Numbers
  1. 1 2 3 4 5 6 7 8 9
Apr 19 '17 #8
weaknessforcats
9,208 Expert Mod 8TB
When you pass an argument to a function in C the argument is a copy of the variable used by the calling function. In the case of arrays, what is passed is the address of the array so the function in using that address operates on the original array that belongs to the calling function.

For this reason you cannot pass an array to a function.

What does happen is the function has a copy of the address of the array but has lost the dimensions so it has no idea how large the array is. This is called decay-of-array. True you can provide access to the dimensions by using variables from somewhere but there is no guarantee the values are accurate. If these values change in the called function, then the calling function has bad info and crashes. Any dimensional values must be passed (in C) by address.

If the array is on the stack and you pass the address to a function, that function can pass it anywhere else, save it to disc, etc. Then when the original function completes, the stack frame is deleted and all those saved copies of the array pointers now are invalid and when accessed will crash the program.

So addresses of arrays must be heap addresses which brings up memory allocations, leaks, reference counting whereby the array cannot be deleted until the last copy of the pointer is deleted.

etc.
etc.

This dire weakness of arrays in C is the main reason for the <vector> template in C++.
Apr 20 '17 #9
Thanks a lot , I understood the concept now .
Apr 20 '17 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
4
by: Samie | last post by:
Hi I have declared a structure that contains pointer to a function. The code goes like this: typedef struct lcd_funs lcd_funs; struct lcd_funs { void (*decode_image)(cyg_uint32 imageWidth,...
9
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
11
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
2
by: Neil Munro | last post by:
I'm having some "type" difficulty in passing a .NET array (byref) to a COM based API (Autodesk Inventor). In VB6 the code to return an array of tolerance values is: Dim ToleranceCount As Long...
4
by: qazmlp1209 | last post by:
Is the following code reliable? I'm wondering whether it is valid to invoke the static function using uninitialized pointer. ----------------- #include <iostream> class staticCl { public:...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
2
by: james44 | last post by:
Hello All, In the code below when the char *s pointer is declared 4 bytes will be allocated to hold the address of object being pointed by but how much memory is allocated to the...
9
by: =?Utf-8?B?RGFya21hbg==?= | last post by:
Hi, I am wondering how you multi-dimension an array function? My declared function looks like this: Public Function GetCustomerList(ByVal Val1 As String, ByVal Val2 As Long, ByVal Val3 As...
1
by: KushShukla | last post by:
Is this right? int *p = NULL; for(int i=0;i<M;i++) p = new int; Will this be going to allcate 2D array of M*N? If yes how to check?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.