Perché il programma,Incremento puntatori char in C++
char *s, *p, c;
s = "abc";
printf(" Element 1 pointed to by S is '%c'\n", *s);
printf(" Element 2 pointed to by S is '%c'\n", *s+1);
printf(" Element 3 pointed to by S is '%c'\n", *s+2);
printf(" Element 4 pointed to by S is '%c'\n", *s+3);
printf(" Element 5 pointed to by S is '%c'\n", s[3]);
printf(" Element 4 pointed to by S is '%c'\n", *s+4);
dare i seguenti risultati?
Element 1 pointed to by S is 'a'
Element 2 pointed to by S is 'b'
Element 3 pointed to by S is 'c'
Element 4 pointed to by S is 'd'
Element 5 pointed to by S is ' '
Element 4 pointed to by S is 'e'
Come ha fatto il compilatore a continuare la sequenza? E perché s[3]
restituisce un valore vuoto?
Precedenza. Vuoi '* (s + 1)' ecc. Ma '* (s + 4)' sarebbe un comportamento indefinito. –
's [3]' restituisce il carattere 4 ° (sono indicizzati da 0), che è una stringa che termina il carattere '\ 0'. –
's [3]' è '\ 0', quindi non mostra nessuno –