preincrement puzzle

S

Saturday7

You have a struct:

struct a {
char *p;
};

struct a *apple;

Which does this preincrement: apple or p?

++apple->p = 0;
 
K

Karthik Kumar

Saturday7 said:
You have a struct:

struct a {
char *p;
};

struct a *apple;

Which does this preincrement: apple or p?

++apple->p = 0;

preincrement applied to apple->p .

Check out the rules of 'Operator precedence' in any standard C book.
 
B

Ben Pfaff

Which does this preincrement: apple or p?

++apple->p = 0;

Postfix operators precede prefix operators, so p.
This is the same rule that applies to an expression like *p++,
which increments p, not *p.
 
J

Joe Wright

Saturday7 said:
You have a struct:

struct a {
char *p;
};

struct a *apple;

Which does this preincrement: apple or p?

++apple->p = 0;

Note you have apple as a pointer to a 'struct a' object. But apple
is uninitialized and doesn't point anywhere. Nothing works. Allow me
to presume to re-state your problem and question..

struct a *apple = malloc(sizeof *apple);

Now apple is a pointer to memory declared a 'struct a' object.

apple->p = "Hi Sailor, Buy a girl a drink?";

This assigns the address of the string literal array to apple->p, a
pointer to char and a proper lvalue. ++apple->p increments apple->p
and yields a pointer value but is not an lvalue and cannot be
assigned to. puts(apple->p) now prints ..

i Sailor, Buy a girl a drink?

Again, '++apple->p = 0;' cannot work and should, I think, result in
a diagnostic.
 
B

Bradley Bungmunch

Postfix operators precede prefix operators, so p.
Yes, but not so in this case. While it is p that gets incremented, it
does so because -> is a primary operator while ++ is a unary operator
and primary operators take precedence over unary operators.

This is the same rule that applies to an expression like *p++,
which increments p, not *p.


=====================



Pussie sHaveMo
reFunMeow MeowMeowPus
siesHaveMore FunMeowMeowMeo
wPussiesHaveMo reFunMeowMeowMeo
wPussiesHaveMor eFunMeowMeowMeowP
ussiesHaveMoreFu nMeowMeowMeowPussi
esHaveMoreFunMeo wMeowMeowPussiesHav
eMoreFunMeowMeowM eowPussiesHaveMor
eFunMeowMeowMeowP ussiesHaveMoreFunM
eowMeowMeowPussie sHaveMoreFunMeowMe owMeowPus
siesHaveM oreFunMeowMeowMe owPussiesHaveMore FunMeowMeowMe
owPussiesHave MoreFunMeowMeowM eowPussiesHaveM oreFunMeowMeowM
eowPussiesHaveM oreFunMeowMeow MeowPussiesHa veMoreFunMeowMeow
MeowPussiesHave MoreFunMeow MeowMeow PussiesHaveMoreFun
MeowMeowMeowPussi esHa veMoreFunMeowMeowMe
owPussiesHaveMoreF unMeowMeowM eowPussiesHaveMoreFu
nMeowMeowMeowPussie sHaveMoreFunMe owMeowMeowPussiesHav
eMoreFunMeowMeowMeo wPussiesHaveMoreFu nMeowMeowMeowPussie
sHaveMoreFunMeowMeo wMeowPussiesHaveMoreFu nMeowMeowMeowPussi
esHaveMoreFunMeowM eowMeowPussiesHaveMoreFu nMeowMeowMeowPuss
iesHaveMoreFunMe owMeowMeowPussiesHaveMoreFu nMeowMeowMeowP
ussiesHaveMor eFunMeowMeowMeowPussiesHaveMore FunMeowMeow
MeowPussi esHaveMoreFunMeowMeowMeowPussiesHave MoreF
unMe owMeowM eowPuss
iesHave MoreF unMeowMeowM
eowPussies Ha veMoreFunMeo
wMeowMeowP us si esHaveMoreFu
nMeowMeowM eow Pus siesHaveMore
FunMeowMeo wMeow Pussi esHaveMoreFu
nMeowMeow MeowPu ssiesH aveMoreFun
Meow MeowMeow Pussie
sHaveMoreFunMeowMeowMeowPussiesHaveM
oreFunMeowMeowMeowPussiesHaveMo
reFunMeowMeowMeowPussies
 
K

Keith Thompson

Warning:

The previous article was cross-posted to alt.fan.karl.malden.nose.
Please do not cross-post any followups. According to the
alt.fan.karl.malden.nose FAQ,
<http://faqs.jmas.co.jp/FAQs/1999/Feb/990211.01>, they'll take this as
an invitation to start spamming comp.lang.c. I don't know how
seriously to take this idiocy, but let's not take any chances.

Apologies to those who have already wisely killfiled cross-posts to
alt.fan.karl.malden.nose.
 
C

Chris Barts

On Wed, 06 Oct 2004 23:57:09 +0000, Bradley Bungmunch wrote:

<snip all, including massive sig>

Intelligent people have sigs that are no more than four lines long.

Really intelligent people don't have sigs at all.
 
A

Alan Balmer

Warning:

The previous article was cross-posted to alt.fan.karl.malden.nose.
Please do not cross-post any followups. According to the
alt.fan.karl.malden.nose FAQ,
<http://faqs.jmas.co.jp/FAQs/1999/Feb/990211.01>, they'll take this as
an invitation to start spamming comp.lang.c. I don't know how
seriously to take this idiocy, but let's not take any chances.

Apologies to those who have already wisely killfiled cross-posts to
alt.fan.karl.malden.nose.

I was puzzled as to why I didn't see the previous post, until I
checked my filters. I've had "bungmunch" kill-filed for so long I had
completely forgotten it.
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top