Call_by_value_Pointers_explaination
Call_by_value_Pointers_explaination
CALL BY VALUE
&
CALL BY REFERENCE
POINTER
# include <stdio.h>
int main( )
{ Output
int i = 3 ;
Address of i = 65524
printf ( " %u\n", &i ) ; Value of i = 3
printf ( " %d\n", i ) ; Value of i = 3
printf ( "%d\n", *( &i ) ) ;
return 0 ;
}
• Note that printing the value of *( &i ) is same as printing the value of i.
• The expression &i gives the address of the variable i. This address can be collected in a
variable, by saying,
j = &i ;
• But remember that j is not an ordinary variable like any other integer variable. It is a
variable that contains the address of other variable (i in this case).
• Since j is a variable, the compiler must provide it space in the memory. Once again, the
memory map shown in Figure would illustrate the contents of i and j.
# include <stdio.h>
int main( ) Output
{
Address of i = 65524
int i = 3 ; Address of i = 65524
int *j ; Address of j = 65522
j = &i ; Value of j = 65524
Value of i = 3
printf ( "%u\n", &i ) ; Value of i = 3
printf ( "%u\n", j ) ; Value of i = 3
printf ( "%u\n", &j ) ;
printf ( "%u\n", j ) ;
printf ( "%d\n", i ) ;
printf ( "%d\n", *( &i ) ) ;
printf ( "%d\n", *j ) ;
return 0 ;
}
int *alpha ;
char *ch ;
float *s ;
***The declaration float *s does not mean that s is going to contain a floating-point value.
• Pointer, we know is a variable that contains address of another variable.
• Now this variable itself might be another pointer.
• Thus, we now have a pointer that contains another pointer’s address.
• k = &j and j = &i (So printing value of ‘i’ in terms of k will be)
*j=i
*k=j
i=**k
# include <stdio.h> Address of i = 65524
int main( ) Address of i = 65524
{ Address of i = 65524
Address of j = 65522
int i = 3, *j, **k ;
Address of j = 65522
j = &i ; Address of k = 65520
k = &j ; Value of j = 65524
printf ( "%u\n", &i ) ;
Value of k = 65522
Value of i = 3
printf ( "%u\n ", j ) ; Value of i = 3
printf ( "%u\n ", *k ) ; Value of i = 3
printf ( "%u\n ", &j ) ; Value of i = 3
printf ( "%u\n ", k ) ;
printf ( "%u\n ", &k ) ;
printf ( "%u\n ", j ) ;
printf ( "%u\n ", k ) ;
printf ( "%d\n ", i ) ;
printf ( "%d\n ", * ( &i ) ) ;
printf ( " %d\n ", *j ) ;
printf ( "%d\n ", **k ) ;
• The two types of function calls—call by value and call by reference.