|Lessons||Lesson 21: Pointers and Arrays|
“The name of the array is a constant pointer which contains the memory address of the first element of the array”.
The difference between this and an ordinary pointer is that the array name is a constant pointer. It means that the array name will always point to the start of the array. In other words, it always contains the memory address of the first element of the array and cannot be reassigned any other address. Let's elaborate the point with the help of following example.
In the above statements, we declare an array y of ten integers and a pointer to an integer i.e. yptr. This pointer may contain a memory address of an integer.
yptr = y;
This is an assignment statement. The value of y i.e. the address of the first element of the array is assigned to yptr. Now we have two things pointing to the same place, y and yptr. Both are pointing to the first element of the array. However, y is a constant pointer and always points to the same location whereas yptr is a pointer variable that can also point to any other memory address.
Pointer Expressions and Arithmetic
Suppose we have an array y and yptr, a pointer to array. We can manipulate arrays with both y and yptr. To access the fourth element of the array using y, we can say y; with yptr, we can write as *(yptr + 3). Now we have to see what happens when we increment or add something to a pointer. We know that y is a constant pointer and it can not be incremented. We can write y, y etc. On the other hand, yptr is a pointer variable and can be written as the statement yptr = y. It means that yptr contains the address of the first element of the array. However, when we say yptr++, the value of yptr is incremented. But how much? To explain it further, we increment a normal integer variable like x++. If x contains 10, it will be incremented by 1 and become 11. The increment of a pointer depends on its data type. The data type, the pointer points to, determines the amount of increment. In this case, yptr is an integer pointer. Therefore, when we increment the yptr, it points to the next integer in the memory. If an integer occupies four bytes in the memory, then the yptr++; will increment its value by four. This can be understood from the following example.
/* This program will
print the memory address of a pointer and its incremented address.*/
// printing the memory
In the above program, the statement cout << yptr will show the memory address the yptr points to. You will notice the difference between the two printed addresses. By default, the memory address is printed in hexadecimal by the C output system. Therefore, the printed address will be in hexadecimal notation. The difference between the two addresses will be four as integer occupies four bytes and yptr is a pointer to an integer. “When a pointer is incremented, it actually jumps the number of memory spaces according to the data type that it points to”.
The sample out put
of the program is:
yptr which was pointing to the start of the array y, starts pointing to the next integer in memory after incrementing it. In other words, yptr is pointing to the 2nd element of the array. On being incremented again, the yptr will be pointing to the next element of the array i.e. y, and so on. We know that & is address operator which can be used to get the memory address. Therefore, we can also get the address of the first element of the array in yptr as:
yptr = &y ;
y is a single element and its address can be got with the use of. the address operator (&). Similarly we can get the address of 2nd or 3rd element as &y, &y respectfully. We can get the address of any array element and assign it to yptr. Suppose the yptr is pointing to the first element of the array y. What will happen if we increment it too much? Say, the array size is 10. Can we increment the yptr up to 12 times? And what will happen? Obviously, we can increment it up to 12 times. In this case, yptr will be pointing to some memory location containing garbage (i.e. there may be some value but is useless for us).
Here is an example describing different methods to access array elements.
/* This program contains
different ways to access array elements */
In the above example, there are two new expressions i.e. *(yptr+5) and yptr. In the statement *(yptr+5), yptr is incremented first by 5 (parenthesis are must here). Resultantly, it points to the 6th element of the array. The dereference pointer gives the value at that address. As yptr is a pointer to an integer, so it can be used as array name. So the expression yptr gives us the 6th element of the array.
NEXT>>>>>Lesson 22. Pointer Comparison