Wednesday, November 4, 2009

Manipulating Addresses

DIFFERENTIATING BETWEEN CHANGING ADDRESSES AND CHANGING VALUES
















1)For a pointer to an integer

void main()
{
int a=3;
int *b;
b=&a;
func(b);
printf("The output is %d",b);
}

func(int *b)
{
b=b+1;
}

For the above the answer is 4.

2)For this

void main()
{
int a=1,b=2;
int *aa,*bb;
aa=&a;
bb=&b;
printf("\nAddress at aa is %u, Address at bb is %u",aa,bb);

/*Lets say address at a is 001 and b is 002
We create a function to try and swap these addresses so aa has address b and bb has address a*/

fswap(&aa,&bb);
printf("\nValue at aa is %d, Address at aa is %u,\nValue at bb is %d, Address at bb is %u",*aa,aa,*bb,bb);
}

fswap(int **addressaa,int **addressbb)
{
int **tempcc;
*tempcc=*addressaa;
*addressaa=*addressbb;
*addressbb=*tempcc;
}

No comments:

Post a Comment