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 %26lt;stdio.h%26gt;
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;
}
Why can't functions be returned in C without using pointers?
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%26lt;row;i++)
{
for( j=0;j%26lt;col;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%26lt;row;i++)
{
for( j=0;j%26lt;col;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 !!!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment