English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2006-10-28 07:19:34 · 2 answers · asked by Dvidid/0 2 in Computers & Internet Programming & Design

Sorry i meant to ask "why can't functions return an array without using pointers.?"

2006-10-28 18:13:51 · update #1

2 answers

Cause that's the way Brian and Dennis designed the language.

(Same answer for the returning of an array.)
You can return a structure with an array in it:
#include
struct thingie {
int a[3];
};

struct thingie testFunc( ) {
thingie t;
t.a[0] = 1;
t.a[1] = 3;
t.a[2] = 5;
return t;
}

int main ( ) {
struct thingie x = testFunc( );
printf ("%d\n",x.a[2]);
return 0;
}

2006-10-28 07:31:59 · answer #1 · answered by gerardw 2 · 0 0

U mean passing an array to a function ? Yes it can be done both using / without using pointers. Take a look at the program below :

main()
{
int a[3][4] = { 1,2,3,4,,5,6,7,8,9,0,1,6} ;
clrscr() ;
display(a,3,4) ;
show(a,3,4) ;
}

// accessing array using pointer
display( int *q, int row, int col)
{
int i,j ;
for( i=0;i {
for( j=0;j printf("%d", *(q+i*col+j)) ;
printf("\n") ;
}
printf("\n");
}

//accessing array without using pointer
show( int q[][4],int row,int col)
{
int i,j ;
for( i=0;i {
for( j=0;j printf("%d", q[i][j]) ;
printf("\n") ;
}
printf("\n");
}


In both cases, it'll print
1 2 3 4
5 6 7 8
9 0 1 6
as the output. Check it out !!!

2006-10-29 01:14:34 · answer #2 · answered by Innocence Redefined 5 · 0 0

fedest.com, questions and answers