addition/substraction of pointers while passing to a function

U

udayan.singh

Hi ,

tried this prog .. need a clarification..

--------------------------------------------------------------------------

[root@localhost chap02]# cat a3.c

#include <stdio.h>

void print(int *a, int *b, int *c, int *d , int *e)
{
printf("\n %d %d %d %d %d \n", *a,*b, *c, *d , *e);
}

int main()
{
static int arr[] = {97, 98, 99, 100, 101, 102, 103, 104};

int *ptr = arr + 1;

print(++ptr, ptr--, ptr, ptr++ , ++ptr);

return 0;
}


--------------------------------------------------------------------------
i expected the output to be (reason being that the computation takes
place from right extreme of the function - i think like that, might b i
m wrong)

100 100 100 99 99
--------------------------------------------------------------------------

but the actual output is :
100 100 100 99 100

--------------------------------------------------------------------------

pls explain!!!!!

tia,
Udayan
 
C

Clark S. Cox III

int *ptr = arr + 1;

print(++ptr, ptr--, ptr, ptr++ , ++ptr);
pls explain!!!!!

You are invoking undefined behavior. You are attempting to modify ptr 4
times without an intervening sequence point. Additionally, even if that
were not the case, you have no guarantee as to the order in which
function arguments are evaluated. The only guarantee that you have is
that they will all have been evaluated before the actual function call
is made.
 
M

Martin Ambuhl

print(++ptr, ptr--, ptr, ptr++ , ++ptr);

Doesn't anyone check the FAQ or follow the newsgroup before posting
their tired, wearisome, and flat-out erroneous crap here?
i expected the output to be

All possible expectations are wrong, but your incorrect expectation
_could_ happen.
 
U

udayan.singh

Well Thanks a lot :). Sorry for reposting a tierd, message again..
I would surely go through the mentioned list before posting again
here..

Regards,
Udayan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top