pointer-arithmetic

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

家住魔仙堡 提交于 2019-12-05 00:41:14
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). ptrdiff_t index = pointer_found - array_name; 来源: https://stackoverflow.com/questions/2711653/c-how-to-convert-a-pointer-in-an-array-to-an-index

Is taking the address of a local variable a constant expression in C++11?

我是研究僧i 提交于 2019-12-04 16:11:07
问题 The following C++11 program: int x = 42; void f() { int y = 43; static_assert(&x < &y, "foo"); } int main() { f(); } Doesn't compile with gcc 4.7 as it complains: error: ‘&y’ is not a constant expression This would agree with my intuition. The address of y potentially changes with each invocation of f , so of course it cannot be calculated during translation. However none of the bullet points in 5.19 [expr.const] seem to preclude it from being a constant expression. The only two contenders I

Array-syntax vs pointer-syntax and code generation?

我的梦境 提交于 2019-12-04 14:55:43
问题 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 *(vector+i) . The notation vector[i] generates machine code that starts at location vector , moves i positions from this location, and uses its content. The notation *(vector+i) generates machine code that starts at location vector , adds i to the address, and then uses the contents at that address.

Can one get a pointer to a complete object representation element from a pointer to a suboject?

霸气de小男生 提交于 2019-12-04 09:03:25
Let's consider this code: int i; int is[10]{}; unsigned char * p = reinterpret_cast<unsigned char*>(&i); //p defined to point to the object-representation of the first element of array ints unsigned char * ps = reinterpret_cast<unsigned char*>(&is[0]); p += sizeof(int); ps += sizeof(int); //now ps points to the end of ints[0] and p point to the end of i; p += sizeof(int); //Undefined behavior according to [expr.add] ps += sizeof(int); //Undefined behavior? unsigned char c = *ps;//Undefined behavior? If we consider that ps points to the object-representation of is[0] then according to pointer

difference between *y++ and ++*y?

守給你的承諾、 提交于 2019-12-04 07:43:07
I'm confused in how this code will get executed. Suppose we have int x=30,*y,*z; y=&x; what is the difference between *y++ and ++*y? and also what will be the output of this program? #include<stdio.h> int main(){ int x=30,*y,*z; y=&x; z=y; *y++=*z++; x++; printf("%d %d %d ",x,y,z); return 0; } The expression x = *y++ is in effects same as: x = *y; y = y + 1; And if expression is just *y++; (without assignment) then its nothing but same as y++; , that is y start pointing to next location after increment. Second expression ++*y means to increment the value pointed by y that same as: *y = *y + 1;

Is pointer arithmetic possible with C++ string class?

可紊 提交于 2019-12-04 05:10:57
问题 After programming a little in C I decided to jump right into C++. At first I was pleased with the presence of the string class and being able to treat strings as whole units instead of arrays of characters. But I soon found that the C-style strings had the advantage of letting the program move through it character by character, using pointer arithmetic, and carry out a desired logical operation. I have now found myself in a situation that requires this but the compiler tells me it is unable

Pointer arithmetic across subobject boundaries

三世轮回 提交于 2019-12-03 22:16:13
Does the following code (which performs pointer arithmetic across subobject boundaries) have well-defined behavior for types T for which it compiles (which, in C++11, does not not necessarily have to be POD ) or any subset thereof? #include <cassert> #include <cstddef> template<typename T> struct Base { // ensure alignment union { T initial; char begin; }; }; template<typename T, size_t N> struct Derived : public Base<T> { T rest[N - 1]; char end; }; int main() { Derived<float, 10> d; assert(&d.rest[9] - &d.initial == 10); assert(&d.end - &d.begin == sizeof(float) * 10); return 0; } LLVM uses

Can ptrdiff_t represent all subtractions of pointers to elements of the same array object?

社会主义新天地 提交于 2019-12-03 22:05:40
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_­t , the behavior is undefined. —  end note ] But given [support.types.layout#2] , which states that ( emphasis mine): The type ptrdiff_­t is an implementation-defined signed integer type that can hold the difference of two subscripts in an array object, as described in [expr.add] . Is it even possible for the result of i-j not to be in the range of representable values of ptrdiff_t ? PS: I

Array-syntax vs pointer-syntax and code generation?

烈酒焚心 提交于 2019-12-03 09:17:46
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 *(vector+i) . The notation vector[i] generates machine code that starts at location vector , moves i positions from this location, and uses its content. The notation *(vector+i) generates machine code that starts at location vector , adds i to the address, and then uses the contents at that address. While the result is the same, the generated machine code is different. This difference is rarely of

Portable and safe way to add byte offset to any pointer

独自空忆成欢 提交于 2019-12-03 06:50:00
问题 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 of any type in C++11? SomeType* ptr; int offset = 12345 /* bytes */; ptr = ptr + offset; // <-- I found many answers on Stack Overflow and Google, but they all propose different things. Some variants I have encountered: Cast to char *: ptr = (SomeType*)(((char*)ptr) + offset); Cast to unsigned int