HELP ME OUT

0 Royson Mendonca · June 3, 2015
 I am currently working with arrays and pointers .

I do understand that it is not possible to modify an array.

eg:if i have an array a[4]={1,2,3,4,5}

: *a++ is not possible .But the same is possible with pointers as in * pa++

i am trying to run this piece of code 



int a[4]={1,6,3,4,5};

int * pa=a;

printf("%d ,%d ",*pa++,*pa);


return 0;
}


the output i am getting is 1  1

but when i am running this piece of code

printf("%d ",*pa++);

printf("%d ",*pa);

the output i am getting is 1 6

why is the increment not being done in the first example like it is just displaying a[0] in the first case

would be grateful for your help



Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · June 3, 2015
This is entirely theoretical, but honestly what you are attempting is undefined behavior. Please write code that is more clearly defined. You are attempting to read by reference while modifying by referencing instead of creating a temporary value. You shouldn't assume code like printf("%d ,%d ",*pa++,*pa); will work correctly.

It is unclear whether *pa should have its original value or the incremented value. 

The second piece of code suggest that ++ occurs after *pa occurs and the value substituted into %d.

What I am guessing at in the first case is that since *pa is being asked for twice, it isn't entirely sure which has higher precedence so it picked to change *pa.

Use parentheses for clarity. 
  • 1

C

106,996 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator