answersLogoWhite

0


Best Answer

Addition of pointers does not work, and here is why...

A pointer is an address in memory. It points to something, such as an integer. If you add 3 times the size of an integer to the pointer, you would be pointing at the third integer past the first integer, assuming that all four of them were lined up nicely in memory. Well, that is the same as an array.

What you CAN do to a pointer is add something to it, subtract something from it, or subtract another pointer from it, with special rules.

Lets say you have a pointer p that points to a[17]. You can refer to the item in question as *p or as a[17], it does not matter. Let says you add 5 to p, p = p+5; Now *p is a reference to a[22], and a[17] can only be referenced as a[17], or *(p-5) if you wanted to get cute.

Subtraction works the same way. p=p-22; means that *p now refers to a[0].

Well what about p=p-123; Now you are in deep doo doo, because you are outside the bounds of a. The same thing would happen if you add too much to p.

You are only allowed to address memory that you properly allocated and for the purpose for which you allocated it. Other memory that you might find "lying around' is not yours to play with and, in fact, using it could cause all sorts of havoc. Only play with memory that you properly allocated.

Now, what about subtraction of pointers. That is legal, and here is why. Lets say you had two pointers p and q, pointing to a[5] and a[17]. If you subtract p from q, you get 12, which is the difference in index between a[5] and a[17]. Note that this ONLY works when the two pointers refer to the same array, otherwise, all bets are off.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why does addition of two pointers not possible?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why Two pointers cannot be added in c language?

Because no-one knows what the sum of two pointers should be...of course you can convert them to integers and then sum them, but why on earth would you do that?


How do you get score 6?

3 two-pointers or 2 three-pointers


Which are the possibe arithmetic operation with pointers?

Pointers in C are stored as integers. You can perform any mathematical operations on pointers that you can perform on ints.Of course not, the following operations are possible: =, +, +=, ++, -, -=, --, *, [], ->, typecast


What are restricted pointers in C99?

Using this you specify that two pointers can't point on the same address


How many shots to get 40000 points in the NBA?

that would be 20000 two pointers or about 13333 three pointers


Why are there two pointers on the Wheel of Fortune wheel?

There are not those are to slow the wheel down by hitting the metal bars and are not the pointers


What kind of drills do you use for three pointers two pointers and foul shots?

just keep shooting its not that hard


How to write a C program to find largest 2 numbers using pointers?

program to find maximum of two numbers using pointers


What are the two common problems with pointers?

Memory leakage, problems with tracking and managing.


What are void pointers?

They are pointers without type


Write a c program to copy two strings using pointers?

nahi malum


What are the background pointers in java?

Java does not support pointers.