Home / Tutorial / Programming / C / C ++ Programming Tutorials (Episode 15) Other Pointer
c programming tutorial

C / C ++ Programming Tutorials (Episode 15) Other Pointer

A programmer can solve any problem in any manner, or a codec can write a program code for its own sake. Lastly, it does not matter whether it works or not. But if the quality of the program is asked to be judged, that is a different matter. In this case, if the code is performed with an algorithm, the program works most quickly or using the least resources. Pointer and many other features have emerged for these. Although discussions of different types of pointer were discussed in the last section, only null pointers were explained. In this episode, discussions related to the relationship between the other pointer and the next pointer and the array are discussed.

Vide pointer

In simple words, the Void Pointer is a special pointer, which can be used to determine the address of variables in any other type without having the castings. In other words, such type of pointer can be pointless without any type of variable casting. The rules for declaring such a pointer are:


void * poiter_name;

It turns out that to use this type of pointer to use the void keyword as the data type. We know that int * p means that p pointer points to an integer. Similarly double * p means that p pointer points to any double. Likewise void * p means that p is a pointer, which does not point to integers or float or double all types of variables without casting. If the pointer was not typed in the void, then the type of pointer could be pointing to the other type variables through casting.

It should be noted that using void as the data type of the common variable, there is no data in it (which is not commonly seen, because the main purpose of the variable is to keep the data). And if the data type of any function is void, then it does not return any value. That means that the void data type is used only when there is no need of quality. But there is a completely different incident in pointers. In this case, when all the data is needed, the voyage is used. Here is a small program such as the Voices Pointer:

int x = 10;
double y = 3.12;
void * ptr;
ptr = & x;

Here ptr is a pointer to the void type, and for this, the variable x address of the integer type will be determined, which means the ptr pointers will point to x. Similarly,

ptr = & y;

In this case, for the ptr, the variable type y address of double type will be determined, while the ptr pointers will point to y. Again, another type of data can be assigned to the void pointer. Such as:

int x = 20;
int * ip;
void * vp;
ip = & x;
vp = ip;

Here x is declared an integer variable, which is defined as 20. Then both the integers and the void type two pointer have been declared ip and vp respectively. Sox can be pointer to x with ip. Now VP is a void type pointer, so it’s pointing to everyone. Again, the void pointer is not just pointing to any variables, it can also point to pointers pointing to any type of pointers.

So at the end of the upper code, the vp pointer points to the data of the ip and the x. In this case the compiler will not show any errors. If there was any other pointers in place of vp, casting would be needed. But nothing needs to be done in the void pointer. In this way, void pointer can be assigned to other type pointer or other data type pointer for void pointer without casting. But if you want to work with the poinded address data through the void pointer, then the pointing data should be stored as:

* (pointed_data_type *) void_ptr;
Such as:
int x = 10, y;
void * ptr;
ptr = & x;
y = * (int *) ptr;

Here the address of x for ptr has been set here. Then ptr was cast in int * to read integer type data through ptr. In order to work with the poinched address data through the void pointer, the poinched data has to be cast in appropriate pointer type. However, a significant point is that the type of data that will be captured by the pointer data via the vide pointer, the data can be found according to the type in the output. Voic pointer is also called generic pointers. Void pointer cannot be used as an operand of increment, decrement or any other extrusion until it is cast in any other type.

Constant Pointer

Const Keywords can be used in many ways while declaring the pointer. But first, let’s see what happens when using this keyword while decrying non-pointers variables.

const i = 10;

Here the comp keyword is being informed by const keyword, i is a constant variable. So changing the data of this variable somewhere in the program in the same scope, there will be an error when compiling a compiling program. That is, the value of the constant variable cannot be changed.

Thus, if a variables have to be unchanged then they have to declare them as constants. Similarly, const keyword is used to constant the pointer to the program. Constant keywords can be used in both ways. Such as:

const datatype * pointerName = value; A_ev
datatype * const pointerName = value;

That is, the const keyword can sit in both places before or after the data type. But this is not the same thing. Let’s see what happens in the baseline and what happens next.

If the keyword is placed before the data type, then the pointer will point to the value of the point. This means that the value of the pointer can be changed, but the value of the points being pointed cannot be changed. In other words, pointer data can be read through the pointer, but can not be changed. In this way, a file is read-only in Windows, so that the file can be read-only, but can not be changed. Such as:

int i = 10, j;
const int * ptr;
ptr = & i;
j = * ptr;
* ptr = 20;

Here I can determine the data for I for j * ptr = 20; This statement cannot be set to 20 for i. Because declare the pointer to the compiler, the pointer that points to the value of the value will remain unchanged. So in the last line to change pointer data through ptr *, the compiler will show the error. So at the end, const int * ptr; This means that the integer variable address for the pointer will be determined, the data of that variable can be read only through the pointer, but it can not be changed.

But if the const keyword is used after data type, such as: int * const ptr; Then the pointer remains constant. That means that the value of the pointer itself can not be changed, but the pointers pointing to the pointers can be changed. A small program is given as an example:

int i = 10, j;
int * const ptr = & i;
j = * ptr;
* ptr = 20;
ptr = & j;

The pointer used here is Constant, which means that no other variable data can be specified by the pointer or the data of the pointer will remain unchanged. But the value of the poinched data can be read through the pointer (the third line) and it can be changed if needed (the fourth line). Finally, int * const ptr means the pointer variables are constants.

So this pointer cannot be pointer to anyone else. And const int * ptr means the point of the pointers pointing to the Constant So pointing data cannot be changed, but if you want the pointer to point it out to anyone else. Because the point is not pointing to the pointer – it does not have the pointer’s own data, pointed data. But this is not the end. Users can use const keywords on either side if they are needed. Such as const int * const ptr = & i; In this case both the value of the pointer and the value of the poinched data will remain constant. That means that pointers can not be pointing to someone else, so the value of points that can be changed with the pointer cannot be changed.

About Admin

Leave a Reply

Your email address will not be published. Required fields are marked *