751 questions
1823 votes
20 answers
136k views
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 ...
254 votes
10 answers
190k views
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, ...
136 votes
5 answers
382k views
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 ...
136 votes
3 answers
10k views
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 ...
64 votes
7 answers
124k views
Pointer Arithmetic [closed]
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++.
55 votes
8 answers
4k views
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 *(...
54 votes
5 answers
6k views
Does the C standard permit assigning an arbitrary value to a pointer and incrementing it?
Is the behaviour of this code well defined? #include <stdio.h> #include <stdint.h> int main(void) { void *ptr = (char *)0x01; size_t val; ptr = (char *)ptr + 1; val = (...
51 votes
4 answers
3k views
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 ...
47 votes
5 answers
3k views
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 ...
42 votes
2 answers
25k views
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 ...
39 votes
1 answer
14k views
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 [...
38 votes
12 answers
13k views
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 ...
33 votes
1 answer
2k views
Set shared_ptr with new_pointer that is old_pointer + offset
Here is a smart pointer: std::shared_ptr<char> 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 ...
33 votes
2 answers
1k views
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_...
30 votes
5 answers
33k views
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 ...