gpt4 book ai didi

c - 在 gcc 中编译 SHA1( ) 函数失败。 c语言

转载 作者:行者123 更新时间:2023-11-30 20:06:16 25 4
gpt4 key购买 nike

这是我的代码:

#include "openssl/sha.h"
#include "stdio.h"
#include "string.h"

unsigned char *SHA1(const unsigned char *d, unsigned long n, unsigned char *md);

int main(void)
{
char data[] = "Hello, world!";

size_t length = sizeof(data);

unsigned char hash[SHA_DIGEST_LENGTH];

SHA1(data, length, hash);

return 0;
}

当我使用不带 -Wall 选项的 gcc 编译它时,程序运行良好并打印出一些字符串。当我使用-Wall时,我遇到以下情况:

test.c: In function ‘main’:

test.c:11:3: warning: pointer targets in passing argument 1 of ‘SHA1’ differ in signedness [-Wpointer-sign]

SHA1(data, length, hash);
^
In file included from test.c:1:0:

/usr/include/openssl/sha.h:126:16: note: expected ‘const unsigned char *’ but argument is of type ‘char *’

unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md);

Can anyone help me, Thanks in advance.

最佳答案

您的程序不会编译失败,而是会产生警告。

在这种情况下,SHA1 希望您传递一个 unsigned char*,但您传递的是一个 char *,您可以安全地将 char* 转换为 unsigned char*,所以就这样做

SHA1((unsigned char*)data, length, hash);

关于c - 在 gcc 中编译 SHA1( ) 函数失败。 c语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26019876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com