[LCP]query regarding pointers in C

Greg Black gjb at gbch.net
Fri Apr 16 09:59:02 UTC 2004


On 2004-04-15, sowmya.k wrote:

> pls tell me how these 2 pgms work
>  
> 1. #include<stdio.h>     main()
>    {
>      int a=555,*ptr=&a,b=*ptr;
>      printf("%d %d %d",++a,--b,(*ptr)++);
> }

Both of your programs are undefined, so any output they provide is
arbitrary and of no significance -- in other words, they don't
work at all.

This is a FAQ, so you should have found the answer already.  The
place to start with C questions is:

    http://www.eskimo.com/~scs/C-faq/top.html

Until you've mastered that, you should not be coming to places
like this.

To provide all the clues you need, read Section 6.3 and Section
6.3.2.2 of the C89 Standard.  The sentences of interest are:

    6.3 Expressions

	[...] Between the previous and next sequence point an
	object shall have its stored value modified at most once
	by the evaluation of an expression. [...]

    6.3.2.2 Function calls

        [...] The order of evaluation of the function designator,
	the arguments, and subexpressions within the arguments is
	unspecified, but there is a sequence point before the
	actual call. [...]

As you can see, in your code you modify the value of a more than
once between two sequence points.  The result is undefined and is
not required to make sense in any way at all.  Nor is the compiler
required to provide a diagnostic.

Cheers, Greg



More information about the linuxCprogramming mailing list