answersLogoWhite

0

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

15y ago

What else can I help you with?

Continue Learning about Math & Arithmetic

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

program to find maximum of two numbers using pointers


What are arithmetic operaters can be perform on pointer?

Arithmetic operators can be performed on pointers in C and C++ to navigate through arrays or memory blocks. The most common operations include addition and subtraction, which adjust the pointer's address by multiplying the integer value by the size of the data type it points to. Incrementing a pointer (using ++ or --) moves it to the next or previous element in the array, while subtracting two pointers yields the number of elements between them. However, using multiplication and division with pointers is not defined.


What is the group of two addition and two subtraction that uses the same three numbers?

The group of two addition and two subtraction using the same three numbers can be represented as follows: Given numbers (a), (b), and (c), one possible arrangement is ( (a + b) - c + (a - b) ). This expression uses addition and subtraction to combine the same three numbers in different ways, resulting in a calculation that involves each of the numbers.


What is calculating the sum of two or more numbers?

ADDITION.


Is it possible for vector A plus vector B to equal vector A - vector B?

It's impossible as the addition of two vectors is commutative i.e. A+B = B+A.For subtraction of two vectors, you have to subtract a vector B from vector A.The subtraction of the vector B from A is equivalent to the addition of (-B) with A, i.e. A-B = A+(-B).