Error in Code? [C] -
i'm stuck in infinite loop, reason program doesn't terminate when \n
entered. point of code reverse input, i'm trying using pointer instead of integer keep track of position in array.
#define msg_length 80 int main(void) { char msg[msg_length], *p; printf("enter message: "); (p = msg; p < &msg[msg_length];) { *p++ = getchar(); if (*p == '\n') break; } printf("reversal is: "); (p--; p >= msg;) putchar(*p--); return 0; }
for (p = msg; p < &msg[msg_length];) { *p++ = getchar(); <-- getting character in *p , incrementing p if (*p == '\n') <-- checking value of next character, not 1 read break; }
possible correction:
for (p = msg; p < &msg[msg_length]; p+=sizeof(char)) { *p = getchar(); if (*p == '\n') break; }
Comments
Post a Comment