Nekochan Net

Official Chat Channel: #nekochan // irc.nekochan.net
It is currently Thu Jul 24, 2014 6:38 am

All times are UTC - 8 hours


Forum rules


No politics, please.



Post new topic Reply to topic  [ 6 posts ] 
Author Message
Unread postPosted: Thu Jun 14, 2012 6:55 am 
Offline
User avatar

Joined: Wed Feb 23, 2005 5:14 am
Posts: 104
Location: London, UK
I wanted to pass a multi-dimensional array to a function in C. As in, directly manipulate the contents of an array passed as a parameter.

After many hours of messing around with pointers and getting nowhere, I decided maybe I was trying to be too complicated and tried the following :

Code:
#include <stdio.h>

void changeArray(int testarray[2][2])
{
   testarray[1][1]=3;
}

void changeInt(int test)
{
   test=4;
}


int main()
{
   int array[2][2] = { {1,1}, {1,1} };
   int myint=1;

   printf("%i %i\n",array[1][1],myint);
   changeArray(array);
   changeInt(myint);
   printf("%i %i\n",array[1][1],myint);
   

}


Sure enough, the result was :

Code:
$ ./PointerTest
1 1
3 1
$


myint works as I expected C to work. What I don't understand is why the multi-dimensional array is treated differently?

Thanks for any help you can give.


Top
 Profile  
 
Unread postPosted: Thu Jun 14, 2012 7:37 am 
Offline
User avatar

Joined: Tue Sep 25, 2007 1:00 am
Posts: 158
Location: Braunschweig (Germany)
When you call "changeInt(myint)", a copy of myint is made.
When you call "void changeArray(array)" a pointer to array is passed to the function.

That's why the content of the array is changed.


Top
 Profile  
 
Unread postPosted: Thu Jun 14, 2012 10:22 am 
Offline

Joined: Mon Sep 12, 2011 1:28 pm
Posts: 539
Location: Boston
the specific reason is that, except when used as the argument to operator& or sizeof(), array expressions are converted to a pointer to their first element.
This is handy for C's function calling rules, which do not allow array types to be passed to, or returned from functions.

_________________
:PI: :O2: :Indigo2IMP: :Indigo2IMP:


Top
 Profile  
 
Unread postPosted: Fri Jun 15, 2012 7:36 am 
Offline
User avatar

Joined: Wed Feb 23, 2005 5:14 am
Posts: 104
Location: London, UK
Thank you both for your replies. So, how would I pass a multi-dimensional array of unknown dimensions to a function? Using [][]?


Top
 Profile  
 
Unread postPosted: Fri Jun 15, 2012 9:38 am 
Offline
User avatar

Joined: Thu Dec 31, 2009 3:18 pm
Posts: 235
Location: Warsaw, PL
Use a pointer int*, pass inner dimension as argument and access it like array[i * width + j]. (the compiler will complain about passing int[][] as int*, I'm not sure how portable it is).


This:
Code:
void changeInt(int test)
{
   test=4;
}


does exactly nothing.

_________________
:PI: :Indigo: :Indigo: :Indigo: :Indy: :Indy: :Indigo2: :Indigo2IMP: :Octane: :Fuel: Image


Top
 Profile  
 
Unread postPosted: Fri Jun 15, 2012 11:46 am 
Offline
User avatar

Joined: Wed Nov 01, 2006 10:37 pm
Posts: 2914
Location: NZ
If the function does not know the dimensions you will need to pass those in, and if you have an arbitrary number of dimensions those need to be passed in an array along with another count of the number of dimensions.

_________________
Land of the Long White Cloud and no Software Patents.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group