Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Incrementing int passed by reference
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 21st May 05:38
ian bell
External User
 
Posts: 1
Default Incrementing int passed by reference



I am passing the address of an int into a function. I want to increment the
int. Code snippet

void foo(int * pc)
{
*pc++;
}

pc does not get incremented. However if I do this:

foor(int * pc)
{
int temp;

temp = *pc;
temp++
*pc = temp;
}

then pc is incremented.

Is this a funny precedence thing with *?

Ian
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote


  sponsored links


2 21st May 05:38
jack klein
External User
 
Posts: 1
Default Incrementing int passed by reference



On 25 Nov 2003 04:37:48 GMT, Ian Bell

C doesn't actually have precedence, but it does have a parse sequence
defined by the grammar, and yes this is the reason.

The expression *p++, where p is a pointer to come complete type,
evaluates the value of what p is currently point to, then increments
the pointer p, not the pointed to object.

Replace this with (*p)++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote
3 21st May 05:38
dave neary
External User
 
Posts: 1
Default Incrementing int passed by reference


On 25 Nov 2003 04:37:48 GMT, Ian Bell said:

This increments the pointer pc, but evaluates to the value pointer to by
the old value (++ binds tighter than *).
(*pc)++;
will work.

Apart from the fact that it's not that funny, yes

Cheers,
Dave.

--
David Neary,
E-Mail: bolsh at gimp dot org
Work e-mail: d dot neary at phenix dot fr
CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote
4 21st May 05:38
andy sinclair
External User
 
Posts: 1
Default Incrementing int passed by reference


It is. '++' has a very high precedence.
In fact ++ and * (indirection) have the same precedence but right to
left associativity. Therefore the post increment gets applied to the
pointer rather than the location and the statement has no effect.

See K&R2 p53 or http://www.difranco.net/cop2220/op-prec.htm for an
operator precendence table.

Changing to one of the following gives the expected results.
(*pc)++;
*pc+=1;

Regards
Andy
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote
5 1st June 00:23
paolo montegriffo
External User
 
Posts: 1
Default Incrementing int passed by reference


void foo(int *pc)
{
(*pc)++ ;
}

Unary operators like * and ++ are evalued from right to left, i.e. postfix
operator ++ has the precedence over the prefix unary operator *.

see K&R Sec 5.1 or look at C F.A.Q. - Question 4.3

Cheers
Paolo
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote
6 1st June 00:23
hans-bernhard broeker
External User
 
Posts: 1
Default Incrementing int passed by reference


Yes. I'll leave it as an exercise to you to find out how to fix up the
precedence. ;->

--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote
7 1st June 00:23
francis glassborow
External User
 
Posts: 1
Default Incrementing int passed by reference


In article <clcm-20031124-0014@plethora.net>, Ian Bell
<ian@ruffrecordsDOTworldonline.co.uk> writes


Depends on your definition of 'funny'. Did you try:

void foo(int *pc){
(*pc) ++;
}


--
Francis Glassborow ACCU
If you are not using up-to-date virus protection you should not be reading
this. Viruses do not just hurt the infected but the whole community.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote
8 1st June 00:23
lndresnick
External User
 
Posts: 1
Default Incrementing int passed by reference


unary * and ++ have the same precedence, however unary operators
associate
from right to left. You are thus incrementing the pointer in foo.
You want (*pc)++, or ++*pc, or *pc += 1.

-David
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote
9 1st June 00:23
mark gordon
External User
 
Posts: 1
Default Incrementing int passed by reference


On 25 Nov 2003 04:37:48 GMT

It is a standard precedence and is generally useful since it is more
common for you to want to increment a pointer than the value it points
to.

To achieve what you want without a temporary use parenthesis. E.g.
(*pc)++
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote
10 1st June 00:24
dan.pop
External User
 
Posts: 1
Default Incrementing int passed by reference


In <clcm-20031124-0014@plethora.net> Ian Bell <ian@ruffrecordsDOTworldonline.co.uk> writes:

This is not the same thing as passing by reference (which is not supported
by C at all).

(*pc)++;

would be enough.

What does your C book have to say on the topic?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666