www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 20: Pointers and Functions Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
In C language, the default mechanism of function call is ‘call by value’. Sometimes we want to make a call by reference. In call by reference, we pass the address of the variable to a function by using & operator. One of the major usages of pointers is to simulate call by reference while using it with
function calls. In the calling function, we pass the address of the variable to a function being called by using & operator. We write a function call as fn( &x ) where &x indicates that the address of variable x is being passed to the function fn. In the receiving function, the function must know that the parameter passed to it is an address. So the declaration of the receiving function will be as

void fn ( int *num)

statement(s) ;

The int *num in the function declaration indicates that the receiving variable is a pointer to a memory address. In the body of the function, we will use this variable as:

cin >> *num ;

This statement describes that the value entered through the keyboard (as cin is used) will be stored at the memory address wherever the pointer num is pointing to. While using value associated with the pointer, we write *num and &num in case of using the address. This thing can be summarized as follows “*num means the value of whatever the num points to and &num means the address of the variable num” The pointers can appear on the left hand side exactly like ordinary variables. In this case, you would have an address statement on the right hand side. The address (operator (&) ) cannot be of an expression. Rather, it is always of a simple variable. We cannot write &(x+y). The address (&) would be either of x (&x) or of y (&y). The address operator (&) operates on a simple variable. Precisely speaking, whenever we have a pointer on left hand side, the right hand side should have an address. If a pointer appears on the right hand side of an expression, it can participate in any expression. In this case, we use the operator * with the pointer name and get the value stored where the pointer points to. Obviously we can do any calculation with this value (i.e. it can be used in any expression).

Constant Pointers:

Let’s look at the use of const with pointers. Consider the following line of declaration:

int *const myptr = &x ;

The right hand side of this assignment statement could be read as, myptr is a constant pointer to an integer. Whenever we use the keyword const with a variable, the value of that variable becomes constant and no other value can be assigned to it later on. We know that when we declare a constant variable like const int x ; it is necessary to assign a value to x and we write

const int x = 10;

After this, we cannot assign some other value to x. The value of x can not be changed as it is declared as a constant. Now consider the previous statement

int *const myptr = &x ;

Here we declare a constant pointer to an integer. Being a constant pointer, it should immediately point to something. Therefore, we assign this pointer an address of a variable x at the time of declaration. Now this pointer cannot be changed. The pointer myptr will hold the address of variable x throughout the program. This way, it becomes just another name for the variable x.The use of keyword const in declaration statement is a little tricky. The statement

int *const myptr = &x ;

means myptr is a constant pointer to an integer. But if we change the place of const in this
statement and write

const int *myptr = &x ;

This statement describes that myptr is a pointer to a constant integer. This means that the value of pointer myptr can be changed but the value stored at that location cannot be changed. This declaration is useful. It has a common use in call by reference mechanism. When we want to pass the arguments to a function by reference without changing the values stored at that addresses. Then we use this construct of declaration (i.e. const int *myptr) in the called function declaration. We write the declaration of the function like

fn ( const int *myptr)

This declaration informs the function that the receiving value is a constant integer. The function cannot change this value. Thus we can use the address of that value for manipulations but cannot change the value stored at that location.

NEXT>>>>>Lesson 21. Pointers and Arrays

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.