当我尝试编译使用gets()
GCC函数的C代码时,
我明白了
警告:
(.text + 0x34):警告:`gets'函数很危险,不应该使用.
我记得这与堆栈保护和安全性有关,但我不确定为什么?
有人可以帮我删除这个警告并解释为什么会有这样的使用警告gets()
?
如果gets()
是如此危险,为什么我们不能删除它?
我必须打印一个浮点值,但是在编译时不知道精度。所以这个参数必须作为参数传递。这是如何实现的。在 windows 中,使用 CString,格式函数有助于实现这一点。如何在没有 CString 的情况下实现这一点。代码:
int main()
{
/* This below code segment works fine.*/
char str[80];
sprintf(str, "Value of Pi = %.3f", 3.147758);
std::cout << str << std::endl; //Prints "Value of Pi = 3.148"
/* When the precision may vary, henc It needs to be printed required on that.*/
char str2[80];
std::string temp = "%.3f"; //This is required as the precision may change.
// i.e I may have 4 instead 3 decimal points.
sprintf(str2, "Value = %s", temp, …
Run Code Online (Sandbox Code Playgroud) 我在C代码中有以下定义:
#define set_str(r,s) sprintf(r, "%-.*s", (int)sizeof(r)-1,s)
Run Code Online (Sandbox Code Playgroud)
我尝试在我的代码中调用函数set_str来理解符号的含义,但它没有给出任何特殊的格式或任何东西,变量只是按原样复制.谁能告诉我这是什么意思?