问题很简单,s是一个字符串,我突然想到试图用printf(s)它来查看它是否可行,我在一个案例中得到警告而在另一个案例中没有警告.
char* s = "abcdefghij\n";
printf(s);
// Warning raised with gcc -std=c11:
// format not a string literal and no format arguments [-Wformat-security]
// On the other hand, if I use
char* s = "abc %d efg\n";
printf(s, 99);
// I get no warning whatsoever, why is that?
// Update, I've tested this:
char* s = "random %d string\n";
printf(s, 99, 50);
// Results: no warning, output "random 99 string".
Run Code Online (Sandbox Code Playgroud)
那么,什么是之间的基本差异printf(s),并printf("%s", s)和为什么会在短短的一个案例一个警告?
我想在 Visual Studio 中使用默认的 VSCode 键绑定,例如 Ctrl + D。
这个变量 num 保持为 0。我怀疑这是因为我传入了一个 NULL 指针,但我被限制在主(和参数)中。如何调整辅助函数的内容以正确更新 num?
int main(void) {
int * num_results = NULL;
int num = 4;
set(num_results, num);
printf(“%d\n”, *num_results);
return 0;
}
void set(int * results,num){
num_results = malloc(sizeof(int));
*num_results = num;
}
Run Code Online (Sandbox Code Playgroud)