Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
Practical Paper
Industrial Training
Pointers and arrays-1
Looking at the figure 2, this would turn out to be 6004 and 0. When you execute the program, the address may turn out to
be something other than 6004, but the value at the address would be surely 0.
Now look at the second printf( ) .
printf ( "\n%u %u %d", p, *p, **p ) ;
Here p would give the base address of the array p[ ] , i.e. 9016; *p would give the value at this address, i.e. 6004; **p
would give the value at the address given by *p , i.e. value at address 6004, which is 0. Now onto the last printf( ) .
printf ( "\n%u %u %d", ptr, *ptr, **ptr ) ;
Here ptr contains the base address of the array p[ ] , i.e. 9016; *ptr would give the value at this address, i.e. 6004; **ptr
would give the value at the address given by *ptr , i.e. value at address 6004, which is 0.
What would be the output of following program
main( )
{
static int a[ ] = { 0, 1, 2, 3, 4 } ;
static int *p[ ] = { a, a + 1, a + 2, a + 3, a + 4 } ;
int **ptr = p ;
ptr++ ;
printf ( "\n%d %d %d", ptr - p, *ptr - a, **ptr ) ;
*ptr++ ;
printf ( "\n%d %d %d", ptr - p, *ptr - a, **ptr ) ;
*++ptr ;
printf ( "\n%d %d %d", ptr - p, *ptr - a, **ptr ) ;
++*ptr ;
printf ( "\n%d %d %d", ptr - p, *ptr - a, **ptr ) ;
}
Output
1 1 1
2 2 2
3 3 3
4 4 4
Explanation
Figure 3 would go a long way in helping to understand this program.
Here ptr has been declared as a pointer to an integer pointer and assigned the base address of the array p[ ] , which has
been declared as an array of pointers. What happens when ptr++ gets executed? ptr points to the next integer pointer in
the array p[ ] . In other words, now ptr contains the address 9018. Now let us analyse the meaning of ptr - p , *ptr - a
and **ptr .
ptr - p
Since ptr is containing the address 9018, we can as well say that ptr is containing the address given by p + 1 . Then pt
r - p is reduced to ( p + 1 - p ) , which yields 1.
*ptr - a ;
*ptr means value at the address contained in ptr . Since ptr contains 9018, the value at this address would be 6006. Now
6006 can be imagined as ( a + 1 ) . Thus the expression becomes ( a + 1 - a ) , which is nothing but 1.
**ptr
ptr contains 9018, so *ptr yields 6006, and hence **ptr becomes *( 6006 ) , which yields 1.
Thus the output of thefirst ; printf( ) becomes 1 1 1.
Take a deep breath and then begin with the analysis of *ptr++ . Here * and ++ both are unary operators. Unary operators
have an associativity of right to left, hence ++ is performed before * . ++ increments ptr such that ptr now contains
9020. Then *( 9020 ) is performed, which gives the value at 9020. But since this value is not assigned to any variable, it
just gets ignored. Now with ptr containing 9020, let us once again analyse the expressions ptr - p , *ptr - a and **ptr .
Figure 3.
ptr - p
Since ptr contains 9020, it can be visualised as ( p + 2 ) . Thus ptr - p would become ( p + 2 - p ) , which gives 2.
*ptr - a
*ptr would give value at address 9020, i.e. 6008, which is nothing but the address given by a + 2 . Thus the expression becomes ( a + 2 - a ) , which gives 2.
**ptr
*ptr gives the value at address 9020, i.e. 6008, and *( 6008 ) gives the value at 6008, i.e. 2.
I hope your confidence is building and you are ready to meet head on the expression *++ptr . Here, since ++ precedes ptr
, firstly ptr is incremented such that it contains the address 9022, and then the value at this address is obtained. Since
the value is not collected in any variable, it gets ignored. Now having cooked enough pointer stew you can easily imagine
that the output of the third printf( ) would be 3 3 3.
Finally, let us understand the expression ++*ptr . Here obviously, the priority goes to the * . Thus, this expression increments the value given by *ptr . Since ptr contains 9022, *ptr gives value at 9022, i.e. 6010. This value is incremented to 6012.
So p[3] now contains 6012, whereas value of ptr remains stationary at 9022. Let us now analyse the expressions ptr - p , *ptr - a and **ptr .
ptr - p
ptr contains 9022, therefore ptr can be imagined as ( p + 3 ) . Thus ( ptr - p ) becomes ( p + 3 - p ) , which yields 3.
*ptr - a
*ptr yields 6012 which can be thought of as ( a + 4 ) . Thus the expression is reduced to ( a + 4 - a) , which yields 4.
**ptr
*ptr yields 6012, therefore **ptr would yield the value at *ptr , or the value at 6012, which is >4.