Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 179103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:20:52+00:00 2026-05-11T14:20:52+00:00

Consider this example: struct { int num; } s, *ps; s.num = 0; ps

  • 0

Consider this example:

struct {     int num; } s, *ps;  s.num = 0; ps = &s; ++ps->num;  printf('%d', s.num); /* Prints 1 */ 

It prints 1.
So I understand that it is because according to operators precedence, -> is higher than ++, so the value ps->num (which is 0) is firstly fetched and then the ++ operator operates on it, so it increments it to 1.

struct {     int num; } s, *ps;  s.num = 0; ps = &s; ps++->num;  printf('%d', s.num); /* Prints 0 */ 

In this example I get 0 and I don’t understand why; the explanation of the first example should be the same for this example. But it seems that this expression is evaluated as follows:
At first, the operator ++ operates, and it operates on ps, so it increments it to the next struct. Only then -> operates and it does nothing because it just fetches the num field of the next struct and does nothing with it.
But it contradicts the precedence of operators, which says that -> have higher precedence than ++.

Can someone explain this behavior?

Edit:
After reading two answers which refer to a C++ precedence tables which indicate that a prefix ++/-- operators have lower precedence than ->, I did some googling and came up with this link that states that this rule applies also to C itself. It fits exactly and fully explains this behavior, but I must add that the table in this link contradicts a table in my own copy of K&R ANSI C. So if you have suggestions as to which source is correct I would like to know.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:20:52+00:00Added an answer on May 11, 2026 at 2:20 pm

    The post-increment (ps++) and the pre-increment (++ps) have different associativity in C. The former associates left-to-right whereas the latter associates right-to-left. Check this page out (though this is for C++, so the precedences may be misleading).

    In your last example you are changing the pointer to one past the end of &s. You have not changed the value of the pointee. If you need to increment num, you need to bind the ++ to num.

    Detailed explanation:

     ps++->num; 

    A (hypothetical) compiler on seeing this expression, may push the ps object to the stack followed by the ++ operator, the -> operator and finally the object — num. While evaluationg, where should the compiler start? It looks at the available operators i.e. ++ and ->. Does it choose ps++ or ps? Here precedence rules: since -> has a higher precedence than ++, it takes -> to process with num as one operand and ps as the other operand. So, the value of the expression becomes ps->num i.e. 0 as you rightly observe. What happens to ps after the evaluation? Remember, there is another operator left on the stack? So, the compiler applies this to ps and it now points to one element past &s.

    Footnote:

    The ANSI/ISO C standard does not use a operator precedence grammar. Rather it uses what is known as a fully-factored grammar. This typically involves an exacting grammar definition dotted with a number of non-terminals like ‘primary-expression’ and ‘shift-expression’ and so on. This is hard to understand, but is easier for the language designer or the compiler vendor. Also, such a grammar is able to encode precedences easily. However, any fully-factored grammar is compatible with an operator-precedence grammar and this is what most books (and websites) do (and also mess up at times).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 202k
  • Answers 202k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Trying to validate the contents of a file based on… May 12, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer Ok, I was a bit trigger-happy with sending the question...… May 12, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer Is it not possible just to cast it: _bstr_t b("Steve");… May 12, 2026 at 8:21 pm

Related Questions

I have class foo that contains a std::auto_ptr member that I would like to
consider this simple and pointless code. #include <iostream> struct A { template<int N> void
Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e,
I was reading the C++0x FAQ by Stroustrup and got stuck with this code.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.