Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > addition/substraction of pointers while passing to a function

Reply
Thread Tools

addition/substraction of pointers while passing to a function

 
 
udayan.singh@gmail.com
Guest
Posts: n/a
 
      01-19-2007
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

 
Reply With Quote
 
 
 
 
Clark S. Cox III
Guest
Posts: n/a
 
      01-19-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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.


--
Clark S. Cox III
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-19-2007
(E-Mail Removed) wrote:

> 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.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      01-19-2007
(E-Mail Removed) wrote:

> tried this prog .. need a clarification..


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


> pls explain!!!!!


u shld use r3al wrds & nt txt msg spk. thn, r3ad teh FAQ:
http://c-faq.com/expr/evalorder1.html (r3ad 3.2 & 3.3 also). ur dbts
will b answ3rd!!!1!!1!!

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
 
Reply With Quote
 
udayan.singh@gmail.com
Guest
Posts: n/a
 
      01-20-2007
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

Christopher Benson-Manica wrote:
> (E-Mail Removed) wrote:
>
> > tried this prog .. need a clarification..

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

>
> > pls explain!!!!!

>
> u shld use r3al wrds & nt txt msg spk. thn, r3ad teh FAQ:
> http://c-faq.com/expr/evalorder1.html (r3ad 3.2 & 3.3 also). ur dbts
> will b answ3rd!!!1!!1!!
>
> --
> C. Benson Manica | I *should* know what I'm talking about - if I
> cbmanica(at)gmail.com | don't, I need to know. Flames welcome.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
passing member function pointers to a function tbringley@gmail.com C++ 18 02-26-2007 05:46 PM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
void pointers & void function pointers Peter Goddard C Programming 3 05-16-2005 09:44 PM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments