Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Pointers (http://www.go4expert.com/forums/pointers-t3357/)

swapnaoe 12Mar2007 10:37

Pointers
 
Hi ALL,
When I run the follwoing code, I get the follwoing
1245048
1245044
1245052
6
I tried changing the value of 'a' and the last line of the code, the printf prints whatever is assigned to 'a'. Please explain me this behaviour.
Code:

void main()
{
        int a=6;
        int b=1024;
        int *swap=(&b-1);
        printf("%u\n",&b);
        printf("%u\n",swap);
        swap+=2;
        printf("%u\n",swap);
        printf("%d\n",*swap);
}

Regards,
Swapna

shabbir 12Mar2007 12:04

Re: Pointers
 
You are printing the address of b which is in your system 1245048 and swap which is 4 bytes less which should be 1245044 and swap after adding 2 should be 1245044 + 2 = 1245052 which is what is expected.

swapnaoe 12Mar2007 12:09

Re: Pointers
 
Yes u r right.. then
what is *swap in the last line???
The value present in 1245052 at that point of time.. right???
If u can run the program in ur system.. u can find that, the value is nothing but 'a', in this case 6.
If u change the value of 'a' then the output also changes..
Any idea fro this type of behaviour??

DaWei 12Mar2007 17:04

Re: Pointers
 
swap is set to the address of the integer before b (&b minus 1), in this case, a. Therefore, when you print what swap points to (a), you get the contents (a). This only works because your machine happens to have a common implementation of the stack. There is no requirement of the language that requires it to be so.

swapnaoe 13Mar2007 10:27

Re: Pointers
 
Mr DaWei
Let us consider the follwoing example:
&a=1245044
&b=1245048
Now,
swap=&b-1, swap=1245044(According to ur explanation)
swap+=2, swap=swap+2, swap=1245052(if int occupies 4 bytes)
*swap= contents(1245052), swap= unknown value not 'a'.
Please correct me if i am wrong

Regards,
Swapna

wrecker 13Mar2007 11:45

Re: Pointers
 
Quote:

Originally Posted by swapnaoe
Hi ALL,
When I run the follwoing code, I get the follwoing
1245048
1245044
1245052
6
I tried changing the value of 'a' and the last line of the code, the printf prints whatever is assigned to 'a'. Please explain me this behaviour.
Code:

void main()
{
        int a=6;
        int b=1024;
        int *swap=(&b-1);
        printf("%u\n",&b);
        printf("%u\n",swap);
        swap+=2;
        printf("%u\n",swap);
        printf("%d\n",*swap);
}

Regards,
Swapna



Quite simple, should be no confusion. Just pointer manipulation. Understand whats actually procedure of pointer storage and you will make it. Or take a K&R and make your task simple.....

swapnaoe 13Mar2007 11:49

Re: Pointers
 
Wrecker,
Could you please go through the discussion i had with Dawei!!!
Swapna

DaWei 13Mar2007 15:06

Re: Pointers
 
It's almost certainly the case that your system is implemented in such a way that locals and automatic variables are allocated on the stack, and this in a system, such as the x86, where the stack grows downward. Presuming that 'a' is located at 1245412, in your case, then the variables are at the following addresses:
Code:

1245412 a        6
1245408 b        1024
1245404 swap        1245404 (&b - 1*sizeof int)

swap then becomes, after += 2*sizeof int, 1245404 + 8, 1245412, which is the address of a
when dereferenced, then, the value of a (6) is printed. Your number, 1245052, seems to be a typo.

Again, this is not only implementation dependent, hardware-wise, but also, software-wise. Exercises like this are only useful when produced on the same system with the same OS and the same compiler. Just a caveat.

swapnaoe 14Mar2007 14:53

Re: Pointers
 
Thank you guys.. its clear now.. :)
Swapna


All times are GMT +5.5. The time now is 17:51.