pointer-arithmetic

Pointer arithmetic for structs

血红的双手。 提交于 2020-01-01 04:16:12
问题 Given a struct definition that contains one double and three int variables (4 variables in all), if p is a pointer to this struct with a value 0x1000, what value does p++ have? This is not a homework problem, so don't worry. I'm just trying to prepare for a test and I can't figure out this practice problem. Thanks This is in C. Yes I want the value of p after it is incremented. This is a 32-bit machine 回答1: struct foobar *p; p = 0x1000; p++; is the same as struct foobar *p; p = 0x1000 +

Should this be called some special case of object slicing?

折月煮酒 提交于 2019-12-31 11:40:58
问题 Let's say I have a class Derived which derives from class Base whereas sizeof(Derived) > sizeof(Base) . Now, if one allocates an array of Derived like this: Base * myArray = new Derived[42]; and then attempts to access the n -th object using doSomethingWithBase(myArray[n]); Then this is might likely (but not always) cause undefined behaviour due to accessing Base from an invalid location. What is the correct term for such an programming error? Should it be considered a case of object slicing?

Can std::uintptr_t be used to avoid undefined behavior of out-of-bounds pointer arithmetic?

邮差的信 提交于 2019-12-30 08:34:24
问题 Now we know that doing out-of-bounds-pointer-arithmetic has undefined behavior as described in this SO question. My question is: can we workaround such restriction by casting to std::uintptr_t for arithmetic operations and then cast back to pointer? is that guaranteed to work? For example: char a[5]; auto u = reinterpret_cast<std::uintptr_t>(a) - 1; auto p = reinterpret_cast<char*>(u + 1); // OK? The real world usage is for optimizing offsetted memory access -- instead of p[n + offset] , I

Accessing array values via pointer arithmetic vs. subscripting in C

社会主义新天地 提交于 2019-12-29 10:27:38
问题 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 Objective-C and Cocoa on Macs? Which is the preferred coding style for array access, in both C and Objective-C? Which is considered (by professionals of their respective languages) more legible, more "correct" (for lack of a better term)? 回答1: You

Why memory address contained by pointer +1 is different from address of value being pointed + 1

最后都变了- 提交于 2019-12-24 01:32:12
问题 Pointer stores memory address of value being pointed at so memory address contained by pointer is same as memory address of value. So adding 1 to both these memory addresses should yield same result, which is not happening. Why? Here is the code int main() { int ages[] = {23, 43, 12, 89, 2}; int *cur_ages = ages; printf("\n&cur_ages=%p, &cur_ages+1=%p", &cur_ages, &cur_ages+1); printf("\n&ages=%p, &ages+1=%p", &ages, &ages+1); printf("\ncur_ages=%p, cur_ages+1=%p", cur_ages, cur_ages+1);

Why memory address contained by pointer +1 is different from address of value being pointed + 1

爷,独闯天下 提交于 2019-12-24 01:31:52
问题 Pointer stores memory address of value being pointed at so memory address contained by pointer is same as memory address of value. So adding 1 to both these memory addresses should yield same result, which is not happening. Why? Here is the code int main() { int ages[] = {23, 43, 12, 89, 2}; int *cur_ages = ages; printf("\n&cur_ages=%p, &cur_ages+1=%p", &cur_ages, &cur_ages+1); printf("\n&ages=%p, &ages+1=%p", &ages, &ages+1); printf("\ncur_ages=%p, cur_ages+1=%p", cur_ages, cur_ages+1);

(2 - 4 = -1) when int value assigned to pointer in C?

人盡茶涼 提交于 2019-12-23 22:07:28
问题 I am unable to get that why in this program 2 - 4 gives -1, it has assigned int values to pointers rather than addresses, I know but while I compiled it compiler gave some warnings but compiled the program and it executed but... Program #include<stdio.h> int main(void) { int *p, *q; int arr[] = {1,2,3,4}; // I know p and q are pointers and address should be assigned to them // but look at output, why it evaluates (p-q) to -1 while p as 2 and q as 4 p = arr[1]; q = arr[3]; printf("P-Q: %d, P:

Subtraction between pointers of different type [duplicate]

主宰稳场 提交于 2019-12-22 11:11:53
问题 This question already has answers here : Pointer/Address difference [duplicate] (3 answers) Closed 3 years ago . I'm trying to find the distance in memory between two variables. Specifically I need to find the distance between a char[] array and an int. char data[5]; int a = 0; printf("%p\n%p\n", &data[5], &a); long int distance = &a - &data[5]; printf("%ld\n", distance); When I run my my program without the last two lines I get the proper memory address of the two variables, something like

Why scanf(“%s”,&str); behaves as scanf(“%s”,str);? [duplicate]

大兔子大兔子 提交于 2019-12-22 10:06:54
问题 This question already has answers here : How come an array's address is equal to its value in C? (6 answers) Closed 3 years ago . Look at the following code: #include <stdio.h> int main() { char str[80]; int n; scanf("%s%n",str,&n); printf("%s\t%d",str,n); putchar('\n'); getchar(); //to remove '\n' scanf("%s%n",&str,&n); printf("%s\t%d",str,n); return 0; } Here is the input and output: abc abc 3 123 123 3 As we know, scanf is a variable parametric function, so its parameters will not be cast

C - how to convert a pointer in an array to an index?

你。 提交于 2019-12-22 03:52:49
问题 In the many search functions of C (bsearch comes to mind) if a result is found, a pointer to the spot in the array is returned. How can I convert this pointer to the index in the array that was searched (using pointer arithmetic, i assume). 回答1: ptrdiff_t index = pointer_found - array_name; 来源: https://stackoverflow.com/questions/2711653/c-how-to-convert-a-pointer-in-an-array-to-an-index