如果必须返回i,以下代码(func1())是否正确?我记得在某处读到返回对局部变量的引用时存在问题.它与func2()有什么不同?
int& func1()
{
int i;
i = 1;
return i;
}
int* func2()
{
int* p;
p = new int;
*p = 1;
return p;
}
Run Code Online (Sandbox Code Playgroud) 我想在Linux C中找到md5sum文件,是否有任何API我可以发送文件名来获取该文件的md5sum.
我在x86_64 Linux上使用GCC 4.8和glibc 2.19.
在为不同的问题使用不同的输入法时,我比较fscanf和sscanf.具体来说,我要fscanf直接使用标准输入:
char s[128]; int n;
while (fscanf(stdin, "%127s %d", s, &n) == 2) { }
Run Code Online (Sandbox Code Playgroud)
或者我首先将整个输入读入缓冲区然后遍历缓冲区sscanf.(将所有内容读入缓冲区需要花费很少的时间.)
char s[128]; int n;
char const * p = my_data;
for (int b; sscanf(p, "%127s %d%n", s, &n, &b) == 2; p += b) { }
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,该fscanf版本是大大加快.例如,处理数以万计的行fscanf需要这么长时间:
10000 0.003927487 seconds time elapsed
20000 0.006860206 seconds time elapsed
30000 0.007933329 seconds time elapsed
40000 0.012881912 …Run Code Online (Sandbox Code Playgroud) 我需要将包含十六进制值作为字符的字符串转换为字节数组。尽管这里已经作为第一个答案已经回答了,但出现以下错误:
warning: ISO C90 does not support the ‘hh’ gnu_scanf length modifier [-Wformat]
Run Code Online (Sandbox Code Playgroud)
由于我不喜欢警告,因此遗漏hh只会产生另一个警告
warning: format ‘%x’ expects argument of type ‘unsigned int *’, but argument 3 has type ‘unsigned char *’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何正确执行此操作?为了完成,我再次在此处发布示例代码:
#include <stdio.h>
int main(int argc, char **argv)
{
const char hexstring[] = "deadbeef10203040b00b1e50", *pos = hexstring;
unsigned char val[12];
size_t count = 0;
/* WARNING: no sanitization or error-checking whatsoever */
for(count = 0; count < sizeof(val)/sizeof(val[0]); count++) {
sscanf(pos, …Run Code Online (Sandbox Code Playgroud)