In programming we make a call by two methods
1)call by value- Pass a value
2)call by reference - Pass a address and fun taking this address should have pointer.And also to display result again pointer.
Array is passed to a function using Call be reference.
There is Two ways
1) Using &
1)call by value- Pass a value
2)call by reference - Pass a address and fun taking this address should have pointer.And also to display result again pointer.
Array is passed to a function using Call be reference.
There is Two ways
1) Using &
- void main( )
- {
- int a[5]={1,2,3,4,5}
- for(int i=0 ; i<=4 ;i++)
- {
- printarray(&a[0]); //or printarray(&a);[NOT SURE]
- }
- }
- .
- .
- .
- .
- void printarray(int *p)
- {
- printf("%d",*p);
- }
2) Without Using &
- void main( )
- {
- int a[5]={1,2,3,4,5}
- printarray(a);
- }
- .
- .
- .
- .
- void printarray(int *p)
- {
- for(int i=0 ; i<=4 ;i++)
- {
- printf("%d",*p);
- }
- }
- }
Passing Character Array through a Function
STRING IS THE ARRAY OF CHARACTER WITH ONE NULL '\O' CHARACTER IN THE END
There is 4 ways of initilizing a character
1)
- char c[4];
- c[0]='j'; c[1]='o'; c[2]='h'; c[3]='n'; c[4]='\0';
2)
- c[ ]={'j','o','h','n','\0'}
3)Using String Literal
- c[ ]="john";
4) Using Pointer
- char*c="Hellow";
- int main( )
- {
- char *c="Hellow";
- print( c)
- .
- return(0);
- .
- }
- .
- .
- .
- .
- ..
- .
- void print(char *c)
- {
- int i=0;
- while( *(c+i)!= '\0') )
- {
- printf("%c",c[i]);
- i++;
- }
- printf("\n");
- }
- .
- .//or
- .
- .
- .
- .
- .
- void print(char *c)
- {
- while( *(c)!= '\0') )
- {
- printf("%c",c[i]);
- c++;
- }
- printf("\n");
Passing a 2-Dimentional Array through a Function
while passing two dimensional array to function last dimension is compulsory
- int func (int a[ ][3])
- {
- .
- .
- .
- .
- .}
Passing a 3-Dimentional Array
- int func (int a[ ][2][2]) / (int (*a)[2][2])
- {
- .
- .
- .
- .
- .}
No comments:
Post a Comment