c - Dereferencing pointers -
i came along question , when compiled , run in codeblockes got error , not run it.
here question.
what output of program ?
#include<stdio.h> #include<string.h> int main(){ int i, n; char *x="alice"; n = strlen(x); *x = x[n]; for(i=0; i<=n; i++){ printf("%s ", x); x++; } printf("\n", x); return 0; } a. alice
b. ecila
c. alice lice ice ce e
d. lice ice ce e
that code looks wrong. line
*x = x[n]; tries write string literal "alice". string literals cannot modified results in undefined behaviour. crash (as think you're seeing) valid , expected result here.
to answer question, output program undefined. unlikely give of results suggest.
if change declaration of x
char s[]="alice"; char* x = s; then
*x = x[n]; will replace first character of x null terminator, meaning printf loop output lice ice ce e (option d choices)
Comments
Post a Comment