Sunday, September 26, 2010

Pointer Assignments in C,C++

As with any variable, you may use a pointer on the right-hand side of an assignment
statement to assign its value to another pointer. For example,
#include <stdio.h>
int main(void)
{
int x;
int *p1, *p2;
p1 = &x;
p2 = p1;
printf(" %p", p2); /* print the address of x, not x's value! */
return 0;
}
Both p1 and p2 now point to x. The address of x is displayed by using the %p printf()
format specifier, which causes printf() to display an address in the format used by the
host computer.

No comments:

Post a Comment