Sunday, May 29, 2016

Pointers - II


We have discussed pointers pointing to different data types. Now Let us deal with void pointers.

Void pointers- Void pointers are those pointers which can store memory address values for variables of all the data types. In other words, if you declare a void pointer, then you can point it to the address of a character variable, an integer variable, a float variable or a user-defined data type variable such as struct .

Let us understand this with an example :-

 
In the above code, void pointer 'p' is made to point to each and every variable of different data types. We will now see how easily it just gives us the address location of each and every variable without throwing any “incompatible pointer type” warning , which normally appears when pointer of some data type points to address location of a variable of a different data type. This warning will never appear when working with a void pointer.



So far, so good. Now let us deal with a few more things. When we can obtain the address location of a particular variable, then we should also be able to access the value on that location.

In short, When we can reference a void pointer, then we should be able to dereference it too. Can't we?? Ok, let's see. We will take a short program and see whether it is possible or not.


Let us try compiling it .


As seen in the snapshot above, it throws error. Then, What's the solution to it? How to dereference the pointer to read the value at that location?

This can be done by typecasting the pointer to the data type which this pointer is pointing to.
In the above program, we can access the value of that location by typecasting 'p' to 'char'.
 
In the above program, the pointer 'p' has been typecast to a char pointer.

Syntax for typecasting a pointer is – *((data_type*) pointer)

Let us try compiling it and checking the value .

 
So, the program builds successfully and when the binary file is executed, we get the correct value at the location pointed by 'p'.
In the above code, we can also typecast 'p' to int pointer and get the correct expected result by dereferencing. It is because 'int' stores 4 Bytes and we are pointing to a char variable which is only 1 Byte.


Let us see the output.


 
No difference in the output.


But it's not the other way round. Let us understand it with the following example :-

We will store a 32-bit hexadecimal value in an 'int' variable and then try to dereference it with a pointer typecast to 'char'.


 
Let us observe the output.

 
Some of you might have been able to guess it right as to why we didn't get the desired output.

It's simply because 'char' only can only store upto 1 byte.

Since our value was 0xabcd4030 and 'char' only stores 1 Byte,Why did we get '30' as the value and why not 'ab'?

The answer to this question lies in a concept called endianness, which varies from architecture to architecture. This machine is a Little Endian. So we got '30' as the output. If you build and execute the same program on a machine with Big Endian format, you will get 'ab' as the output.

We will discuss endianness in detail in a later section.

<<<ptr1-ptr2, if both int, will give the number of locations(and not Bytes) both the pointers are apart from each other based on the data type.>>>
This will be illustrated more in a later section.



No comments:

Post a Comment