Hello Viewer! How are you I hope you well? In the previous lesson, we learn Array in C language. in this lesson we'll learn Pointer in C language ( Part 1 )...
In C language the most difficult topic is POINTER. In other language have a pointer but few uses as compare to C language. In this post, we discussed pointer and their usage in the function call. Let's begin with the function calls.
Call by Value and Call by Reference
We are well know how to call a function. whenever we call a function and passed something to it we have always passed the "values" of variables to the called function. Such a function are called Call by Value. understand with the following example :
sum = calculate ( a, b, c ) ;
f = factr ( a ) ;
We have also learned that variable is stored somewhere in memory. so instead of passing the value of a variable, can we not pass the location number ( called address ) of the variable to a function? if we are able to pass the address, it would become a Call by Reference. this features of C function needs at least an elementary knowledge of a concept called Pointer.
Introduction
Basically, Pointer is variable that contains the address of another variable. understand the following declaration,
int i = 3 ;
Above declaration compiler understand :
a) Reserve space in memory to hold the integer value.
b) Associate the name i with this memory location.
c) Store the value 3 at this location.
we see that the computer has selected location 1000 as the place to store the value 3. The location number 1000 is not a number to be relied upon, because some other time the computer may choose a different location for storing the value 3. The important point is, i's address in memory is a number.
We can print address number through the following program:
#include < stdio.h >
int main( )
{
int i = 3 ;
printf ( "\nAddress of i = %u\n",&i );
printf ( "\nValue of i = %d\n" , i ) ;
return 0 ;
}
The Output of the above program would be...
Address of i = 1000 // ( 1000 is the address of the variable where that stored in memory )
Value of i = 3
Look at the 1st printf ( ) statement carefully. " & ( address of ) " operator is used to print the address of the variable. The expression &i returns the address of the variable i, which in this case happens to be 1000. since 1000 represents an address, there is no question of a sign being associated with it. Hence it is printed out using %u which is a format specifier for printing an unsigned integer. we have been using the " & " operator all the time in the scanf ( ) statement.
The other pointer operator available in C is " * ", called Value at Address operator. it gives the value stored at a particular address. The Value at Address operator is also called Indirection operator.
Understand the following example...
#include < stdio. h>
int main ( )
{
int i = 3 ;
printf ("Address of i = %u\n", &i ) ;
printf ("Value of i = %d\n", i ) ;
printf ("Value of i = %d\n", *(&i) ) ;2
return 0 ;
}
Output :
Address of i = 1000
Value of i = 3
Value of i = 3
In the above program, you see the value of *(&i) is the same as the 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 " j " is not an ordinary variable like any other integer variable. it is a variable that contains the address of another variable. Since " j " is a variable, the compiler must provide it space in the memory. Once again the memory map shown in the figure ...
As you can see, i value is 3 and j value is i's address.
But, in a program, we can't use j in a program without declaring it. and since j is a variable that contains the address of i, it is declared as,
int *j ;
This declaration tells the compiler that j will be used to store the address of an integer value. in other words, j points to an integer. how do we justify the stage of * in the declaration,
int * j ;
Now our point is to understand the meaning of *. It stands for Value at address. Thus, int *j means the value at the address contained in j is an int.
In C language the most difficult topic is POINTER. In other language have a pointer but few uses as compare to C language. In this post, we discussed pointer and their usage in the function call. Let's begin with the function calls.
Call by Value and Call by Reference
We are well know how to call a function. whenever we call a function and passed something to it we have always passed the "values" of variables to the called function. Such a function are called Call by Value. understand with the following example :
sum = calculate ( a, b, c ) ;
f = factr ( a ) ;
We have also learned that variable is stored somewhere in memory. so instead of passing the value of a variable, can we not pass the location number ( called address ) of the variable to a function? if we are able to pass the address, it would become a Call by Reference. this features of C function needs at least an elementary knowledge of a concept called Pointer.
Introduction
Basically, Pointer is variable that contains the address of another variable. understand the following declaration,
int i = 3 ;
Above declaration compiler understand :
a) Reserve space in memory to hold the integer value.
b) Associate the name i with this memory location.
c) Store the value 3 at this location.
We can print address number through the following program:
#include < stdio.h >
int main( )
{
int i = 3 ;
printf ( "\nAddress of i = %u\n",&i );
printf ( "\nValue of i = %d\n" , i ) ;
return 0 ;
}
The Output of the above program would be...
Address of i = 1000 // ( 1000 is the address of the variable where that stored in memory )
Value of i = 3
Look at the 1st printf ( ) statement carefully. " & ( address of ) " operator is used to print the address of the variable. The expression &i returns the address of the variable i, which in this case happens to be 1000. since 1000 represents an address, there is no question of a sign being associated with it. Hence it is printed out using %u which is a format specifier for printing an unsigned integer. we have been using the " & " operator all the time in the scanf ( ) statement.
The other pointer operator available in C is " * ", called Value at Address operator. it gives the value stored at a particular address. The Value at Address operator is also called Indirection operator.
Understand the following example...
#include < stdio. h>
int main ( )
{
int i = 3 ;
printf ("Address of i = %u\n", &i ) ;
printf ("Value of i = %d\n", i ) ;
printf ("Value of i = %d\n", *(&i) ) ;2
return 0 ;
}
Output :
Address of i = 1000
Value of i = 3
Value of i = 3
In the above program, you see the value of *(&i) is the same as the 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 " j " is not an ordinary variable like any other integer variable. it is a variable that contains the address of another variable. Since " j " is a variable, the compiler must provide it space in the memory. Once again the memory map shown in the figure ...
As you can see, i value is 3 and j value is i's address.
But, in a program, we can't use j in a program without declaring it. and since j is a variable that contains the address of i, it is declared as,
int *j ;
This declaration tells the compiler that j will be used to store the address of an integer value. in other words, j points to an integer. how do we justify the stage of * in the declaration,
int * j ;
Now our point is to understand the meaning of *. It stands for Value at address. Thus, int *j means the value at the address contained in j is an int.
In this part of array so enough 4 today. we'll discussed part II 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.