Pointers in C++

this pointer in C++

C++ uses a unique keyword called this to represent an object that invokes a member function. this pointer in C++ is a pointer that points to the object for which this function was called. For example, the function call: A.max() will set the pointer this to the address of the object A. The starting address is the same as the address of the first variable in the class structure. This unique pointer is automatically passed to a member function when it is called. The pointer this acts as an implicit argument to all the member functions. Consider the following simple example: class ABC { int a; … … }; The private variable a can be used directly inside a member function, like: a = 123; We can also use the following statement to do the same job: this -> a = 123; Since C++ permits the use of shorthand form...

Pointers to functions in C++

In C++, just as we can have pointers to variables, we can have pointers to functions as well. The general syntax of a pointers to a functions is: return-type (*pointer-name) (list of parameter); For example: void (*p) (float, int); In the above declaration, a pointer to a function returns void and takes the two formal arguments of float and int types respectively. After declaring a pointer to a function, the address of the function must be assigned to a pointer as follows: p = &function-name; where p is a pointer variable. Through pointer to function, the function is called as follows: A = (*p) (x, y); where, x and y are actual parameters, and A is a variable in which the function’s return value is stored, p is a pointer name which points to the function Let us illustrate pointer to function with a programming example as follows: #include<iostream.h> void...

Void Pointers in C++

For understanding the concept of void pointers, we should note that we cannot assign the address of one data-type variable to a pointer of another data-type variable. Consider the following: Note that we can not assign the address of float type variable to a pointer to int as: float y; int *p; p = &y; //illegal statement Similarly, see the following: float *p; int x; p = &x; //illegal statement That means, if variable-type and pointer-to-type is same then only we can assign the address of that variable to pointer variable. If both, i.e., the data-type of the variable and the data-type pointed to by the pointer variable, are different then we can not assign the address of variable to pointer variable. But this impossible thing is made possible in C++ by declaring pointer variables as void as follows: void *p; The above pointer is called pointer to void type....

Accessing of the Variables pointed to by the Pointer

Accessing of the variable pointed to by the pointer needs to be understood from two angles, i.e., Accessing of normal basic data-type variables Accessing of class-type objects Accessing of normal basic data-type variables: Let us straight-away learn the accessing of normal basic data-type (line int, float, etc.) variables by seeing a simple example as follows: int *p; int x; x = 20; p = &x; //read as assign address of x (&x) to pointer variable p cout << *p; //read as print content at pointer variable p The statement: cout << *p; prints the value 20 because *p means the value at the address p. Similarly: *p = *p + 10 is same as: x = x + 10; Note that if we write the following statement: cout << p; this prints the address of x. Similarly the statement: cout << &x; also prints the addresss of x. Accessing the...

Declaration of Pointers in C++

Pointer variables, like all other variables, must be declared before they may be used in a C++ program. The interpretation of a pointer declaration differs, however, from the interpretation of other variable declarations. When a pointer variable is declared, the variable name must be preceded by an asterisk (*). This identifies the fact that the variable is a pointer. The data type that appears in the declaration refers to the object of the pointer, i.e., the data item that is stored in the address represented by the pointer, rather than the pointer itself. This, the pointer declaration may be written in general terms as: data-type *ptvar; where, ptvar is the name of the pointer variable, and data-type refers to the datatype of the pointer’s object. Remember that an * (asterisk) must precede ptvar.

Basics of Pointers in C++

Before we start with basics of pointers, it is strictly recommended to go through the concepts of pointers from the Learn C Online (Click here). We have attempted to cover pointers in the most simplest manner possible. But, it will be impossible for you to grasp the knowledge of the pointers unless and until you have the concepts of pointers clear in your mind by reading – Learn C Online (Click here). A pointer is a variable that represents the location (rather than the value) of a data item, such as a variable or an array element. Suppose v is a variable that represents some particular data item. The compiler will automatically assign memory cells for this data item. The data item can then be accessed if we know the location (i.e. the address) of the first memory cell allocated to variable v by the compiler. The address of v‘s...