Saturday, 15 March 2014

c - Is that only an error with calling gets? -



c - Is that only an error with calling gets? -

we got task, , know if overlooked in our solution:

there problem next code fragment. compile, there of import semantic problem, cause code behave differently expected. identify problem , solution problem.

int main(void) { char * s = malloc(82 * sizeof(char)); if(s==null) { perror("failed allocate memory"); homecoming exit_failure; } fgets(s, sizeof(s), stdin); if(s[strlen(s)-1] != '\n') { fprintf(stderr, "error: entered much input"); free(s); homecoming exit_failure; } free(s); homecoming exit_success; }

we think error fgets function, read in including null terminating character. believe there potential write character has not been malloc'd. believe line fget(s,sizeof(s)-1, stdin); fix. right? or there more / other errors?

sizeof( s ) <=> sizeof( char * ), , that's not want.

c pointers semantics

No comments:

Post a Comment