Questions tagged [pointer-arithmetic]

You can perform a limited number of arithmetic operations on pointers. These operations are: increment, decrement, addition, subtraction, comparison and assignment.

You can perform a limited number of arithmetic operations on pointers. These operations are:

  • Increment and decrement
  • Addition and subtraction
  • Comparison
  • Assignment

The increment (++) operator increases the value of a pointer by the size of the data object the pointer refers to. For example, if the pointer refers to the second element in an array, the ++ makes the pointer refer to the third element in the array.

The decrement (--) operator decreases the value of a pointer by the size of the data object the pointer refers to. For example, if the pointer refers to the second element in an array, the -- makes the pointer refer to the first element in the array.

You can add an integer to a pointer but you cannot add a pointer to a pointer.

If the pointer p points to the first element in an array, the following expression causes the pointer to point to the third element in the same array:

p = p + 2;

If you have two pointers that point to the same array, you can subtract one pointer from the other. This operation yields the number of elements in the array that separate the two addresses that the pointers refer to.

You can compare two pointers with the following operators: ==, !=, <, >, <=, and >=.

Pointer comparisons are defined only when the pointers point to elements of the same array. Pointer comparisons using the == and != operators can be performed even when the pointers point to elements of different arrays.

You can assign to a pointer the address of a data object, the value of another compatible pointer or the NULL pointer.

Source:http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc03ptarith.htm

718 questions
1794
votes
20 answers

With arrays, why is it the case that a[5] == 5[a]?

As Joel points out in Stack Overflow podcast #34, in C Programming Language (aka: K & R), there is mention of this property of arrays in C: a[5] == 5[a] Joel says that it's because of pointer arithmetic but I still don't understand. Why does a[5] ==…
Dinah
  • 52,922
  • 30
  • 133
  • 149
238
votes
10 answers

Pointer arithmetic for void pointer in C

When a pointer to a particular type (say int, char, float, ..) is incremented, its value is increased by the size of that data type. If a void pointer which points to data of size x is incremented, how does it get to point x bytes ahead? How does…
Siva Sankaran
  • 2,407
  • 2
  • 15
  • 4
136
votes
3 answers

How does this piece of code determine array size without using sizeof( )?

Going through some C interview questions, I've found a question stating "How to find the size of an array in C without using the sizeof operator?", with the following solution. It works, but I cannot understand why. #include int main() { …
janojlic
  • 1,299
  • 1
  • 11
  • 10
128
votes
5 answers

How to increment a pointer address and pointer's value?

Let us assume, int *p; int a = 100; p = &a; What will the following code actually do and how? p++; ++p; ++*p; ++(*p); ++*(p); *p++; (*p)++; *(p)++; *++p; *(++p); I know, this is kind of messy in terms of coding, but I want to know what will…
Dinesh
  • 16,014
  • 23
  • 80
  • 122
65
votes
7 answers

Pointer Arithmetic

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++.
leora
  • 188,729
  • 360
  • 878
  • 1,366
55
votes
8 answers

Array-syntax vs pointer-syntax and code generation?

In the book, "Understanding and Using C Pointers" by Richard Reese it says on page 85, int vector[5] = {1, 2, 3, 4, 5}; The code generated by vector[i] is different from the code generated by *(vector+i) . The notation vector[i] generates machine…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
54
votes
5 answers

Does the C standard permit assigning an arbitrary value to a pointer and incrementing it?

Is the behaviour of this code well defined? #include #include int main(void) { void *ptr = (char *)0x01; size_t val; ptr = (char *)ptr + 1; val = (size_t)(uintptr_t)ptr; printf("%zu\n", val); return…
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
51
votes
4 answers

Pointer arithmetics with two different buffers

Consider the following code: int* p1 = new int[100]; int* p2 = new int[100]; const ptrdiff_t ptrDiff = p1 - p2; int* p1_42 = &(p1[42]); int* p2_42 = p1_42 + ptrDiff; Now, does the Standard guarantee that p2_42 points to p2[42]? If not, is it…
Sergey
  • 7,985
  • 4
  • 48
  • 80
47
votes
5 answers

For a pointer p, could p < p+1 be false in an extreme case?

Is it possible, for a pointer variable p, that p<(p+1) is false? Please explain your answer. If yes, under which circumstances can this happen? I was wondering whether p+1 could overflow and be equal to 0. E.g. On a 64-bit PC with GCC-4.8 for a…
akarapatis
  • 852
  • 8
  • 18
39
votes
11 answers

Accessing array values via pointer arithmetic vs. subscripting in C

I keep reading that, in C, using pointer arithmetic is generally faster than subscripting for array access. Is this true even with modern (supposedly-optimizing) compilers? If so, is this still the case as I begin to move away from learning C into…
John Rudy
  • 37,282
  • 14
  • 64
  • 100
38
votes
2 answers

Pointer arithmetic in Go

Considering you can (can't think of a great way to put it, but) manipulate pointers in Go, is it possible to perform pointer arithmetic like you would in C, say for iterating over an array? I know loops are just fine for that kind of things these…
user5360268
35
votes
1 answer

How to avoid pointer arithmetic when using char** argv

When trying to print the first command line argument: std::cout << argv[0] << std::endl; clang-tidy gives the warning: warning: 'do not use pointer arithmetic' from [cppcoreguidelines-pro-bounds-pointer-arithmetic] Is there an alternative…
33
votes
2 answers

Can ptrdiff_t represent all subtractions of pointers to elements of the same array object?

For subtraction of pointers i and j to elements of the same array object the note in [expr.add#5] reads: [ Note: If the value i−j is not in the range of representable values of type std​::​ptrdiff_­t, the behavior is undefined. — end note ] But…
jotik
  • 17,044
  • 13
  • 58
  • 123
32
votes
1 answer

Set shared_ptr with new_pointer that is old_pointer + offset

Here is a smart pointer: std::shared_ptr p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header…
Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
29
votes
5 answers

Portable and safe way to add byte offset to any pointer

I'm quite new at working with C++ and haven't grasped all the intricacies and subtleties of the language. What is the most portable, correct and safe way to add an arbitrary byte offset to a pointer of any type in C++11? SomeType* ptr; int offset =…
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
1
2 3
47 48