scanf - String using Character pointer in C -


when taking user input senences least of 1 space character pointer taking first blank space only, why? please provide alternative if necessary

char *ptr; ptr=(char*)malloc(sizeof(char)); scanf("%ns",ptr); 

*edited

the %s format specifier scanf reads sequence of non-whitespace characters. means stops after reading space, newline, tab, etc.

also, you're allocating space single char. that's enough store terminating null byte. need allocate @ least enough space number of characters expect read, plus 1 null terminator.

if want read full line of text, should use fgets function.

char line[100]; fgets(line, 100, stdin); // if newline stored, remove if (strrchr(line, '\n') != null) {     *(strrchr(line, '\n')) = '\0'; } 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -