448,691 Members | 1,123 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,691 IT Pros & Developers. It's quick & easy.

# Valid operations on pointers in C

 P: n/a Why division/mulitiplication/addition of pointers are not allowed in C? Nov 14 '05 #1
32 Replies

 P: n/a aruna scribbled the following: Why division/mulitiplication/addition of pointers are not allowed in C? First define how it would work, and then we'll see why it should or should not be allowed. -- /-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "The obvious mathematical breakthrough would be development of an easy way to factor large prime numbers." - Bill Gates Nov 14 '05 #2

 P: n/a "aruna" wrote in message news:a2**************************@posting.google.c om... Why division/mulitiplication/addition of pointers are not allowed in C? Those operations have no obvious meaning, so there is no need. Nov 14 '05 #3

 P: n/a -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 aruna wrote: | Why division/mulitiplication/addition of pointers are not | allowed in C? A pointer can be considered to be the 'location' of a data item. Subtraction of one pointer from another gives you the 'distance' between two data items. What would addition of two pointers give you? (Hint: What does the addition of '12 Oak Lane' to '757 Main Street West' give you?) What would multiplication of two pointers give you? (Hint: What does the multiplication of '12 Oak Lane' with '757 Main Street West' give you?) What would division of two pointers give you? (Hint: What does the division of '12 Oak Lane' by '757 Main Street West' give you?) While pointer subtraction is meaningful, the other operations are not. - -- Lew Pitcher, IT Consultant, Enterprise Application Architecture Enterprise Technology Solutions, TD Bank Financial Group (Opinions expressed here are my own, not my employer's) -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (MingW32) iD8DBQFAiSyFagVFX4UWr64RAo1XAKC8YTdVBF0bWGf93dNiVV zWrhMhsQCeOszA RKCqbpu4N77+X+8EE5VcwEk= =+jwy -----END PGP SIGNATURE----- Nov 14 '05 #4

 P: n/a Lew Pitcher wrote: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 aruna wrote: | Why division/mulitiplication/addition of pointers are not | allowed in C? A pointer can be considered to be the 'location' of a data item. Subtraction of one pointer from another gives you the 'distance' between two data items. What would addition of two pointers give you? (Hint: What does the addition of '12 Oak Lane' to '757 Main Street West' give you?) What would multiplication of two pointers give you? (Hint: What does the multiplication of '12 Oak Lane' with '757 Main Street West' give you?) What would division of two pointers give you? (Hint: What does the division of '12 Oak Lane' by '757 Main Street West' give you?) While pointer subtraction is meaningful, the other operations are not. - -- Lew Pitcher, IT Consultant, Enterprise Application Architecture Enterprise Technology Solutions, TD Bank Financial Group One can add a scalar quantity to a pointer to a new location. For example, "Move 3 houses north of 12 Oak Lane", makes sense. If two pointers can be subracted for a distance then a distance should be added from one pointer to generate a new pointer value. -- Thomas Matthews C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt C++ Faq: http://www.parashift.com/c++-faq-lite C Faq: http://www.eskimo.com/~scs/c-faq/top.html alt.comp.lang.learn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html Other sites: http://www.josuttis.com -- C++ STL Library book Nov 14 '05 #5

 P: n/a aruna wrote: Why division/mulitiplication/addition of pointers are not allowed in C? Using right forefinger, point to nose. Using left forefinger, point to right ear. Now define the product, sum, ratios of those pointers. -- Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net) Available for consulting/temporary embedded and systems. USE worldnet address! Nov 14 '05 #6

 P: n/a Thomas Matthews scribbled the following: Lew Pitcher wrote: aruna wrote: | Why division/mulitiplication/addition of pointers are not | allowed in C? A pointer can be considered to be the 'location' of a data item. Subtraction of one pointer from another gives you the 'distance' between two data items. What would addition of two pointers give you? (Hint: What does the addition of '12 Oak Lane' to '757 Main Street West' give you?) What would multiplication of two pointers give you? (Hint: What does the multiplication of '12 Oak Lane' with '757 Main Street West' give you?) What would division of two pointers give you? (Hint: What does the division of '12 Oak Lane' by '757 Main Street West' give you?) While pointer subtraction is meaningful, the other operations are not. One can add a scalar quantity to a pointer to a new location. For example, "Move 3 houses north of 12 Oak Lane", makes sense. If two pointers can be subracted for a distance then a distance should be added from one pointer to generate a new pointer value. Where did anyone mention a scalar quantity? -- /-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "You could take his life and..." - Mirja Tolsa Nov 14 '05 #7

 P: n/a In ar********@yahoo.co.in (aruna) writes: Why division/mulitiplication/addition of pointers are notallowed in C? Try figuring out some *useful* semantics for these operations and you may understand why. Dan -- Dan Pop DESY Zeuthen, RZ group Email: Da*****@ifh.de Nov 14 '05 #8

 P: n/a "CBFalconer" wrote in message news:40***************@yahoo.com... aruna wrote: Why division/mulitiplication/addition of pointers are not allowed in C? Using right forefinger, point to nose. Using left forefinger, point to right ear. Now define the product, sum, ratios of those pointers. This analogy is lost on me. If I subtract one of those pointers from the other, then what body part would I be pointing to now? -- Tim Hagan Nov 14 '05 #9

 P: n/a Tim Hagan scribbled the following: "CBFalconer" wrote in message news:40***************@yahoo.com... aruna wrote: > Why division/mulitiplication/addition of pointers are not > allowed in C? Using right forefinger, point to nose. Using left forefinger, point to right ear. Now define the product, sum, ratios of those pointers. This analogy is lost on me. If I subtract one of those pointers from the other, then what body part would I be pointing to now? None. But you have the distance between your nose and your right ear. C works like that too. Now define the product, sum and ratio of those pointers. Either in real-world or C terms. -- /-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "B-but Angus! You're a dragon!" - Mickey Mouse Nov 14 '05 #10

 P: n/a Joona I Palaste wrote: [...] This analogy is lost on me. If I subtract one of those pointers from the other, then what body part would I be pointing to now? None. But you have the distance between your nose and your right ear. C works like that too. Now define the product, sum and ratio of those pointers. Either in real-world or C terms. The closest thing I can come up with (in real-world, but not C) is (ptr1+ptr2)/2 for the average. (ie: what is midway between your nose and right ear.) Of course, this is really the average of two _points_ and not two _pointers_. And, in C, this can be done (assuming that ptr1 and ptr2 are such that their difference is defined) with "ptr1 + ( (ptr2-ptr1) / 2 )", which is more meaningful in a sense. (ie: start here and go halfway to there.) -- +-------------------------+--------------------+-----------------------------+ | Kenneth J. Brody | www.hvcomputer.com | | | kenbrody at spamcop.net | www.fptech.com | #include | +-------------------------+--------------------+-----------------------------+ Nov 14 '05 #11

 P: n/a Addition / subtraction is definitely allowed, the reason being pointers refer to address and you can back and forth in the memory space. (Say, as in an array as the memory is allocated continuously ). Multiplication / Division just does not make sense, as there is no necessity for them logically in a given program. HTH aruna wrote: Why division/mulitiplication/addition of pointers are not allowed in C? Nov 14 '05 #12

 P: n/a In article , ar********@yahoo.co.in (aruna) wrote: Why division/mulitiplication/addition of pointers are not allowed in C? Tell us what the result would be. Nov 14 '05 #13

 P: n/a aruna wrote: Why division/mulitiplication/addition of pointers are not allowed in C? Pointer values are addresses. As such they are similar to postal zip codes or telephone numbers. Of what possible value would be the division, multiplication or addition of two or more zip codes? -- Joe Wright mailto:jo********@comcast.net "Everything should be made as simple as possible, but not simpler." --- Albert Einstein --- Nov 14 '05 #14

 P: n/a Rakesh Kumar scribbled the following: Addition / subtraction is definitely allowed, the reason being pointers refer to address and you can back and forth in the memory space. (Say, as in an array as the memory is allocated continuously ). Addition of two pointers is not allowed, addition of a pointer and a scalar is. -- /-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "I am not very happy acting pleased whenever prominent scientists overmagnify intellectual enlightenment." - Anon Nov 14 '05 #15

 P: n/a On Fri, 23 Apr 2004 07:28:43 -0700, aruna wrote: Why division/mulitiplication/addition of pointers are not allowed in C? This was covered back in January. See: - Rob -- (to email me, remove "warez.") Nov 14 '05 #16

 P: n/a [top-post rearranged] aruna wrote: Why division/mulitiplication/addition of pointers are not allowed in C? Rakesh Kumar wrote:Addition / subtraction is definitely allowed, the reason being pointersrefer to address and you can back and forth in the memory space. (Say,as in an array as the memory is allocated continuously ). Addition of pointers is *not* allowed. What should it yield, anyway? Regards -- Irrwahn Grausewitz (ir*******@freenet.de) welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt clc faq-list : http://www.faqs.org/faqs/C-faq/faq/ clc OT guide : http://benpfaff.org/writings/clc/off-topic.html Nov 14 '05 #17

 P: n/a Joona I Palaste wrote:Thomas Matthews scribbled the following: Lew Pitcher wrote: aruna wrote: | Why division/mulitiplication/addition of pointers are not | allowed in C? While pointer subtraction is meaningful, the other operations are not. One can add a scalar quantity to a pointer to a new location. Where did anyone mention a scalar quantity? Pointers *are* scalars. Thomas surely meant to talk about *arithmetic* (more precisely: integer) values added to or subtracted from pointer values. Regards -- Irrwahn Grausewitz (ir*******@freenet.de) welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt clc faq-list : http://www.faqs.org/faqs/C-faq/faq/ clc OT guide : http://benpfaff.org/writings/clc/off-topic.html Nov 14 '05 #18

 P: n/a On 24 Apr 2004 05:42:41 GMT, Joona I Palaste wrote: Addition of two pointers is not allowed, addition of a pointer and ascalar is. The last clause contradicts the first. Arithmetic types and pointer types are collectively called scalar types. If one operand is of pointer to object type, the other operand must be of integer type. -- Best wishes, Bob Nov 14 '05 #19

 P: n/a О Joona! Откуда ты взялся? 23 апр 04 18:59, you wrote to All: This analogy is lost on me. If I subtract one of those pointers from the other, then what body part would I be pointing to now? JIP> None. But you have the distance between your nose and your right ear. You can calculate size of an array by calculating pEnd-pFirst. PS. Sorry for my horrible English =) Joona? Куда он делся... Nov 14 '05 #20

 P: n/a On Sun, 25 Apr 2004 01:31:28 +0400, in comp.lang.c , To**********@p532.f495.n463.z2.fidonet.org (Tolik Piskov) wrote: Joona! wrote This analogy is lost on me. If I subtract one of those pointers from the other, then what body part would I be pointing to now? JIP> None. But you have the distance between your nose and your right ear. You can calculate size of an array by calculating pEnd-pFirst. That was joona's point. Pointer subtraction makes sense, other math operations don't. BTW don't use non english character sets on usenet, they don't translate well. -- Mark McIntyre CLC FAQ CLC readme: ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =--- Nov 14 '05 #21

 P: n/a In article , Lew Pitcher wrote: What would addition of two pointers give you? Something from which a pointer can be subtracted to get a pointer. For example: char a[10], b[10]; char *p = a; sum_of_two_char_star sum = a + b; while(1) { ...; p = sum - p; /* switch between a and b */ } -- Richard Nov 14 '05 #22

 P: n/a In ri*****@cogsci.ed.ac.uk (Richard Tobin) writes: In article ,Lew Pitcher wrote:What would addition of two pointers give you?Something from which a pointer can be subtracted to get a pointer.For example: char a[10], b[10]; char *p = a; sum_of_two_char_star sum = a + b; while(1) { ...; p = sum - p; /* switch between a and b */ } When would that be useful? Looks like a solution in search of a problem to me... Dan -- Dan Pop DESY Zeuthen, RZ group Email: Da*****@ifh.de Nov 14 '05 #23

 P: n/a Richard Tobin wrote: In article , Lew Pitcher wrote:What would addition of two pointers give you? Something from which a pointer can be subtracted to get a pointer. For example: char a[10], b[10]; char *p = a; sum_of_two_char_star sum = a + b; while(1) { ...; p = sum - p; /* switch between a and b */ } Well that is interesting. When adding two char pointers to obtain an integer (I assume that's what you'd get), what must the type and range of the integer be? I haven't checked the Standard for this sort of thing. I doubt it is defined. You're teasing, right? -- Joe Wright mailto:jo********@comcast.net "Everything should be made as simple as possible, but not simpler." --- Albert Einstein --- Nov 14 '05 #24

 P: n/a In article , Joe Wright wrote: Well that is interesting. When adding two char pointers to obtain aninteger (I assume that's what you'd get) No no no... you'd get a type quite distinct from pointers and integers. I haven't checked the Standard for this sort of thing. Very wise. -- Richard Nov 14 '05 #25

 P: n/a In article , Dan Pop wrote: When would that be useful? I gave an example: swapping between two pointers. I have several times used the same technique to swap between two integers. But: Looks like a solution in search of a problem to me... The point was not to show that it was necessary, or even a good idea, but to refute the claim that pointer addition is necessarily nonsensical. -- Richard Nov 14 '05 #26

 P: n/a In ri*****@cogsci.ed.ac.uk (Richard Tobin) writes: In article , Dan Pop wrote:When would that be useful?I gave an example: swapping between two pointers. I have severaltimes used the same technique to swap between two integers. But: It's not a good idea for swapping between two integers, either: undefined behaviour if the addition overflows, while the usual method is perfectly safe. And it works for pointers, too... Looks like a solution in search of a problem to me...The point was not to show that it was necessary, or even a good idea,but to refute the claim that pointer addition is necessarily nonsensical. If it's neither necessary nor a good idea, you have yet to prove that it is not nonsensical. Dan -- Dan Pop DESY Zeuthen, RZ group Email: Da*****@ifh.de Nov 14 '05 #27

 P: n/a In article , Dan Pop wrote: It's not a good idea for swapping between two integers, either: undefinedbehaviour if the addition overflows, If the integers are 1 and 2, this rarely happens. If it's neither necessary nor a good idea, you have yet to prove that itis not nonsensical. Since when did something have to be necessary or a good idea in order to not be nonsensical? -- Richard Nov 14 '05 #28

 P: n/a ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote: In article , Dan Pop wrote:It's not a good idea for swapping between two integers, either: undefinedbehaviour if the addition overflows, If the integers are 1 and 2, this rarely happens. If the objects swapped are pointers, you have no idea how "large" they are, so you cannot be so certain. So much for pointer addition. As for the same trick with integers, why confuse the optimiser with code which is hard to read even for humans? Richard Nov 14 '05 #29

 P: n/a In article Da*****@cern.ch (Dan Pop) writes: In ri*****@cogsci.ed.ac.uk (Richard Tobin) writes: ....When would that be useful?I gave an example: swapping between two pointers. I have severaltimes used the same technique to swap between two integers. But: It's not a good idea for swapping between two integers, either: undefined behaviour if the addition overflows, while the usual method is perfectly safe. And it works for pointers, too... But that sum idea can be used in linked lists. Just put in the link field the sum of the back and forward pointer and you can easily walk both ways (because you know where you came from). -- dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131 home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/ Nov 14 '05 #31

 P: n/a "Dik T. Winter" wrote: In article Da*****@cern.ch (Dan Pop) writes: > In ri*****@cogsci.ed.ac.uk (Richard Tobin) writes: ... > >>When would that be useful? > > > >I gave an example: swapping between two pointers. I have several > >times used the same technique to swap between two integers. But: > > It's not a good idea for swapping between two integers, either: undefined > behaviour if the addition overflows, while the usual method is perfectly > safe. And it works for pointers, too... But that sum idea can be used in linked lists. Just put in the link field the sum of the back and forward pointer and you can easily walk both ways (because you know where you came from). Ew. Vade retro... Richard Nov 14 '05 #32

 P: n/a In article <40*****************@news.individual.net>, Richard Bos wrote: >It's not a good idea for swapping between two integers, either: undefined >behaviour if the addition overflows, If the integers are 1 and 2, this rarely happens. If the objects swapped are pointers, you have no idea how "large" theyare, so you cannot be so certain. Obviously the sum-of-two-pointers type would have to be big enough for the sum of two pointers. -- Richard Nov 14 '05 #33

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

Browse more C / C++ Questions on Bytes