Tuesday, July 14, 2009

Testing basic string array properties with pointer and addresses

In this program we unearth basic properties of character arrays.
Check the following program out….
Here inside the main() function we are in initializing the array with the string “rahul….”
The next two lines were basically written to indicate two facts:
· Whenever you try to print an array using the variable you get the base address of the array.
· Trying to print the base address using %d or %u doesn’t matter it gives the same output.
Then we are printing the address of last character of the array using &a[strlen(a)-1].
Then we are printing the length of the array using strlen() function.
Then we are accessing the content of the array using *&a[0] and *&a[strlen(a)-1].
Basically &variable_name means address of variable_name and
*address means value at that address.
So finally, *&variable = value at(address of variable) i.e, the variable value.

The following is the output of this program