473,324 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

preincrement puzzle

You have a struct:

struct a {
char *p;
};

struct a *apple;

Which does this preincrement: apple or p?

++apple->p = 0;
Nov 14 '05 #1
7 2403
Saturday7 wrote:
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.

--
Karthik.
http://akktech.blogspot.com .
Nov 14 '05 #2
u9***@yahoo.com (Saturday7) writes:
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.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Nov 14 '05 #3
Saturday7 wrote:
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.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #4
On Mon, 04 Oct 2004 17:42:52 -0700, Ben Pfaff <bl*@cs.stanford.edu>
had a scratch and wrote:
u9***@yahoo.com (Saturday7) writes:
Which does this preincrement: apple or p?

++apple->p = 0;
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
Nov 14 '05 #5
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
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.

Nov 14 '05 #7
On Thu, 07 Oct 2004 00:31:57 GMT, Keith Thompson <ks***@mib.org>
wrote:
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.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
7
by: Huck Phin | last post by:
OK, so I have looked and looked for something similar to my problem, and I cannot find one. I am writing a program for a HugeInteger, and I am attempting to overload the preincrement,...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
5
by: dmf1207 | last post by:
Hi All! I'm new to javascript and need a little help with a simple puzzle im trying to design. I have a 600x100 pixel picture that I have sliced into 6 100x100 rectangles making a table of of 6...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.