C arrays and pointers: equivalent pointer to array element - c

Edit: The similar question asked before has not addressed some perspectives related to the issue.
In the ANSI C book by Kernighan and Ritchie, they say that the following are equivalent
a[i]
*(a+i)
I don't see how this can be true for elements that occupy more than one address space, e.g. structs.
Please explain? Edit: Thank you for all answers, but I don't quite understand it. It would seem I am suffering the same confusion as #CucumisSativus from his answer and comments to it.
Say sizeof(*a) is 3. If for some reason I wanted to access the middle byte of the first element in a, I had thought this is how I would do it: *(a+1).
Say the address of a is 10, and the sizeof each element is 20. And say we want to get the pointer to the second element. As I see it, we could do this: p = (10 + 20). I thought this would be equivalent to &a[1].
I'm having real trouble explaining what I don't understand!!

Pointer arithmetic is treated differently than regular integer arithmetic in C. Adding an integer i to a pointer p advances the memory address by i * sizeof(*p), i.e., by i times the size of the type being pointed to.
As a potentially interesting, but practically useless, sidenote: due to the definition of p[i] as *(p+i), the expression i[p] is also equivalent to the same…

The + operator is not meant to be the next element in the address space.
The increment is defined by the size of the data type involved. Thus, a + 1 will refer to the next int if a is a pointer to an integer, and will refer to the next struct if a is a pointer to a struct.

Lets say we have an Integer pointer and a Character pointer as follows
int *a;
char *b;
Now assume that a is stored at location 10000
and b is stored at location 20000
So by doing the following operations
*(a+1) : It will return the value contained in address 10004 i.e. (10000+4)
*(b+1) : It will return the value contained in address 20001 i.e. (20000+1)
Reason:
Pointer addition is different from regular arithmetic addition. If you add 1 to an integer pointer it advances the pointer to a location by the size of the integer. So in this case it advances the pointer to 10004. Since character is only 1 byte long it advances the pointer by 1 byte.

a+i is translated to something like this in pseudo code
*(a+i*sizeof(variableType))

I read more of K&R and found the missing pieces to the puzzle, allowing me to rationalise what's happening. (Whether I'm correct or not is open to debate!)
From what I gather, the following is simply invalid:
p = (10 + 20)
Given that, to quote: "Pointers and integers are not interchangeable. Zero is the only exception."
I interpret this to mean that *(a+i) is not performing straight forward integer arithmetic.
I believe this behaviour is determined by the existence of a pointer in the expression.
And to quote K&R again, and other answers provided to my question, the exact behaviour of arithmetic of arithmetic involving:
one pointer with
one or more integers
for (p + n) is defined like so: "n is scaled according to the size of the objects p points to, which is determined by the declaration of p".
I realised what caused me to be so confused: I have in the past been able to use printf() to output the actual address value of a pointer. I believe this is "undefined behaviour" which the compiler I was using happened to handle by treating the pointer as an integer.
Similarly, given the above two numbered bullet points, and the definition that uses them, I gather that adding two pointers together (of the same type) results in undefined behaviour, and more certainly, adding two pointers of different types.
To get the nth byte of an integer, see this question.
K&R later adds that the following are legal pointer operations:
Assigning one pointer to another (of the same type)
Adding or subtracting a pointer and an integer
Subtracting one pointer from another (to the same array)
Comparing pointers (to the same array)
Assigning / comparing to 0
All other pointer arithmetic is illegal.

Related

