Member Avatar for I_m_rude

hi....

int x=30, *y,*z;

y=&x;    assume x address is 500 and int is of 4 bytes
z=y;

*y++=*z++;
x++;

printf("%d %d %d",x,y,z);

i want to ask that how the value of y is 504 here ?

firstly value of z is assigned to value of y. then z is incremented after assigment. then value of y is incremented after it get its value from *z. is it right what i am saying ? value of y will get new value or y(pointer) itself will get a new value. this question is confusing me alot. please explain.

thanks in advance.

When you say *p++, what really happens is *(p++). So you're evaluating to the value pointed to by p, and then incrementing the address stored in p. So let's break it down:

y = &x;

The address stored in y is 500 (going off your original stated assumption for simplicity).

z = y;

The address stored in z is 500. Now y and z both point to x.

*y++ = *z++;

The value stored in the object pointed to by z is copied into the object pointed to by y. After this happens, both y and z are incremented by 4 bytes (ie. the assumed size of an int). Note that the assignment is directly equiavelent to x = x, because both y and z point to the same address.

x++;

The value stored by x is incremented to 31.

printf("%d %d %d",x,y,z);

The output will be 31, 504, 504. All variables involved were incremented one time. For x that means the value 30 becomes 31, and for the two pointers, the increment skips the address value ahead by the number of bytes in the pointed to type. Since int is assumed to be 4 for this example, 500 becomes 504.

commented: excellent :) +14
> int x=30, *y,*z;
> y=&x;    assume x address is 500 and int is of 4 bytes
> z=y;     // y=500 and z=500 
> *y++=*z++; // since '=' has higher per precedence right hand side value is evaluated;                firstly evaluate left hand side just to make it simple.
*z=30 is assigned to *y++ and then the *z is incremented *z=31, the value of z=504
so *y++ holds value 30
now evaluate *y++
*y=30
*y++ => *y = *y+1
so the value *y=31
so the value of y=504 
 the main point here is *y++, in this step *y increments its value and stores in *y 

> x++; // x=31
> printf("%d %d %d",x,y,z);


 the values will 31,504,504

 I hope you got my point, in case of any doubts do repost, i will try to intrepet and let you know :)

But why does C consider *y++ to be an incremention to the adress of y? This only makes code more misleading. Is there a reasonable answer to this wierd operator precedence order?
And will (*y)++ work as expected?

Member Avatar for I_m_rude

@deceptikon what will u say to this statement ?

int *p,x=3;

int c=9;

p=&x;

*p++ = c;

how values are assigned/incremented in the last line ? I think i will get your point after this answer.

Member Avatar for I_m_rude

@phani92 will u plzz it more clear ? please!

But why does C consider *y++ to be an incremention to the adress of y?

That's just how the precedence rules work.

Is there a reasonable answer to this wierd operator precedence order?

I'm not aware of any rationale for it, though Dennis Ritchie wrote a history of C that might mention it. However, by my recollection the only mention of precedence in that history has to do with bitwise operators.

And will (*y)++ work as expected?

Yes.

@deceptikon what will u say to this statement ?

int *p,x=3;
int c=9;
p=&x;
*p++ = c;
how values are assigned/incremented in the last line ? I think i will get your point after this answer.

You're essentially overwriting x with 9, then incrementing p. Note that this increment is unsafe because pointer arithmetic is only meaningful inside an array or dynamically allocated block.

Member Avatar for I_m_rude

quite good link. any other ?

Member Avatar for I_m_rude

@decetikon

int (*p)[10];

in this, p is an pointer to an array of 10 integers. how can i use it now? means how to initaialize the array or how to print the values after writing values to it ? Can you please make me to visualize it(pointer to an array). ? please !
thanks.

You have a pointer, not an array. So you need to point that pointer to an array or dynamically allocate some memory to it before using it. Then, you need only remember to dereference the pointer before indexing it:

#include <stdio.h>
#include <stdlib.h>

void display(int (*p)[10])
{
    for (int i = 0; i < sizeof *p / sizeof **p; i++)
        printf("%-2d", (*p)[i]);
}

int main(void)
{
    int data[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    int (*p1)[10] = &data;              /* Use an existing array */
    int (*p2)[10] = malloc(sizeof *p2); /* Dynamically allocate */

    /* Initialize the dynamic array */
    for (int i = 0; i < sizeof *p2 / sizeof **p2; i++)
        (*p2)[i] = i + 1;

    /* And use the pointers */
    display(p1);
    putchar('\n');
    display(p2);

    return 0;
}
commented: Excellent!! +2
Member Avatar for I_m_rude

hmmm... nice one.. I got it..
hey, Can you give me link about the pragma directices ? I have searched www.google.com alot for this, but I am not getting what exaclty is the work of #pragma and why is it used ?

thanks. please help.

Pragmas are implementation-defined (barring a few standard ones starting with C99). Read your compiler's documentation for supported pragmas and what they do.

However, I'll give you an example of a somewhat common pragma: pragma once.

commented: link is damn good! +2
Member Avatar for I_m_rude

@decptikon implementaion defined, you mean that if i write any #pragma statement, then the work that it will do is not specified but it will work definitely. is this right ?

If the pragma (excluding a few standard pragmas starting with C99) is supported by your compiler, it must be documented and it must work as documented. But that same pragma need not be supported by a different compiler.

Member Avatar for I_m_rude

Sir, that link is simply awesome. i tried for 3 hours just to get that what exactly pragma is. uff! Can i have one more example of #pragma.
thanks in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.