pointer-arithmetic

Do pointers support “array style indexing”?

喜夏-厌秋 提交于 2019-11-27 03:40:56
问题 (Self-answered Q&A - this matter keeps popping up) I assume that the reader is aware of how pointer arithmetic works. int arr[3] = {1,2,3}; int* ptr = arr; ... *(ptr + i) = value; Teachers/C books keep telling me I shouldn't use *(ptr + i) like in the above example, because "pointers support array style indexing" and I should be using ptr[i] = value; instead. No argument there - much easier to read. But looking through the C standard, I find nothing called "array style indexing". In fact, the

Pointer/Address difference [duplicate]

半城伤御伤魂 提交于 2019-11-27 02:10:28
This question already has an answer here: C/C++: Pointer Arithmetic 6 answers Why is the difference between the two addresses wrong? http://codepad.org/NGDqFWjJ #include<stdio.h> int main() { int i = 10, j = 20; int *p = &i; int *q = &j; int c = p - q; printf("%d\n", p); printf("%d\n", q); printf("%d", c); return 0; } Output: -1083846364 -1083846368 1 First, pointer arithmetic isn't defined when performed on unrelated pointers. Second, it makes sense. When subtracting pointers you get the number of elements between those addresses, not the number of bytes. If you were to try that with char *p1

Access element beyond the end of an array in C

元气小坏坏 提交于 2019-11-27 02:04:50
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? C doesn't allow access to memory beyond the end of the array. It does, however, allow a pointer to point at one element beyond the end of the array. The distinction is important. Thus, this is OK: char array[N]; char *p; char *end; for (p = array, end = array + N; p < end; ++p) do_something(p); (Doing *end would be an error.) And that shows the

Is the “one-past-the-end” pointer of a non-array type a valid concept in C++?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 02:03:44
The C++ standard [sec 5.7] says: If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. So, am I correct in assuming that pointers one-past-the-end of other types than arrays are undefined? For example: int a = 0; vector<int> v(&a, (&a)+1); The above snippet compiles and works just fine (with g++), but is it valid? No, it is legal. 5.7(4) - one paragraph before your quote - says: "For the purposes of these operators, a pointer to

C pointer arithmetic

a 夏天 提交于 2019-11-26 20:17:56
问题 Given this code: int *p, *q; p = (int *) 1000; q = (int *) 2000; What is q - p and how? 回答1: It's actually undefined, according to the standard. Pointer arithmetic is not guaranteed to work unless the pointers are both pointing to either an element in, or just beyond, the same array. The relevant section of the standard is 6.5.6:9 (n1362 draft of c1x but this hasn't changed since c99) which states: When two pointers are subtracted, both shall point to elements of the same array object, or one

What is the rationale for limitations on pointer arithmetic or comparison?

有些话、适合烂在心里 提交于 2019-11-26 18:15:23
问题 In C/C++, addition or subtraction on pointer is defined only if the resulting pointer lies within the original pointed complete object. Moreover, comparison of two pointers can only be performed if the two pointed objects are subobjects of a unique complete object . What are the reasons of such limitations? I supposed that segmented memory model (see here §1.2.1) could be one of the reasons but since compilers can actually define a total order on all pointers as demonstrated by this answer, I

Pointer Arithmetic In C

风流意气都作罢 提交于 2019-11-26 11:53:41
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 means that incrementing q made it larger in 12. But the second print does surprises me. It prints 1! So why would it print 1 instead of 12? it just puzzles me. Like the ++ increment operator, the - subtraction operator with pointers also takes into account the

Address canonical form and pointer arithmetic

。_饼干妹妹 提交于 2019-11-26 11:19:15
问题 On AMD64 compliant architectures, addresses need to be in canonical form before being dereferenced. From the Intel manual, section 3.3.7.1: In 64-bit mode, an address is considered to be in canonical form if address bits 63 through to the most-significant implemented bit by the microarchitecture are set to either all ones or all zeros. Now, the most significat implemented bit on current operating systems and architectures is the 47th bit. This leaves us with a 48-bit address space. Especially

Access element beyond the end of an array in C

雨燕双飞 提交于 2019-11-26 09:53:50
问题 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? 回答1: C doesn't allow access to memory beyond the end of the array. It does, however, allow a pointer to point at one element beyond the end of the array. The distinction is important. Thus, this is OK: char array[N]; char *p; char *end; for

What is the result of NULL + int?

天涯浪子 提交于 2019-11-26 04:42:18
问题 I have seen the following macro being used in OpenGL VBO implementations: #define BUFFER_OFFSET(i) ((char *)NULL + (i)) //... glNormalPointer(GL_FLOAT, 32, BUFFER_OFFSET(x)); Could you provide a little detail on how this macro works? Can it be replaced with a function? More exactly, what is the result of incrementing a NULL pointer? 回答1: Let's take a trip back through the sordid history of OpenGL. Once upon a time, there was OpenGL 1.0. You used glBegin and glEnd to do drawing, and that was