Category: Pointers in C++

Interested in learning C Programming? Click here...


this pointer in Cpp 1

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:

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...

Pointers to functions in Cpp 2

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:

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:

where,...


Void Pointers in Cpp 0

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:

Similarly, see the following:

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...

Accessing of the Variables pointed to by the Pointer 0

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:

The statement:

prints the value 20 because *p means the value at the address p. Similarly:

is same as:

Note that if we write the following statement:

this prints the address of x. Similarly the statement:

also prints the...