LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2013, 06:33 AM   #1
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Understanding C++ operator precedence


Hi folks,

When I develop s/w, I typically try to avoid ambiguous statements like the following due to possible issues with operator precedence.

Code:
if (--ptr->count == 0)
{
    ...
}
After referring to the following document, I'm still unclear as to whether the pre-decrement operator or the pointer-dereference operator have the same precedence(when reading the code left-to-right), or if the decrement operator has precedence over the pointer dereference operator.

I'm debugging some code (for work-related purposes), and my application is seg-faulting at a line very similar to what I have described above. Can someone please clarify my doubts about operator precedence?

My gut feeling is that the code should have written as:
Code:
if (--(ptr->count) == 0)
{
    ...
}

Last edited by dwhitney67; 09-19-2013 at 06:50 AM.
 
Old 09-20-2013, 04:37 AM   #2
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
right to left, so it should effect count and not the pointer.

but put braces around as you did, it makes it easier for everyone
 
Old 09-20-2013, 04:56 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Original Poster
Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by a4z View Post
right to left, so it should effect count and not the pointer.

but put braces around as you did, it makes it easier for everyone
Thank you.
 
Old 09-20-2013, 05:44 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by dwhitney67 View Post
After referring to the following document, I'm still unclear as to whether the pre-decrement operator or the pointer-dereference operator have the same precedence(when reading the code left-to-right), or if the decrement operator has precedence over the pointer dereference operator.
Read that document more carefully. Notice the difference between Suffix and Prefix operators. The document clearly shows the prefix operators are processed after the dereference.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I make precedence among two routes ... itrilok Linux - Networking 1 02-23-2013 09:14 PM
Virtio Balloon- Does -m take precedence? dman777 Linux - Virtualization and Cloud 0 10-11-2011 09:23 PM
[SOLVED] C++ Operator Overloading Within an Already Overloaded Operator mirlin510 Programming 8 04-17-2011 12:02 PM
Simple Operator Precedence C++ Problem rovitotv Programming 9 03-21-2005 10:02 PM
operator precedence in C Majjj Programming 2 07-07-2004 11:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration