Hello Viewer! How are you I hope you well? In the previous lesson, we learn Pointer in C language ( Part 1 ). in this lesson we'll learn Pointer in C language ( Part 2 )...
Look at the following declaration
int *alpha ;
char *ch ;
float *s ;
here alpha,ch, and s are declared as pointer variables, i.e., variable capable for holding addresses, address is always going to whole numbers.
Note:
An address is always whole numbers, pointers would always contain whole numbers.
The declaration float *s does not mean that s is going to contain a floating-point value. it means s is going to contain the address of a floating-point value, similarly, char is going to contain the address of char value.
We know is a variable that contains the address of another variable. now variable itself might be another pointer. Thus we know have a pointer that contains another pointer's address. understand the following example:-
#include < stdio.h >
int main( )
{
int i=3,*j,**k ;
j=&i ;
k=&j ;
printf("\nAddress of i=%u\n",&i) ;
printf("\nAddress of i=%u\n",j) ;
printf("\nAddress of i=%u\n",*k) ;
printf("\nAddress of j=%u\n",&j) ;
printf("\nAddress of j=%u\n",k) ;
printf("\nAddress of k=%u\n",&k) ;
printf("\nValue of j=%u\n",j) ;
printf("\nValue of k=%u\n",k) ;
printf("\nValue of i=%u\n",i) ;
printf("\nValue of i=%u\n",*(&i)) ;
printf("\nValue of i=%u\n",*j) ;
printf("\nValue of i=%u\n",**k) ;
return 0;
}
Output:-
Address of i=6487628
Address of i=6487628
Address of i=6487628
Address of j=6487616
Address of j=6487616
Address of k=6487608
Value of j=6487628
Value of k=6487616
Value of i=3
Value of i=3
Value of i=3
Value of i=3
when you run above program, the address that gets printed might turn out to be something different than then the one shown in below figure...
observe how the variable j and k have been declared,
int i , *j , **k ;
here i an ordinary int, j is a pointer to an int, where k is a pointer to an integer pointer.
Back to Function Calls
We already discussed two type of function calls - call by value and call by reference. arguments can generally be passed to function in one of the two ways :
a) sending the values of the arguments
b) sending the addresses of the arguments
In the 1st method, the 'value' of each of the actual arguments in the calling function is copied into corresponding formal argument of the called function. with this method, the changes made to the formal arguments in the called function have to effect on the values of actual arguments in the calling function.
Conclusions
1) If we want that the value of an actual argument should not get changed in the function being called, pass the actual argument by value.
2) If we want that the value of an actual argument should get changed in the function being called, pass the actual argument by reference.
3) If a function is made to return more than one value at a time, then return these values indirectly by using a call by reference.
Summary
1) A pointer is a variable which holds the addresses of other variables.
2) A pointer to a pointer is a variable that holds the address of a pointer variable.
3) The &, operator fetches the address of the variable in memory.
4) The * Operator access the value present at an address in memory with an intention of reading it or modifying it.
5) A function can be called either by value or by reference.
6) Pointers can be used to make a function return more than one value simultaneously in an indirect manner.
Look at the following declaration
int *alpha ;
char *ch ;
float *s ;
here alpha,ch, and s are declared as pointer variables, i.e., variable capable for holding addresses, address is always going to whole numbers.
Note:
An address is always whole numbers, pointers would always contain whole numbers.
The declaration float *s does not mean that s is going to contain a floating-point value. it means s is going to contain the address of a floating-point value, similarly, char is going to contain the address of char value.
We know is a variable that contains the address of another variable. now variable itself might be another pointer. Thus we know have a pointer that contains another pointer's address. understand the following example:-
#include < stdio.h >
int main( )
{
int i=3,*j,**k ;
j=&i ;
k=&j ;
printf("\nAddress of i=%u\n",&i) ;
printf("\nAddress of i=%u\n",j) ;
printf("\nAddress of i=%u\n",*k) ;
printf("\nAddress of j=%u\n",&j) ;
printf("\nAddress of j=%u\n",k) ;
printf("\nAddress of k=%u\n",&k) ;
printf("\nValue of j=%u\n",j) ;
printf("\nValue of k=%u\n",k) ;
printf("\nValue of i=%u\n",i) ;
printf("\nValue of i=%u\n",*(&i)) ;
printf("\nValue of i=%u\n",*j) ;
printf("\nValue of i=%u\n",**k) ;
return 0;
}
Output:-
Address of i=6487628
Address of i=6487628
Address of i=6487628
Address of j=6487616
Address of j=6487616
Address of k=6487608
Value of j=6487628
Value of k=6487616
Value of i=3
Value of i=3
Value of i=3
Value of i=3
when you run above program, the address that gets printed might turn out to be something different than then the one shown in below figure...
observe how the variable j and k have been declared,
int i , *j , **k ;
here i an ordinary int, j is a pointer to an int, where k is a pointer to an integer pointer.
Back to Function Calls
We already discussed two type of function calls - call by value and call by reference. arguments can generally be passed to function in one of the two ways :
a) sending the values of the arguments
b) sending the addresses of the arguments
In the 1st method, the 'value' of each of the actual arguments in the calling function is copied into corresponding formal argument of the called function. with this method, the changes made to the formal arguments in the called function have to effect on the values of actual arguments in the calling function.
Conclusions
1) If we want that the value of an actual argument should not get changed in the function being called, pass the actual argument by value.
2) If we want that the value of an actual argument should get changed in the function being called, pass the actual argument by reference.
3) If a function is made to return more than one value at a time, then return these values indirectly by using a call by reference.
Summary
1) A pointer is a variable which holds the addresses of other variables.
2) A pointer to a pointer is a variable that holds the address of a pointer variable.
3) The &, operator fetches the address of the variable in memory.
4) The * Operator access the value present at an address in memory with an intention of reading it or modifying it.
5) A function can be called either by value or by reference.
6) Pointers can be used to make a function return more than one value simultaneously in an indirect manner.
In this part of Pointer so enough 4 today. we'll discuss new topic in future post. I hope you like it.
Thank You 4 reading my blog! if you have any problem or issue with this post please comment/contact.
Important :
In this post, Article/Paragraph is taken from Let Us C book that is written by Yashavant Kanetkar.