Are the following statements using pointer arithmetic that includes binding?

char array[8] = "Raining";

I think that all the comments below that are consistent with the statements are true to my understanding.

char *p1 = array + 7;      --> points to '\0'

char *p2 = array + 8;      --> undefined behaviour

char *p3 = array + 9;      --> undefined behaviour

char *p4 = array + 10;     --> undefined behaviour

Do I understand correctly?

+4
source share
3 answers

In your case

  char *p1 = array + 7;

and

  char *p2 = array + 8;

valid because you are allowed to point to

  • any object inside the length of the array
  • after the last element of the array until you find it .

Oto

  char *p3 = array + 9;

and

  char *p4 = array + 10; 

are undefined.


Quote C11, chapter §6.5.6, “Additive operators” (emphasis)

[ C99, §6.5.6/p8, - ]

, , , result . , , . , P i - , (P)+N (, N+(P)) (P)-N ( N N) i+n -th i−n -th , . , P , (P)+1 , Q , (Q)-1 . , ; undefined. [...]

+14

array 8 - NULL-.

char *p1 = array + 7; .

char *p2 = array + 8; . . ( .)

: undefined. .

+5

, . .

char *p1 = array + 7;   \\-->points to '\0', within array

char *p2 = array + 8;   \\-->defined behaviour until dereferenced 

, , , , , (*d++ = *s++);.

enter image description here

, ( ) ( ) : [beginning,end).

+4

Source: https://habr.com/ru/post/1655038/


All Articles