pointer-arithmetic

In C arrays why is this true? 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] == 5[a] ? Edit: The accepted answer is great. For a lower level view of how this works, se...

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++. ...

What are convincing examples where pointer arithmetic is preferable to array subscripting?

I'm preparing some slides for an introductory C class, and I'm trying to present good examples (and motivation) for using pointer arithmetic over array subscripting. A lot of the examples I see in books are fairly equivalent. For example, many books show how to reverse the case of all values in a string, but with the exception of replac...

Pointer Arithmetic In C.

Consider the following code fragment: int (*p)[3]; int (*q)[3]; q = p; q++; printf("%d, %d\n", q, p); printf("%d\n", q-p); I know that pointer arithmetic is intelligent, meaning that the operation q++ advances q enough bytes ahead to point to a next 3-integers-array, so it does not surprises me that the first print is '12, 0' which m...

What C compilers have pointer subtraction underflows?

So, as I learned from Michael Burr's comments to this answer, the C standard doesn't support integer subtraction from pointers past the first element in an array (which I suppose includes any allocated memory). From section 6.5.6 of the combined C99 + TC1 + TC2 (pdf): If both the pointer operand and the result point to elements of t...

C - element beyond the end of an array

I've been reading K & R's book on C, and found that pointer arithmetic in C allows access to one element beyond the end of an array. I know C allows to do almost anything with memory but I just don't understand, what is the purpose of this peculiarity? ...

How does a hardware trap in a three-past-the-end pointer happen even if the pointer is never dereferenced?

In his November 1, 2005 C++ column, Herb Sutter writes ... int A[17]; int* endA = A + 17; for( int* ptr = A; ptr < endA; ptr += 5 ) { // ... } [O]n some CPU architectures, including current ones, the aforementioned code can cause a hardware trap to occur at the point where the three-past-the-end pointer is created, whethe...

Increase a struct pointer with half the struct size

I just got an interesting problem to take care of, and I see no neat way to solve it. I have two base data structures that represents a complex graph, declared something like this: typedef struct _node_t node_t; typedef struct _graph_t graph_t; struct { /* Data fields omitted */ node_t * pNextByLevel; node_t * pNextByProx...

Pointer Arithmetic and Navigating malloc'ed arrays.

Okay, so I'm working on an OpenGL ES application for the iPhone, and I ran into an interesting issue. I have a function that computes the vertices, normals, and texture coordinates of a sphere dependent upon a detail level and a range of spherical coordinates. Originally, storing a vertex in an array looked something like this: //Afte...

pointer arithmetic on vectors in c++

i have a std::vector, namely vector<vector<vector> > > mdata; i want pass data from my mdata vector to the GSL function gsl_spline_init(gsl_spline * spline, const double xa[], const double ya[], size_t size); as ya. i already figured out that i can do things like gsl_spline_init(spline, &(mgrid.front()), &(mdata[i][j][k].front()),...

Doing pointer math in a c++ class: Is it "legit"?

Ah-hoi, hoi, I'm wondering if it's ok to do something like the following: class SomeClass { int bar; }; SomeClass* foo = new SomeClass(); int offset = &(foo->bar) - foo; SomeClass* another = new SomeClass(); *(another+offset) = 3; // try to set bar to 3 Just Curious, Dan O ...

Cleaner pointer arithmetic syntax for manipulation with byte offsets

In the following lines of code, I need to adjust the pointer pm by an offset in bytes in one of its fields. Is there an better/easier way to do this, than incessantly casting back and forth from char * and PartitionMap * such that the pointer arithmetic still works out? PartitionMap *pm(reinterpret_cast<PartitionMap *>(partitionMaps)); ...

Implementing Win32 FileWrite

[DllImport("kernel32.dll", SetLastError=true)] public static extern unsafe bool WriteFile(IntPtr hFile, void* lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, IntPtr lpOverlapped); I am implementing this through a Write(..) method with a signature: Write(IntPtr handleFile, void* bufferData, uint length){ ...

void has unknown size in Visual C++

In Visual Studio C++ version 9 (and probably other versions too), the following code: int a = sizeof(void); void const *b = static_cast<void const *>("hello world"); b += 6; Generates these errors: error C2070: 'void': illegal sizeof operand error C2036: 'const void *' : unknown size This code works under GCC, which treats sizeof(v...

Byte precision pointer arithmetic in C when sizeof(char) != 1

How can one portably perform pointer arithmetic with single byte precision? Keep in mind that: char is not 1 byte on all platforms sizeof(void) == 1 is only available as an extension in GCC While some platforms may have pointer deref pointer alignment restrictions, arithmetic may still require a finer granularity than the size of the ...

Arithmetic with pointer types/

Some examples of adding and subtracting similarly typed pointers, using numeric and character pointers please. Using C. Thanks. ...

C pointer arithmetic

Given this code: int *p, *q; p = (int *) 1000; q = (int *) 2000; What is q - p and how? ...

Copying a string in C

Hi, I am confused about this code: (http://www.joelonsoftware.com/articles/CollegeAdvice.html) while (*s++ = *t++); What is the order of execution? Is *s = *t first done, and then are they each incremented? Or other way around? Thanks. EDIT: And what if it was: while(*(s++) = *(t++)); and while(++*s = ++*t); ...

Pointer arithmetic and arrays: what's really legal?

Consider the following statements: int *pFarr, *pVarr; int farr[3] = {11,22,33}; int varr[3] = {7,8,9}; pFarr = &(farr[0]); pVarr = varr; At this stage, both pointers are pointing at the start of each respective array address. For *pFarr, we are presently looking at 11 and for *pVarr, 7. Equally, if I request the contents...

C++ arithmetic with pointers

I am trying to add the following: I have an array of double pointers call A. I have another array of double pointers call it B, and I have an unsigned int call it C. So I want to do: A[i] = B[i] - C; how do I do it? I did: A[i] = &B[i] - C; I don't think I am doing this correctly. Edit: What I want to do is, take the value at in...