LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-12-2013, 10:47 PM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
pointer incrementing


Looking for explanation as to why, using pointer arithmetic, pointer can be incremented as ptr +1, but cannot be incremented as ptr++, as in the following code:
#include <stdio.h>

int main(void){

int N = 7;
int *ptr1;
int array[] = {10, 20, 30, 40, 50, 60, 70};
//ptr1 = array;
ptr1 = &array[0];

while(ptr1 < &array[N]){
printf("*ptr1 = %d\n", *ptr1); //this works fine
ptr1 = ptr1 +1;

printf("*ptr1 = %d\n", *ptr1); //this doesn't
ptr1 = ptr1++;
}

return 0;

}

Last edited by atlantis43; 06-12-2013 at 10:48 PM.
 
Old 06-12-2013, 10:59 PM   #2
Nauntilus
Member
 
Registered: Oct 2005
Distribution: All of them
Posts: 140

Rep: Reputation: 18
Did a quick google search, and I found this.

(One question that comes up is whether the expression *p++ increments p or what it points to. The answer is that it increments p. To increment what p points to, you can use (*p)++.)

http://www.eskimo.com/~scs/cclass/notes/sx10b.html
 
Old 06-12-2013, 11:31 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
ptr1 = ptr1++;
This is wrong, its result is compiler/platform/random-dependent. If you want to increment ptr1, pick one of the following three:

Code:
ptr1 = ptr1+1;
++ptr1;
ptr1++;
 
1 members found this post helpful.
Old 06-13-2013, 11:12 AM   #4
Nauntilus
Member
 
Registered: Oct 2005
Distribution: All of them
Posts: 140

Rep: Reputation: 18
NevemTeve is right. I didn't even catch that. You can't use ptr1 = ptr1++; and expect a correct answer because the ++ is essentially ptr1 = ptr1 + 1; so you would just want to use ptr1++; like NevemTeve suggests.

Another one you could use is:

Code:
ptr1 += 1;
Just remember to use what will make your code the most readable / maintainable.
 
2 members found this post helpful.
Old 06-13-2013, 11:28 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
NevemTeve gave you most of the answer. For the full answer read this:
http://c-faq.com/expr/seqpoints.html
especially the explanation and examples of the rule: "Between the previous and next sequence point an object shall have its stored value modified at most once"
 
1 members found this post helpful.
Old 06-13-2013, 07:13 PM   #6
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Smile

Thanks again to all of you. Those eskimo notes seem terrific to answer many of my questions without further dependence upon your time. (seems like many others have had similar questions to mine.)
If I read that seg on 'sequence point' 5 or 6 more times I may even get to really understand it!
 
Old 06-13-2013, 07:34 PM   #7
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 atlantis43 View Post
If I read that seg on 'sequence point' 5 or 6 more times I may even get to really understand it!
I don't know if that second rule is comprehensible:
"the prior value shall be accessed only to determine the value to be stored".

I understand what that rule is there to restrict, so I can fit the English phrase to the intent of the rule. But only because I already knew the intent of the rule. If you didn't know that intent some other way, I doubt you could parse it from that sentence:

a[i++] = 5;
Looks to me like the prior value of i is used as the index into a[], which that rule seems to prohibit. But it isn't really the prior value of i being used. It is the result of i++ (which happens to equal that prior value) being used.

The English could also be considered ambiguous regarding "the value to be stored" in an expression storing more than one value (correctly into different places). Once you know what it means, you can parse that English as meaning that. But in my opinion, it fails to communicate what it means.

The real intent is you can't have a read and a write of the same location in ambiguous sequence to each other. Almost everything between two sequence points is in ambiguous sequence, except where that is impossible, such as storing a value before computing it.
 
1 members found this post helpful.
Old 06-14-2013, 06:42 AM   #8
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Thanks again, JF. Your comment corroborates what I often say about programming texts, etc: "they are mostly written for those who already know programming". After my second (careful) reading of that thread, I at least now realize that statements such as i++; are, in themselves, a "full expression", which explains why my i = i++ makes no sense. That's certainly an improvement.
 
Old 06-14-2013, 07:02 AM   #9
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 atlantis43 View Post
statements such as i++; are, in themselves, a "full expression", which explains why my i = i++ makes no sense.
i++ can be a "full expression". It doesn't need to be. The following makes sense:
Code:
 x = i++;
In that, i++ is a sub expression, not a full expression.

The meaning of i++ makes the above statement equivalent to three steps:
A) temp = i
B) i = temp+1
C) x = temp
The meaning of i++ also requires that A occurs before B and that A occurs before C. But nothing about the language says whether B occurs before or after C.

If i and x are different variables, then the final result does not depend on the relative sequence of steps B and C.

If i and x were the same variable, then the result depends on that sequence. Once the result depends on that sequence, the optimization rules say the result is completely undefined. It is not just undefined which sequence steps B and C occur in. An optimizer might (and sometimes does) generate code that produces results that don't fit either sequence.

Last edited by johnsfine; 06-14-2013 at 07:03 AM.
 
1 members found this post helpful.
Old 06-14-2013, 07:16 AM   #10
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Yes, I get it. I only made my reply brief, but the FAQ was clear that something like
Code:
a = i++;
would also be a full expression that would be acceptable.
Glad you mentioned what a "sub-expression" is, 'cause that wasn't in their glossary.

Last edited by atlantis43; 06-14-2013 at 07:18 AM.
 
  


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
[SOLVED] BASH incrementing a variable Garrett85 Programming 3 01-12-2013 09:05 PM
C pointer strangely self incrementing into unkown value an1L Programming 14 03-07-2012 11:20 AM
variable not incrementing in c fakie_flip Programming 3 09-11-2010 12:25 PM
Thanked counter is not incrementing jaypas LQ Suggestions & Feedback 5 08-20-2010 05:56 PM

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

All times are GMT -5. The time now is 10:25 AM.

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