In C, what is the best way to call and save a string without wasted space if we cannot query the length of the string. For example, usually I would do something like the following ...
char fname[30];
char lname[30];
printf("Type first name:\n");
scanf("%s", fname);
printf("Type last name:\n");
scanf("%s", lname);
printf("Your name is: %s %s\n", fname, lname);
However, I am annoyed by the fact that I should use more space than necessary, so I do not want to use it char fname[30], but instead dynamically allocate the size of the string. Any thoughts?
source
share