pointer-arithmetic

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

半世苍凉 提交于 2019-12-21 17:52:42
问题 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

Is “int *ptr = *( ( &a ) + 1 );” where “a” is int[5] well-defined by the Standard?

一曲冷凌霜 提交于 2019-12-21 07:57:42
问题 Based on this Question ( strange output issue in c) there was an Answer ( provided by @Lundin ) about this line: int *ptr = (int*)(&a+1); where he said: the cast (int*) was hiding this bug . So I came with the following: #include <stdio.h> int main( void ){ int a[5] = {1,2,3,4,5}; int *ptr = *( ( &a ) + 1 ); printf("%d", *(ptr-1) ); } I would like to know if this: int *ptr = *( ( &a ) + 1 ); Is well-defined by the Standard? EDIT : At some point @chux pointed to §6.3.2.3.7 which is: A pointer

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

孤街醉人 提交于 2019-12-21 07:02:15
问题 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

pointer arithmetic in C for getting a string

你离开我真会死。 提交于 2019-12-20 06:28:16
问题 I want to get the elements of an array of characters, but no success at all, the problem is that I only get the first and last element and nothing more, my code is: void getcharacters(char *cad) { int l; int *i; l=strlen(cad); for (i=&cad[0];i<&cad[l];i++){ printf("%c\n",*cad); } } any help? Thanks 回答1: You are using the same character ( *cad or cad[0] ) for all printf's. What you need is to use the index to get the next char in each iteration. Also i needs to be a pointer to char: void

Increment operator on pointer of array errors?

房东的猫 提交于 2019-12-19 19:45:00
问题 I'm trying something very simple, well supposed to be simple but it somehow is messing with me... I am trying to understand the effect of ++ on arrays when treated as pointers and pointers when treated as arrays. So, int main() { int a[4] = { 1, 4, 7, 9 }; *a = 3; *(a+1) = 4; *++a = 4; //compiler error } 1: So at *(a+1)=4 we set a[1]=4; //Happy But when *++a = 4; , I'd expect pointer a to be incremented one since ++ is precedent to * and then * kicks in and we make it equal to 4. But this

Increment operator on pointer of array errors?

自古美人都是妖i 提交于 2019-12-19 19:44:58
问题 I'm trying something very simple, well supposed to be simple but it somehow is messing with me... I am trying to understand the effect of ++ on arrays when treated as pointers and pointers when treated as arrays. So, int main() { int a[4] = { 1, 4, 7, 9 }; *a = 3; *(a+1) = 4; *++a = 4; //compiler error } 1: So at *(a+1)=4 we set a[1]=4; //Happy But when *++a = 4; , I'd expect pointer a to be incremented one since ++ is precedent to * and then * kicks in and we make it equal to 4. But this

C weird array syntax in multi-dimensional arrays

风格不统一 提交于 2019-12-18 13:09:14
问题 I've known that this is true: x[4] == 4[x] What is the equivalent for multi-dimensional arrays? Is the following true? x[4][3] == 3[x[4]] == 3[4[x]] 回答1: x[y] is defined as *(x + (y)) x[y][z] would become *(*(x + (y)) + z) x[y[z]] would become *(x + (*(y + (z)))) x[4][3] would become *(*(x + (4)) + 3) would become *(*(x + 4) + 3) 3[x[4]] would become *(3 + (*(x + (4)))) would become *(*(x + 4) + 3) 3[4[x]] would become *(3 + (*(4 + (x)))) would become *(*(x + 4) + 3) Which means they are all

Is apparent NULL pointer dereference in C actually pointer arithmetic?

﹥>﹥吖頭↗ 提交于 2019-12-18 05:01:54
问题 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

Is apparent NULL pointer dereference in C actually pointer arithmetic?

拜拜、爱过 提交于 2019-12-18 05:01:43
问题 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

Is one-past-end pointer OK for non-array object types?

穿精又带淫゛_ 提交于 2019-12-18 05:01:27
问题 Is this valid C++? int main() { int i = 0; int* pi = &i; ++pi; } I know that one-past-end pointers are allowed for array types, but I'm not sure in this case. Does that code technically have undefined behavior? 回答1: Yes, it is okay. It is one of the four categories of values any pointer type may hold. [basic.compound] (emphasis mine) 3 Every value of pointer type is one of the following: a pointer to an object or function (the pointer is said to point to the object or function), or a pointer