I want to input a string or number from the keyboard and then display it. How do I do it?
This is what I have, gives me all sorts of errors, but nevermind my code, it’s just an example. I want to know what the right way to do it is.
Please provide with some code as an example and maybe some links I can further document from.
#include "stdafx.h"
#include <windows.h>
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
LPSTR test;
scanf("%s", &test);
printf("%s", &test);
//_getch();
return 0;
}
scanfreads into a preallocated buffer. It is unsafe with respect to to buffer overflows unless you specify a field width, e.g."%50s".Likewise,
printf( "%s", str )takes a pointer to the first character of the string forstr.You are passing a pointer to pointer, so I presume you expect the functions to do memory management for you.
scanfwill never callmalloc.