pointer-arithmetic

How exactly pointer subtraction works in case of integer array?

自古美人都是妖i 提交于 2019-11-30 07:42:46
问题 #include<stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50, 60}; int *ptr1 = arr; int *ptr2 = arr + 5; printf("Number of elements between two pointer are: %d.", (ptr2 - ptr1)); printf("Number of bytes between two pointers are: %d", (char*)ptr2 - (char*) ptr1); return 0; } For the first printf() statement the output will be 5 according to Pointer subtraction confusion What about the second printf() statement, what will be the output? 回答1: To quote C11 , chapter §6.5.6, Additive operators

Pointer arithmetic in Go

时光毁灭记忆、已成空白 提交于 2019-11-30 06:10:10
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 days but I'm just curious if it's possible. No. From the Go FAQ : Why is there no pointer arithmetic? Safety. Without pointer arithmetic it's possible to create a language that can never derive an illegal address that succeeds incorrectly. Compiler and hardware technology have advanced to the point where a loop using array indices can be as efficient

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

会有一股神秘感。 提交于 2019-11-30 04:02:28
问题 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, whether that pointer is ever dereferenced or not. How does a CPU trap on a bitpattern? What about ... int A[17]; // (i) hardware will trap this ? int *pUgly = A + 18; // (ii)

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

走远了吗. 提交于 2019-11-29 13:48:34
问题 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 replacing an a[i] with a *p the code is identical. I am looking for a good (and short) example with single-dimensional arrays where pointer arithmetic can produce

Is pointer arithmetic on inactive member of a union UB?

这一生的挚爱 提交于 2019-11-29 09:25:21
Let's consider this example code: struct sso { union { struct { char* ptr; char size_r[8]; } large_str; char short_str[16]; }; const char* get_tag_ptr() const { return short_str+15; } }; In [basic.expr] it is specified that pointer arithmetic is allowed as long as the result points to another element of the array (or past the end of an object or of the last element). Nevertheless it is not specified in this setion what happens if the array is an inactive member of a union. I believe it is not an issue short_str+15 is never UB. Is it right? The following question clearly showes my intent

Is apparent NULL pointer dereference in C actually pointer arithmetic?

三世轮回 提交于 2019-11-29 07:16:09
I've got this piece of code. It appears to dereference a null pointer here, but then bitwise-ANDs the result with unsigned int . I really don't understand the whole part. What is it intended to do? Is this a form of pointer arithmetic? struct hi { long a; int b; long c; }; int main() { struct hi ob={3,4,5}; struct hi *ptr=&ob; int num= (unsigned int) & (((struct hi *)0)->b); printf("%d",num); printf("%d",*(int *)((char *)ptr + (unsigned int) & (((struct hi *)0)->b))); } The output I get is 44. But how does it work? This is not an "and", this is taking the address of the right hand side

Why subtract null pointer in offsetof()?

与世无争的帅哥 提交于 2019-11-29 07:13:40
Linux's stddef.h defines offsetof() as: #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) whereas the Wikipedia article on offsetof() ( http://en.wikipedia.org/wiki/Offsetof ) defines it as: #define offsetof(st, m) \ ((size_t) ( (char *)&((st *)(0))->m - (char *)0 )) Why subtract (char *)0 in the Wikipedia version? Is there any case where that would actually make a difference? The first version converts a pointer into an integer with a cast, which is not portable. The second version is more portable across a wider variety of compilers, because it relies on pointer arithmetic by

Why does i[arr] work as well as arr[i] in C with larger data types?

孤街醉人 提交于 2019-11-29 03:57:14
It's fairly common knowledge that if you access an element of an array as arr[i] in C that you can also access the element as i[arr] , because these just boil down to *(arr + i) and addition is commutative. My question is why this works for data types larger than char , because sizeof(char) is 1, and to me this should advance the pointer by just one char. Perhaps this example makes it clearer: #include <string.h> #include <stdio.h> struct large { char data[1024]; }; int main( int argc, char * argv[] ) { struct large arr[4]; memset( arr, 0, sizeof( arr ) ); printf( "%lu\n", sizeof( arr ) ); //

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

风格不统一 提交于 2019-11-28 15:28:54
问题 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 <stdio.h> int main() { int a[] = {100, 200, 300, 400, 500}; int size = 0; size = *(&a + 1) - a; printf("%d\n", size); return 0; } As expected, it returns 5. edit: people pointed out this answer, but the syntax does differ a bit, i.e. the indexing method size = (&arr)[1] -

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

久未见 提交于 2019-11-28 13:05:40
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 smallest fundamental POD type Your assumption is flawed - sizeof(char) is defined to be 1 everywhere. From the C99 standard (TC3) , in section 6.5.3.4 ("The sizeof operator"): (paragraph 2) The sizeof operator yields the size (in bytes) of its operand, which may