Home / Tutorial / Programming / C / C ++ Programming Tutorial (Part 12) Pointer
c programming tutorial

C / C ++ Programming Tutorial (Part 12) Pointer

Pointer is one of the most important features of C language. The last few episodes are being discussed with the pointer. Using pointer as it is a bit difficult at the same time, its use makes the programmer a lot easier. Apart from this, many things can be done with the pointer, which can not be done in any other way. Although there are many types of pointer work, a feature of C language can be seen quite similarly – that is the array. There is a lot of similarity between the basic concept of pointers and basic concepts of the array. And so the pointer is like an array, and sometimes it can be used like a pointer whenever the array wants. But remember, the purpose of the pointer and array is different and if there is something similar between them, the original work is completely different. In this episode, the link between the pointer and the array is discussed.

Since the two pointers and arrays are discussed here, they must have basic idea about them. Since the pointer has been discussed in the last few episodes, so now its basic idea will not be discussed. But to remind you of the array is briefly discussed. Arrays are a method of declaring multiple variables together. Suppose we need to declare five variables simultaneously in a program. Then the user can declare five variables in common rules. You need to write five statements. Again, in a statement, five variables can be declared, but in that case the statement will be very large. But using the array, it is possible to declare five variables simultaneously with a statement.

Rule of declaration of arrays: data_type array_name [array_size]

The name of the array is the name of any variable. That means the names of the variables must be named after the array. The new thing here is the array size. Since the array is the sum of many variables. So, we have to say how many arrays are declared with variables. This is the array size. As the size is, the array with the variables will be composed. The main difference between arrays and variables declaration is size. Such as: int roll [10], class [5]; Etc. Here, 10 integers named Roll, named five classes, have been declared. And 10 variable names that have been declared roll, they can be accessed separately. In that case, roll [0], roll [1], roll [2] etc. will be the names of each variable.

Now let us see how there is a similarity between the arrays and the pointer. C element can also be used by point alignment in pointers notation. We know that to use the array, we have to declare it first. As the above mentioned roll name has been declared, 10 variables have been declared declare. But if the array declares a base address is determined for it. The base address works for the entire array. Let’s say the roll name array starts with the 0003 addresses in memory. That is, the base address of the array is 0001. Actually declares the array only this base address is determined, nothing else. This means that the value of the roll [0] will be 0001-0002 addresses. The value of the roll [1] will sit in the 0003-0004 address. Thus the user can use up to 10 variables. Notice that the base address of this 0001 array, it will work only for the roll arrays. If another array declares its base address will be different.

Now let’s see how this issue of the array relates to the pointer. If you know the address of the first element of the array, then the next element’s address can also be encrypted. For example, the first element of the array is 0001. So the second element will be addressed 0003. Here’s two increments, because it’s the integer type array and we know that an integer variable takes place by 2 bytes. So if it had an array of character type then it would have been incremented with address 1. Now if the user knows the address of the first element of the array, then the next element address can be easily encrypted by the user. Let’s say that the first element’s address, ie 0001, is placed in a pointer. If you increment the pointer, then the next element can be found. To illustrate this, a small program has been given as examples.
int main
int i = 0, roll [5] = {1,2,3,4,5};
int * x;
x = & roll; [0];
for (i = 0; i <5; i ++)
printf (”% d ”, x);
x = x + 2;
return 0;
Here, an integer variable has been initialized to run a loop five times. Then an array called Roll is declared and its value has been assigned as well. An integer type named “Nowi” has been declared a pointer. The function of this pointer is to point out the different elements of the array. Array’s first element address is defined as the value of the pointer after its declaration. So now if the pointer is directed to print, then the value of the roll [0] or the value of the first element of the array will be printed. Again, since the rotary array is an integer type, so each element of its element will be incremented by 2. So after printing the value of the pointer between a loop, its value has been incremented by 2. Thus, all the variables of the array are printed using pointer. It’s used to say arrays with pointers notation.

The user can ask, what is the need to use arrays with pointer notation. There is no problem with using direct arrays. Actually, the difference between a small program can not be understood by the above example. Because the use of pointers notation is actually at run time. Most computer architectures will work faster in pointer notation for example above. The way to calculate the computer is designed like this. Let’s say a little bit about this matter. The general rule of access to each element of an array is its base address to the address of that element.

For example, the address or array of the roll [0] is 0001. Now if the program is called the print of the roll [0], then it is not a problem. Because the program knows what the base address is. Now if the program is called the print of the roll [3], then the program will not be able to work as easily as before. Earlier, the program knew the address address is the address of the roll [0]. But the program does not know how much the address of the roll [3] To find this, the program will have to roll one element from roll [0] to roll [3], which is time consuming and requires runtime. But if accessing the array with pointer notation, the program does not have to travel all the roles [0] from the beginning and from the beginning, with the roll of [0] adding 3 * 2, the address of the roll [3] can be found and the program is directly rolled [3]

Here the 3 * 2 addition is followed by following a formula. Here 3 is the index number of the elements to access, and the reason given is that these are all integers type variables. They were to add 3 * 1 to the character type. This is not a matter of time for a small program. However, if you use this method for large programs, runtime will be reduced. Using this method takes less time to load or run the software. That is why these algorithms are considered as excellent.

About Admin

Leave a Reply

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