What does it mean when strcmp has a plus sign? [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Does anyone have any good articles or explanations (blogs, examples) for pointer arithmetic? Figure the audience is a bunch of Java programmers learning C and C++.
Here is where I learned pointers: http://www.cplusplus.com/doc/tutorial/pointers.html
Once you understand pointers, pointer arithmetic is easy. The only difference between it and regular arithmetic is that the number you are adding to the pointer will be multiplied by the size of the type that the pointer is pointing to. For example, if you have a pointer to an int and an int's size is 4 bytes, (pointer_to_int + 4) will evaluate to a memory address 16 bytes (4 ints) ahead.
So when you write
(a_pointer + a_number)
in pointer arithmetic, what's really happening is
(a_pointer + (a_number * sizeof(*a_pointer)))
in regular arithmetic.
First, the binky video may help. It's a nice video about pointers. For arithmetic, here is an example:
int * pa = NULL;
int * pb = NULL;
pa += 1; // pa++. behind the scenes, add sizeof(int) bytes
assert((pa - pb) == 1);
print_out(pa); // possibly outputs 0x4
print_out(pb); // possibly outputs 0x0 (if NULL is actually bit-wise 0x0)
(Note that incrementing a pointer that contains a null pointer value strictly is undefined behavior. We used NULL because we were only interested in the value of the pointer. Normally, only use increment/decrement when pointing to elements of an array).
The following shows two important concepts
addition/subtraction of a integer to a pointer means move the pointer forward / backward by N elements. So if an int is 4 bytes big, pa could contain 0x4 on our platform after having incremented by 1.
subtraction of a pointer by another pointer means getting their distance, measured by elements. So subtracting pb from pa will yield 1, since they have one element distance.
On a practical example. Suppose you write a function and people provide you with an start and end pointer (very common thing in C++):
void mutate_them(int *begin, int *end) {
// get the amount of elements
ptrdiff_t n = end - begin;
// allocate space for n elements to do something...
// then iterate. increment begin until it hits end
while(begin != end) {
// do something
begin++;
}
}
ptrdiff_t is what is the type of (end - begin). It may be a synonym for "int" for some compiler, but may be another type for another one. One cannot know, so one chooses the generic typedef ptrdiff_t.
applying NLP, call it address arithmetic. 'pointers' are feared and misunderstood mostly because they are taught by the wrong people and/or at the wrong stage with wrong examples in the wrong way. It is no wonder that nobody 'gets' it.
when teaching pointers, the faculty goes on about "p is a pointer to a, the value of p is the address of a" and so on. it just wont work. here is the raw material for you to build with. practice with it and your students will get it.
'int a', a is an integer, it stores integer type values.
'int* p', p is an 'int star', it stores 'int star' type values.
'a' is how you get the 'what' integer stored in a (try not to use 'value of a')
'&a' is how you get the 'where' a itself is stored (try to say 'address')
'b = a' for this to work, both sides must be of the same type. if a is int, b must be capable of storing an int. (so ______ b, the blank is filled with 'int')
'p = &a' for this to work, both sides must be of the same type. if a is an integer, &a is an address, p must be capable of storing addresses of integers. (so ______ p, the blank is filled with 'int *')
now write int *p differently to bring out the type information:
int* | p
what is 'p'? ans: it is 'int *'. so 'p' is an address of an integer.
int | *p
what is '*p'? ans: it is an 'int'. so '*p' is an integer.
now on to the address arithmetic:
int a;
a=1;
a=a+1;
what are we doing in 'a=a+1'? think of it as 'next'. Because a is a number, this is like saying 'next number'. Since a holds 1, saying 'next' will make it 2.
// fallacious example. you have been warned!!!
int *p
int a;
p = &a;
p=p+1;
what are we doing in 'p=p+1'? it is still saying 'next'. This time, p is not a number but an address. So what we are saying is 'next address'. Next address depends on the data type, more specifically on the size of the data type.
printf("%d %d %d", sizeof(char), sizeof(int), sizeof(float));
so 'next' for an address will move forward sizeof(data type).
this has worked for me and all of the people I used to teach.
I consider a good example of pointer arithmetic the following string length function:
int length(char *s)
{
char *str = s;
while(*str++);
return str - s;
}
So, the key thing to remember is that a pointer is just a word-sized variable that's typed for dereferencing. That means that whether it's a void *, int *, long long **, it's still just a word sized variable. The difference between these types is what the compiler considers the dereferenced type. Just to clarify, word sized means width of a virtual address. If you don't know what this means, just remember on a 64-bit machine, pointers are 8 bytes, and on a 32-bit machine, pointers are 4 bytes. The concept of an address is SUPER important in understanding pointers. An address is a number capable of uniquely identifying a certain location in memory. Everything in memory has an address. For our purposes, we can say that every variable has an address. This isn't necessarily always true, but the compiler lets us assume this. The address itself is byte granular, meaning 0x0000000 specifies the beginning of memory, and 0x00000001 is one byte into memory. This means that by adding one to a pointer, we're moving one byte forward into memory. Now, lets take arrays. If you create an array of type quux that's 32 elements big, it will span from the beginning of it's allocation, to the beginning of it's allocation plus 32*sizeof(quux), since each cell of the array is sizeof(quux) big. So, really when we specify an element of an array with array[n], that's just syntactic sugar (shorthand) for *(array+sizeof(quux)*n). Pointer arithmetic is really just changing the address that you're referring to, which is why we can implement strlen with
while(*n++ != '\0'){
len++;
}
since we're just scanning along, byte by byte until we hit a zero. Hope that helps!
There are several ways to tackle it.
The intuitive approach, which is what most C/C++ programmers think of, is that pointers are memory addresses. litb's example takes this approach. If you have a null pointer (which on most machines corresponds to the address 0), and you add the size of an int, you get the address 4. This implies that pointers are basically just fancy integers.
Unfortunately, there are a few problems with this. To begin with, it may not work.
A null pointer is not guaranteed to actually use the address 0. (Although assigning the constant 0 to a pointer yields the null pointer).
Further, you're not allowed to increment the null pointer, or more generally, a pointer must always point to allocated memory (or one element past), or the special null pointer constant 0.
So a more correct way of thinking of it is that pointers are simply iterators allowing you to iterate over allocated memory.
This is really one of the key ideas behind the STL iterators. They're modelled to behave very much as pointers, and to provide specializations that patch up raw pointers to work as proper iterators.
A more elaborate explanation of this is given here, for example.
But this latter view means that you should really explain STL iterators, and then simply say that pointers are a special case of these. You can increment a pointer to point to the next element in the buffer, just like you can a std::vector<int>::iterator. It can point one element past the end of an array, just like the end iterator in any other container. You can subtract two pointers that point into the same buffer to get the number of elements between them, just like you can with iterators, and just like with iterators, if the pointers point into separate buffers, you can not meaningfully compare them. (For a practical example of why not, consider what happens in a segmented memory space. What's the distance between two pointers pointing to separate segments?)
Of course in practice, there's a very close correlation between CPU addresses and C/C++ pointers. But they're not exactly the same thing. Pointers have a few limitations that may not be strictly necessary on your CPU.
Of course, most C++ programmers muddle by on the first understanding, even though it's technically incorrect. It's typically close enough to how your code ends up behaving that people think they get it, and move on.
But for someone coming from Java, and just learning about pointers from scratch, the latter explanation may be just as easily understood, and it's going to spring fewer surprises on them later.
This is one pretty good at link here about Pointer Arithmetic
For example:
Pointer and array
Formula for computing the address of ptr + i where ptr has type T *. then the formula for the address is:
addr( ptr + i ) = addr( ptr ) + [ sizeof( T ) * i ]
For for type of int on 32bit platform, addr(ptr+i) = addr(ptr)+4*i;
Subtraction
We can also compute ptr - i. For example, suppose we have an int array called arr.
int arr[ 10 ] ;
int * p1, * p2 ;
p1 = arr + 3 ; // p1 == & arr[ 3 ]
p2 = p1 - 2 ; // p1 == & arr[ 1 ]

Pointers, addresses and arrays in C

So I know that pointers are variables that store the address(s) of some other variables. But the thing that I am confused with is this:
I know that using the name of the array in most of the contexts would decay it to the base address of the element. But I get confused here:
When we declare an array say int arr[5], and then say scanf("%d",arr+1), here arr is an address right? We did not explicitly store it in any variable i.e, a pointer. So how does arr+1 arithmetic work similar to pointer arithmetic? Also this: *(arr+1) would give us the value in the address arr+1. I do realize that we can dereference an address, but here I kind of find it difficult to work with it because I kind of lose the "hey this pointer points to this address.." here. And my final question would be, is this the exact way that we can differentiate an address from a random value in a computer? Like arr+1 leads to the address of the next integer in this case, and not normal +1 (pointer arithmetic basically).
I hope that my question does not have any ambiguity.
Your problem starts with this misunderstanding:
So I know that pointers are variables that store the address(s) of some other variables.
"Pointer" is a type category. There are values of pointer types, which are also referred to as "addresses", and there also variables declared to hold such values. This is entirely analogous to there being values of type int and variables declared to hold int values. Also, just as we might refer to either an int value or an int variable as "an int", people can and do refer to both pointer values and pointer variables as "pointers".
Additionally, it is a bit imprecise and a bit misleading to say that pointer values are the addresses of variables. They are the addresses of objects, and although in-scope variables do represent objects, not all objects correspond to variables.
When we declare an array say int arr[5], and then say scanf("%d",arr+1), here arr is an address right?
When the scanf call is evaluated, the array value of arr is automatically converted to a pointer value, the address of the first array element.
We did not explicitly store it in any variable i.e, a pointer.
You do not need to store a pointer value in a variable in order to use it in an expression. In fact, just the opposite: if you want to use the value of a pointer variable in an arithmetic expression, you must first read the value from it -- a process called "lvalue conversion". The same applies to variables of all types and expressions involving most operators. Most operations are defined in terms of values, and variables are just one way to convey those.
So how does arr+1 arithmetic work similar to pointer arithmetic?
There is no "similar to" here. It is pointer arithmetic. arr is automatically converted to a pointer value. That value is a suitable operand for the addition operator, provided that the other operand is of integer type. This is a pointer arithmetic expression.
Also this: *(arr+1) would give us the value in the address arr+1.
Yes, though it would be more idiomatic to say the value at address arr+1.
I do realize that we can dereference an address, but here I kind of find it difficult to work with it because I kind of lose the "hey this pointer points to this address.."
Well, most people would write *(arr+1) as arr[1], which is 100% equivalent, but easier to read and usually clearer. Other than that, I'm not sure what you're asking, if anything.
is this the exact way that we can differentiate an address from a random value in a computer? Like arr+1 leads to the address of the next integer in this case, and not normal +1 (pointer arithmetic basically).
How a C program interprets a given sequence of bytes is determined by the data type it attributes to the sequence. That is exactly the function of data typing. There is no difference discernable in the underlying physical memory, it is all a manner of the program's interpretation. x+1 is pointer arithmetic if x has pointer type, including as the result of automatic conversion from an array. It is integer addition if x has type int, long, etc.. It is floating-point addition if x has type double, float, or long double.
Let's suppose we have:
int n = 1;
int arr[100];
arr + n: yes this is an address.
arr + n: yes, we do doing pointer arithmetic here.
arr + n is the same thing as &arr[n].
*(arr + n) is the same thing as arr[n].
Example of pointer arithmetic:
Let's suppose the size of an int is 4 bytes and arr is the address 0x10000:
arr + n is 0x10000 + n * 4
sor for example arr+2 is the address 0x10008
Your last question is somewhat unclear but if you understood the above it should be clear.

Why does pointer arithmetic work the way it does? [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Does anyone have any good articles or explanations (blogs, examples) for pointer arithmetic? Figure the audience is a bunch of Java programmers learning C and C++.
Here is where I learned pointers: http://www.cplusplus.com/doc/tutorial/pointers.html
Once you understand pointers, pointer arithmetic is easy. The only difference between it and regular arithmetic is that the number you are adding to the pointer will be multiplied by the size of the type that the pointer is pointing to. For example, if you have a pointer to an int and an int's size is 4 bytes, (pointer_to_int + 4) will evaluate to a memory address 16 bytes (4 ints) ahead.
So when you write
(a_pointer + a_number)
in pointer arithmetic, what's really happening is
(a_pointer + (a_number * sizeof(*a_pointer)))
in regular arithmetic.
First, the binky video may help. It's a nice video about pointers. For arithmetic, here is an example:
int * pa = NULL;
int * pb = NULL;
pa += 1; // pa++. behind the scenes, add sizeof(int) bytes
assert((pa - pb) == 1);
print_out(pa); // possibly outputs 0x4
print_out(pb); // possibly outputs 0x0 (if NULL is actually bit-wise 0x0)
(Note that incrementing a pointer that contains a null pointer value strictly is undefined behavior. We used NULL because we were only interested in the value of the pointer. Normally, only use increment/decrement when pointing to elements of an array).
The following shows two important concepts
addition/subtraction of a integer to a pointer means move the pointer forward / backward by N elements. So if an int is 4 bytes big, pa could contain 0x4 on our platform after having incremented by 1.
subtraction of a pointer by another pointer means getting their distance, measured by elements. So subtracting pb from pa will yield 1, since they have one element distance.
On a practical example. Suppose you write a function and people provide you with an start and end pointer (very common thing in C++):
void mutate_them(int *begin, int *end) {
// get the amount of elements
ptrdiff_t n = end - begin;
// allocate space for n elements to do something...
// then iterate. increment begin until it hits end
while(begin != end) {
// do something
begin++;
}
}
ptrdiff_t is what is the type of (end - begin). It may be a synonym for "int" for some compiler, but may be another type for another one. One cannot know, so one chooses the generic typedef ptrdiff_t.
applying NLP, call it address arithmetic. 'pointers' are feared and misunderstood mostly because they are taught by the wrong people and/or at the wrong stage with wrong examples in the wrong way. It is no wonder that nobody 'gets' it.
when teaching pointers, the faculty goes on about "p is a pointer to a, the value of p is the address of a" and so on. it just wont work. here is the raw material for you to build with. practice with it and your students will get it.
'int a', a is an integer, it stores integer type values.
'int* p', p is an 'int star', it stores 'int star' type values.
'a' is how you get the 'what' integer stored in a (try not to use 'value of a')
'&a' is how you get the 'where' a itself is stored (try to say 'address')
'b = a' for this to work, both sides must be of the same type. if a is int, b must be capable of storing an int. (so ______ b, the blank is filled with 'int')
'p = &a' for this to work, both sides must be of the same type. if a is an integer, &a is an address, p must be capable of storing addresses of integers. (so ______ p, the blank is filled with 'int *')
now write int *p differently to bring out the type information:
int* | p
what is 'p'? ans: it is 'int *'. so 'p' is an address of an integer.
int | *p
what is '*p'? ans: it is an 'int'. so '*p' is an integer.
now on to the address arithmetic:
int a;
a=1;
a=a+1;
what are we doing in 'a=a+1'? think of it as 'next'. Because a is a number, this is like saying 'next number'. Since a holds 1, saying 'next' will make it 2.
// fallacious example. you have been warned!!!
int *p
int a;
p = &a;
p=p+1;
what are we doing in 'p=p+1'? it is still saying 'next'. This time, p is not a number but an address. So what we are saying is 'next address'. Next address depends on the data type, more specifically on the size of the data type.
printf("%d %d %d", sizeof(char), sizeof(int), sizeof(float));
so 'next' for an address will move forward sizeof(data type).
this has worked for me and all of the people I used to teach.
I consider a good example of pointer arithmetic the following string length function:
int length(char *s)
{
char *str = s;
while(*str++);
return str - s;
}
So, the key thing to remember is that a pointer is just a word-sized variable that's typed for dereferencing. That means that whether it's a void *, int *, long long **, it's still just a word sized variable. The difference between these types is what the compiler considers the dereferenced type. Just to clarify, word sized means width of a virtual address. If you don't know what this means, just remember on a 64-bit machine, pointers are 8 bytes, and on a 32-bit machine, pointers are 4 bytes. The concept of an address is SUPER important in understanding pointers. An address is a number capable of uniquely identifying a certain location in memory. Everything in memory has an address. For our purposes, we can say that every variable has an address. This isn't necessarily always true, but the compiler lets us assume this. The address itself is byte granular, meaning 0x0000000 specifies the beginning of memory, and 0x00000001 is one byte into memory. This means that by adding one to a pointer, we're moving one byte forward into memory. Now, lets take arrays. If you create an array of type quux that's 32 elements big, it will span from the beginning of it's allocation, to the beginning of it's allocation plus 32*sizeof(quux), since each cell of the array is sizeof(quux) big. So, really when we specify an element of an array with array[n], that's just syntactic sugar (shorthand) for *(array+sizeof(quux)*n). Pointer arithmetic is really just changing the address that you're referring to, which is why we can implement strlen with
while(*n++ != '\0'){
len++;
}
since we're just scanning along, byte by byte until we hit a zero. Hope that helps!
There are several ways to tackle it.
The intuitive approach, which is what most C/C++ programmers think of, is that pointers are memory addresses. litb's example takes this approach. If you have a null pointer (which on most machines corresponds to the address 0), and you add the size of an int, you get the address 4. This implies that pointers are basically just fancy integers.
Unfortunately, there are a few problems with this. To begin with, it may not work.
A null pointer is not guaranteed to actually use the address 0. (Although assigning the constant 0 to a pointer yields the null pointer).
Further, you're not allowed to increment the null pointer, or more generally, a pointer must always point to allocated memory (or one element past), or the special null pointer constant 0.
So a more correct way of thinking of it is that pointers are simply iterators allowing you to iterate over allocated memory.
This is really one of the key ideas behind the STL iterators. They're modelled to behave very much as pointers, and to provide specializations that patch up raw pointers to work as proper iterators.
A more elaborate explanation of this is given here, for example.
But this latter view means that you should really explain STL iterators, and then simply say that pointers are a special case of these. You can increment a pointer to point to the next element in the buffer, just like you can a std::vector<int>::iterator. It can point one element past the end of an array, just like the end iterator in any other container. You can subtract two pointers that point into the same buffer to get the number of elements between them, just like you can with iterators, and just like with iterators, if the pointers point into separate buffers, you can not meaningfully compare them. (For a practical example of why not, consider what happens in a segmented memory space. What's the distance between two pointers pointing to separate segments?)
Of course in practice, there's a very close correlation between CPU addresses and C/C++ pointers. But they're not exactly the same thing. Pointers have a few limitations that may not be strictly necessary on your CPU.
Of course, most C++ programmers muddle by on the first understanding, even though it's technically incorrect. It's typically close enough to how your code ends up behaving that people think they get it, and move on.
But for someone coming from Java, and just learning about pointers from scratch, the latter explanation may be just as easily understood, and it's going to spring fewer surprises on them later.
This is one pretty good at link here about Pointer Arithmetic
For example:
Pointer and array
Formula for computing the address of ptr + i where ptr has type T *. then the formula for the address is:
addr( ptr + i ) = addr( ptr ) + [ sizeof( T ) * i ]
For for type of int on 32bit platform, addr(ptr+i) = addr(ptr)+4*i;
Subtraction
We can also compute ptr - i. For example, suppose we have an int array called arr.
int arr[ 10 ] ;
int * p1, * p2 ;
p1 = arr + 3 ; // p1 == & arr[ 3 ]
p2 = p1 - 2 ; // p1 == & arr[ 1 ]

Need help understanding pointers in some sample code [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Does anyone have any good articles or explanations (blogs, examples) for pointer arithmetic? Figure the audience is a bunch of Java programmers learning C and C++.
Here is where I learned pointers: http://www.cplusplus.com/doc/tutorial/pointers.html
Once you understand pointers, pointer arithmetic is easy. The only difference between it and regular arithmetic is that the number you are adding to the pointer will be multiplied by the size of the type that the pointer is pointing to. For example, if you have a pointer to an int and an int's size is 4 bytes, (pointer_to_int + 4) will evaluate to a memory address 16 bytes (4 ints) ahead.
So when you write
(a_pointer + a_number)
in pointer arithmetic, what's really happening is
(a_pointer + (a_number * sizeof(*a_pointer)))
in regular arithmetic.
First, the binky video may help. It's a nice video about pointers. For arithmetic, here is an example:
int * pa = NULL;
int * pb = NULL;
pa += 1; // pa++. behind the scenes, add sizeof(int) bytes
assert((pa - pb) == 1);
print_out(pa); // possibly outputs 0x4
print_out(pb); // possibly outputs 0x0 (if NULL is actually bit-wise 0x0)
(Note that incrementing a pointer that contains a null pointer value strictly is undefined behavior. We used NULL because we were only interested in the value of the pointer. Normally, only use increment/decrement when pointing to elements of an array).
The following shows two important concepts
addition/subtraction of a integer to a pointer means move the pointer forward / backward by N elements. So if an int is 4 bytes big, pa could contain 0x4 on our platform after having incremented by 1.
subtraction of a pointer by another pointer means getting their distance, measured by elements. So subtracting pb from pa will yield 1, since they have one element distance.
On a practical example. Suppose you write a function and people provide you with an start and end pointer (very common thing in C++):
void mutate_them(int *begin, int *end) {
// get the amount of elements
ptrdiff_t n = end - begin;
// allocate space for n elements to do something...
// then iterate. increment begin until it hits end
while(begin != end) {
// do something
begin++;
}
}
ptrdiff_t is what is the type of (end - begin). It may be a synonym for "int" for some compiler, but may be another type for another one. One cannot know, so one chooses the generic typedef ptrdiff_t.
applying NLP, call it address arithmetic. 'pointers' are feared and misunderstood mostly because they are taught by the wrong people and/or at the wrong stage with wrong examples in the wrong way. It is no wonder that nobody 'gets' it.
when teaching pointers, the faculty goes on about "p is a pointer to a, the value of p is the address of a" and so on. it just wont work. here is the raw material for you to build with. practice with it and your students will get it.
'int a', a is an integer, it stores integer type values.
'int* p', p is an 'int star', it stores 'int star' type values.
'a' is how you get the 'what' integer stored in a (try not to use 'value of a')
'&a' is how you get the 'where' a itself is stored (try to say 'address')
'b = a' for this to work, both sides must be of the same type. if a is int, b must be capable of storing an int. (so ______ b, the blank is filled with 'int')
'p = &a' for this to work, both sides must be of the same type. if a is an integer, &a is an address, p must be capable of storing addresses of integers. (so ______ p, the blank is filled with 'int *')
now write int *p differently to bring out the type information:
int* | p
what is 'p'? ans: it is 'int *'. so 'p' is an address of an integer.
int | *p
what is '*p'? ans: it is an 'int'. so '*p' is an integer.
now on to the address arithmetic:
int a;
a=1;
a=a+1;
what are we doing in 'a=a+1'? think of it as 'next'. Because a is a number, this is like saying 'next number'. Since a holds 1, saying 'next' will make it 2.
// fallacious example. you have been warned!!!
int *p
int a;
p = &a;
p=p+1;
what are we doing in 'p=p+1'? it is still saying 'next'. This time, p is not a number but an address. So what we are saying is 'next address'. Next address depends on the data type, more specifically on the size of the data type.
printf("%d %d %d", sizeof(char), sizeof(int), sizeof(float));
so 'next' for an address will move forward sizeof(data type).
this has worked for me and all of the people I used to teach.
I consider a good example of pointer arithmetic the following string length function:
int length(char *s)
{
char *str = s;
while(*str++);
return str - s;
}
So, the key thing to remember is that a pointer is just a word-sized variable that's typed for dereferencing. That means that whether it's a void *, int *, long long **, it's still just a word sized variable. The difference between these types is what the compiler considers the dereferenced type. Just to clarify, word sized means width of a virtual address. If you don't know what this means, just remember on a 64-bit machine, pointers are 8 bytes, and on a 32-bit machine, pointers are 4 bytes. The concept of an address is SUPER important in understanding pointers. An address is a number capable of uniquely identifying a certain location in memory. Everything in memory has an address. For our purposes, we can say that every variable has an address. This isn't necessarily always true, but the compiler lets us assume this. The address itself is byte granular, meaning 0x0000000 specifies the beginning of memory, and 0x00000001 is one byte into memory. This means that by adding one to a pointer, we're moving one byte forward into memory. Now, lets take arrays. If you create an array of type quux that's 32 elements big, it will span from the beginning of it's allocation, to the beginning of it's allocation plus 32*sizeof(quux), since each cell of the array is sizeof(quux) big. So, really when we specify an element of an array with array[n], that's just syntactic sugar (shorthand) for *(array+sizeof(quux)*n). Pointer arithmetic is really just changing the address that you're referring to, which is why we can implement strlen with
while(*n++ != '\0'){
len++;
}
since we're just scanning along, byte by byte until we hit a zero. Hope that helps!
There are several ways to tackle it.
The intuitive approach, which is what most C/C++ programmers think of, is that pointers are memory addresses. litb's example takes this approach. If you have a null pointer (which on most machines corresponds to the address 0), and you add the size of an int, you get the address 4. This implies that pointers are basically just fancy integers.
Unfortunately, there are a few problems with this. To begin with, it may not work.
A null pointer is not guaranteed to actually use the address 0. (Although assigning the constant 0 to a pointer yields the null pointer).
Further, you're not allowed to increment the null pointer, or more generally, a pointer must always point to allocated memory (or one element past), or the special null pointer constant 0.
So a more correct way of thinking of it is that pointers are simply iterators allowing you to iterate over allocated memory.
This is really one of the key ideas behind the STL iterators. They're modelled to behave very much as pointers, and to provide specializations that patch up raw pointers to work as proper iterators.
A more elaborate explanation of this is given here, for example.
But this latter view means that you should really explain STL iterators, and then simply say that pointers are a special case of these. You can increment a pointer to point to the next element in the buffer, just like you can a std::vector<int>::iterator. It can point one element past the end of an array, just like the end iterator in any other container. You can subtract two pointers that point into the same buffer to get the number of elements between them, just like you can with iterators, and just like with iterators, if the pointers point into separate buffers, you can not meaningfully compare them. (For a practical example of why not, consider what happens in a segmented memory space. What's the distance between two pointers pointing to separate segments?)
Of course in practice, there's a very close correlation between CPU addresses and C/C++ pointers. But they're not exactly the same thing. Pointers have a few limitations that may not be strictly necessary on your CPU.
Of course, most C++ programmers muddle by on the first understanding, even though it's technically incorrect. It's typically close enough to how your code ends up behaving that people think they get it, and move on.
But for someone coming from Java, and just learning about pointers from scratch, the latter explanation may be just as easily understood, and it's going to spring fewer surprises on them later.
This is one pretty good at link here about Pointer Arithmetic
For example:
Pointer and array
Formula for computing the address of ptr + i where ptr has type T *. then the formula for the address is:
addr( ptr + i ) = addr( ptr ) + [ sizeof( T ) * i ]
For for type of int on 32bit platform, addr(ptr+i) = addr(ptr)+4*i;
Subtraction
We can also compute ptr - i. For example, suppose we have an int array called arr.
int arr[ 10 ] ;
int * p1, * p2 ;
p1 = arr + 3 ; // p1 == & arr[ 3 ]
p2 = p1 - 2 ; // p1 == & arr[ 1 ]

Pointer Arithmetic [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Does anyone have any good articles or explanations (blogs, examples) for pointer arithmetic? Figure the audience is a bunch of Java programmers learning C and C++.
Here is where I learned pointers: http://www.cplusplus.com/doc/tutorial/pointers.html
Once you understand pointers, pointer arithmetic is easy. The only difference between it and regular arithmetic is that the number you are adding to the pointer will be multiplied by the size of the type that the pointer is pointing to. For example, if you have a pointer to an int and an int's size is 4 bytes, (pointer_to_int + 4) will evaluate to a memory address 16 bytes (4 ints) ahead.
So when you write
(a_pointer + a_number)
in pointer arithmetic, what's really happening is
(a_pointer + (a_number * sizeof(*a_pointer)))
in regular arithmetic.
First, the binky video may help. It's a nice video about pointers. For arithmetic, here is an example:
int * pa = NULL;
int * pb = NULL;
pa += 1; // pa++. behind the scenes, add sizeof(int) bytes
assert((pa - pb) == 1);
print_out(pa); // possibly outputs 0x4
print_out(pb); // possibly outputs 0x0 (if NULL is actually bit-wise 0x0)
(Note that incrementing a pointer that contains a null pointer value strictly is undefined behavior. We used NULL because we were only interested in the value of the pointer. Normally, only use increment/decrement when pointing to elements of an array).
The following shows two important concepts
addition/subtraction of a integer to a pointer means move the pointer forward / backward by N elements. So if an int is 4 bytes big, pa could contain 0x4 on our platform after having incremented by 1.
subtraction of a pointer by another pointer means getting their distance, measured by elements. So subtracting pb from pa will yield 1, since they have one element distance.
On a practical example. Suppose you write a function and people provide you with an start and end pointer (very common thing in C++):
void mutate_them(int *begin, int *end) {
// get the amount of elements
ptrdiff_t n = end - begin;
// allocate space for n elements to do something...
// then iterate. increment begin until it hits end
while(begin != end) {
// do something
begin++;
}
}
ptrdiff_t is what is the type of (end - begin). It may be a synonym for "int" for some compiler, but may be another type for another one. One cannot know, so one chooses the generic typedef ptrdiff_t.
applying NLP, call it address arithmetic. 'pointers' are feared and misunderstood mostly because they are taught by the wrong people and/or at the wrong stage with wrong examples in the wrong way. It is no wonder that nobody 'gets' it.
when teaching pointers, the faculty goes on about "p is a pointer to a, the value of p is the address of a" and so on. it just wont work. here is the raw material for you to build with. practice with it and your students will get it.
'int a', a is an integer, it stores integer type values.
'int* p', p is an 'int star', it stores 'int star' type values.
'a' is how you get the 'what' integer stored in a (try not to use 'value of a')
'&a' is how you get the 'where' a itself is stored (try to say 'address')
'b = a' for this to work, both sides must be of the same type. if a is int, b must be capable of storing an int. (so ______ b, the blank is filled with 'int')
'p = &a' for this to work, both sides must be of the same type. if a is an integer, &a is an address, p must be capable of storing addresses of integers. (so ______ p, the blank is filled with 'int *')
now write int *p differently to bring out the type information:
int* | p
what is 'p'? ans: it is 'int *'. so 'p' is an address of an integer.
int | *p
what is '*p'? ans: it is an 'int'. so '*p' is an integer.
now on to the address arithmetic:
int a;
a=1;
a=a+1;
what are we doing in 'a=a+1'? think of it as 'next'. Because a is a number, this is like saying 'next number'. Since a holds 1, saying 'next' will make it 2.
// fallacious example. you have been warned!!!
int *p
int a;
p = &a;
p=p+1;
what are we doing in 'p=p+1'? it is still saying 'next'. This time, p is not a number but an address. So what we are saying is 'next address'. Next address depends on the data type, more specifically on the size of the data type.
printf("%d %d %d", sizeof(char), sizeof(int), sizeof(float));
so 'next' for an address will move forward sizeof(data type).
this has worked for me and all of the people I used to teach.
I consider a good example of pointer arithmetic the following string length function:
int length(char *s)
{
char *str = s;
while(*str++);
return str - s;
}
So, the key thing to remember is that a pointer is just a word-sized variable that's typed for dereferencing. That means that whether it's a void *, int *, long long **, it's still just a word sized variable. The difference between these types is what the compiler considers the dereferenced type. Just to clarify, word sized means width of a virtual address. If you don't know what this means, just remember on a 64-bit machine, pointers are 8 bytes, and on a 32-bit machine, pointers are 4 bytes. The concept of an address is SUPER important in understanding pointers. An address is a number capable of uniquely identifying a certain location in memory. Everything in memory has an address. For our purposes, we can say that every variable has an address. This isn't necessarily always true, but the compiler lets us assume this. The address itself is byte granular, meaning 0x0000000 specifies the beginning of memory, and 0x00000001 is one byte into memory. This means that by adding one to a pointer, we're moving one byte forward into memory. Now, lets take arrays. If you create an array of type quux that's 32 elements big, it will span from the beginning of it's allocation, to the beginning of it's allocation plus 32*sizeof(quux), since each cell of the array is sizeof(quux) big. So, really when we specify an element of an array with array[n], that's just syntactic sugar (shorthand) for *(array+sizeof(quux)*n). Pointer arithmetic is really just changing the address that you're referring to, which is why we can implement strlen with
while(*n++ != '\0'){
len++;
}
since we're just scanning along, byte by byte until we hit a zero. Hope that helps!
There are several ways to tackle it.
The intuitive approach, which is what most C/C++ programmers think of, is that pointers are memory addresses. litb's example takes this approach. If you have a null pointer (which on most machines corresponds to the address 0), and you add the size of an int, you get the address 4. This implies that pointers are basically just fancy integers.
Unfortunately, there are a few problems with this. To begin with, it may not work.
A null pointer is not guaranteed to actually use the address 0. (Although assigning the constant 0 to a pointer yields the null pointer).
Further, you're not allowed to increment the null pointer, or more generally, a pointer must always point to allocated memory (or one element past), or the special null pointer constant 0.
So a more correct way of thinking of it is that pointers are simply iterators allowing you to iterate over allocated memory.
This is really one of the key ideas behind the STL iterators. They're modelled to behave very much as pointers, and to provide specializations that patch up raw pointers to work as proper iterators.
A more elaborate explanation of this is given here, for example.
But this latter view means that you should really explain STL iterators, and then simply say that pointers are a special case of these. You can increment a pointer to point to the next element in the buffer, just like you can a std::vector<int>::iterator. It can point one element past the end of an array, just like the end iterator in any other container. You can subtract two pointers that point into the same buffer to get the number of elements between them, just like you can with iterators, and just like with iterators, if the pointers point into separate buffers, you can not meaningfully compare them. (For a practical example of why not, consider what happens in a segmented memory space. What's the distance between two pointers pointing to separate segments?)
Of course in practice, there's a very close correlation between CPU addresses and C/C++ pointers. But they're not exactly the same thing. Pointers have a few limitations that may not be strictly necessary on your CPU.
Of course, most C++ programmers muddle by on the first understanding, even though it's technically incorrect. It's typically close enough to how your code ends up behaving that people think they get it, and move on.
But for someone coming from Java, and just learning about pointers from scratch, the latter explanation may be just as easily understood, and it's going to spring fewer surprises on them later.
This is one pretty good at link here about Pointer Arithmetic
For example:
Pointer and array
Formula for computing the address of ptr + i where ptr has type T *. then the formula for the address is:
addr( ptr + i ) = addr( ptr ) + [ sizeof( T ) * i ]
For for type of int on 32bit platform, addr(ptr+i) = addr(ptr)+4*i;
Subtraction
We can also compute ptr - i. For example, suppose we have an int array called arr.
int arr[ 10 ] ;
int * p1, * p2 ;
p1 = arr + 3 ; // p1 == & arr[ 3 ]
p2 = p1 - 2 ; // p1 == & arr[ 1 ]

Resources