char *s;
printf("enter a word \n");
scanf("%s",s);
scanf with %s does not allocate memory, instead it reads into the buffer you provide it (e.g. s = malloc(100);. How big does that buffer need to be to avoid a buffer overflow? Unfortunately it can be however long until a whitespace character, so is inherently unsafe.
You can specify a max up front (not including null terminator!), but to handle variable size you then need to dynamically build the format string which gets complicated. As does what to do if the user enters a string too long.
char s[100];
scanf("%99s", s);
Since you tagged C++, you can use std::string and IO streams (e.g. std::cin for console input) which will handle all the memory allocation for you.
std::string s;
std::cout << "enter a word" << std::endl;
std::cin >> s;
In the case of C, you might use fgets(str, num, stream), this lets you specify the max length and avoid overflow in a simple manner.
char s[128];
fgetsf(s, 100, stdin);
Or with the POSIX 2008 scanf it can allocate the memory, but I don't believe this is universally supported, e.g. by Microsoft Visual Studio.
char *s = 0;
scanf("%ms", &s); // pass a pointer to a pointer!
free(s); // needs to be freed later!
chars in the memory locations pointed to bys. Where doesspoint?s. With that said if this isc++why are usingchar *, scanf(), andprintf()it's much simpler and less error prone if you use modernc++instead ofccode compiled in ac++compiler.printfis defined bycstdio