www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 21: Pointers and Arrays Bookmark and Share
Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
When we write int x, it means that we have attached a symbolic name x, at some memory location. Now we can use x = 10 which replaces the value at that memory location with 10. Similarly while talking about arrays, suppose an array as int y[10]. This means that we have reserved memory spaces for ten integers and named it collectively as y. Now we will see what actually y is? 'y' represents the memory address of the beginning of this collective memory space. The first element of the array can be accessed as y[0]. Remember arrays index starts from 0 in C language, so the memory address of first element i.e. y[0] is stored in y.

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

int y[10];
int *yptr;

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[3]; 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[0], y[1] 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.*/
#include<iostream.h>
main()
{
int y[10]; // an array of 10 integers
int *yptr; // an integer pointer
yptr = y; // assigning the start of array address to pointer

// printing the memory address
cout << “The memory address of yptr = “ << yptr << endl ;
yptr++; // incrementing the pointer
// printing the incremented memory address
cout << “The memory address after incrementing yptr = ” << yptr << endl;
}

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:
The memory address of yptr = 0x22ff50
The memory address after incrementing yptr = 0x22ff54

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[2], 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[0] ;

y[0] 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[1], &y[2] 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 */
#include <iostream.h>
main ()
{
int y[10] = {0,5,10,15,20,25,30,35,40,45};
int *yptr;
yptr = y; // Assigning the address of first element of array.
cout << “Accessing 6th element of array as y[5] = ” << y[5] << endl;
cout << “Accessing 6th element of array as *(yptr + 5) = ” << *(yptr + 5) << endl;
cout << “Accessing 6th element of array as yptr[5] = “ << yptr[5] << endl;
}
The output of the program is:
Accessing 6th element of array as y[5] = 25
Accessing 6th element of array as *(yptr + 5) = 25
Accessing 6th element of array as yptr[5] = 25

In the above example, there are two new expressions i.e. *(yptr+5) and yptr[5]. 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[5] gives us the 6th element of the array.



NEXT>>>>>Lesson 22. Pointer Comparison



Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.