LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-30-2013, 10:50 PM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
pointer addition


Wondering if anyone can clarify something in a text I'm using to learn C (by s. Prata).
Regarding pointer addition, it states:
"You can use the + operator to add an integer to a pointer or a pointer to an integer."

Does this mean only that ptr1 + 3 is same as 3 +
ptr1, or is the author referring to something more like adding values to dereferenced pointers?
 
Old 08-31-2013, 12:26 AM   #2
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
No, the author is actually saying that you can add ints to pointers so ptr + 3 is the same as 3 + ptr.

It should be noted that it is not just adding 3 to a number but 3 * sizeof(data type) to ptr. So if ptr points to a data type that is 8 bytes long then ptr + 3 is really (int) ptr + 24 so that ptr + 3 is synonymous with &ptr[3].

(Thanks NevemTeve)

Last edited by psionl0; 08-31-2013 at 12:50 PM.
 
1 members found this post helpful.
Old 08-31-2013, 07:49 AM   #3
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
---so he is simply affirming the associativity of the addition operator regarding addition of integers to pointers?? If so, then looks like I was confused over a trivial matter!

Last edited by atlantis43; 08-31-2013 at 07:52 AM.
 
Old 08-31-2013, 09:00 AM   #4
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by atlantis43 View Post
---so he is simply affirming the associativity of the addition operator regarding addition of integers to pointers?? If so, then looks like I was confused over a trivial matter!
I think the word is "commutativity" but yes. In fact, I have read that ptr[3] is equivalent to 3[ptr] but I don't have a reference for that now.
 
1 members found this post helpful.
Old 08-31-2013, 09:56 AM   #5
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
(wasn't quite sure whether it was commutativity or associativity). Thanks again.
 
Old 08-31-2013, 12:14 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Note: quite often I write sg like &someptr[1] instead of someptr+1 just to emphasize the fact that it doesn't mean 1 byte, but sizeof(*someptr) bytes
 
1 members found this post helpful.
  


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
pointer to pointer segmentation fault Guilherme Programming 7 06-28-2009 09:47 AM
C - Base 4 addition charlitos Programming 2 04-16-2009 09:48 PM
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM

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

All times are GMT -5. The time now is 04:04 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