[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: Passing arrays in C



On Wed, 29 Mar 2000, you wrote:
> hi all,
> 
> how can i pass a pointer to a multi-dimensional array to a function.all i'm
> able to pass,is a pointer to a single dimension of the array.i want to pass
> the entire array.
> 
> regards,
> sitaram shastri.

Actually, physical RAM is "linear" so multidim arrays are just a "way" of
expressing linear stuff in two dimensions or more. See if the following code
helps. zzz is not reccomended though.. you can also use formal parameters like
int* a[ ] etc.

---------- Code test.c : compile with : gcc -o test -g test.c -Wall ---------
#include <stdio.h>

void zz(int a[2][2])
{
	printf("%d\n%d\n%d\n%d\n",a[0][0],a[0][1],a[1][0],a[1][1]);
}

void zzz(int* a)
{
	printf("%d\n",*a);
	++a;
	printf("%d\n",*a);
	++a;
	printf("%d\n",*a);
	++a;
	printf("%d\n",*a);
	++a;
}	

int main()
{
	int a [2][2];
	
	a[0][0]=1;
	a[0][1]=2;
	a[1][0]=3;
	a[1][1]=4;
	
	zz(a); // equiv to &a[0][0]
	zzz(a);
	
	return 0;
}	



 
-- 
____________________________________________________________
One thing is clear.
Fuzzy logic is here to stay.

Shourya Sarcar
Dept. of Computer Science
Jadavpur University, Calcutta, India
Res : 364/3 Netaji Subhas Chandra Bose Road, India 700 047
Tel : 91-033-471 0477
____________________________________________________________