I assume that you are already familiar with arrays. But while we are discussing pointers, I'd like to go through it once again. In short, an array is a list of objects. Basically, it is a pointer. An array is a special sort of pointers that can point to more than one objects. If you have the address of the first element, you can access the rest of the elements as well. Think of pointer as an Address Card. In this case, let that you rent a number of flats, say 5, that are consecutive. The owner gives you the address of the first one, since the next ones are next to it consecutively, you don't have to get their addresses separately, instead you can move one door to reach the next flat.
int arr[5] = {6,8,9,4,5};
Now, arr is the pointer to the first element but it is an address, if you cout it then you'll get an address displayed. I'm sure you all are already familiar with the way of accessing array element with [ ] e.g. arr[0], arr[1] etc.
However, there are other ways as well. As mentioned earlier, arr points to the first object. You can reference it using
cout << *arr; //it prints the first element i.e. 6
cout << arr; //this however prints an address.
Note the difference, using * has the same effect as [ ]. When you put *, it goes to the address stored in arr and prints the data in it while without it, it simply prints what is stored in arr i.e. the address. (arr is a pointer just like int* or char*, it has the address to data.) Similarly,
cout << * (arr + 0); //prints first element i.e. 6
In this one, we command the program to go to arr and move 0 steps onward and with * print the data there which is the first element. ( *(arr+0) == arr[0] == *arr)
Likewise,
cout << *(arr+1); //prints second element i.e. 8, same as arr[1]
cout << *(arr+4); //prints the last one i.e. 5, same as arr[4]
NOTE :
Do not mix up *(arr+1) and *arr+1 or *(arr)+1.
*arr+1 == *(arr) +1 != *(arr+1)
cout << *arr+1; //prints 7 i.e 6+1
The above command says that first go to arr address then * takes us to the data stored on this address here i.e. 6 in this case and then add 1 to it hence we get 6+1 = 7. (Using *(arr)+1 is the same as this one. )
cout << *(arr)+4; //prints 10 i.e. 6+4
Hence, be careful with the sequence.
cout << (arr+2); //prints the address of the 3rd element. In order to print the element, use * with it.
cout << &arr[3]; //prints the address of the 4th element due to &
Same goes one with the float, doubles arrays.
Difference of Character Array :
A character array, however, is a bit different because it prints differently. When you give print command, it automatically moves till the NULL character is reached and you don't have to print each element separately.
char c_arr[5] = {'a','b','c','d', '\0'};
cout << c_arr; //prints abcd
To access each element separately;
cout << *(c_arr+1); //print 'b' i.e. the 2nd element
cout << c_arr+1; //prints 'bcd'
The last command prints bcd i.e. it starts from the second character and moves till NULL automatically. With this, we command the program to go to the element stored in c_arr and then move 1 step ahead i.e. 2md element b then begins printing till NULL is reached.
cout << c_arr+2; //prints 'cd'
Tip : Try a number of different commands to get over this idea of arrangement. Do practice. ^^
If you have any questions then please comment below. Thank you! :)
0 comments:
Post a Comment