编译器错误:无效从int *到无符号整数的转换* [-fpermissive] [英] Compiler Error: Invalid Conversion from int* to unsigned int* [-fpermissive]

查看:4563
本文介绍了编译器错误:无效从int *到无符号整数的转换* [-fpermissive]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天遇到了最奇怪的问题。我在网上一个例子,对我的缺乏的惊喜,它没有​​工作(他们几乎从不做)。我自己修复它,但我似乎困在这个错误:

 错误:无效从int *转换为无符号int * [-fpermissive] 

我提供一个 int * ,它想要一个 unsigned int * 。但是,我实际上并不知道为什么 int * 正在生成



以下是导致问题的代码段代码:

  unsigned char md_value [EVP_MAX_MD_SIZE] 
int md_length;

EVP_DigestFinal_ex(md_ctx,md_value,& md_length);

该函数调用的第三个参数& md_length ,是导致问题。查看该函数的文档调用(从OpenSSL,如果
很重要)
,它期望参数是 unsigned int * 类型,因为它想要一个地址(或至少我的工作示例是如何使用它)



<有趣的是,我认为& 运算符返回一个 unsigned int * c $ c> int * 没有意义,因为计算机在其内存中没有负地址。



我一直在跟进,如果你想看看: https:// www。 openssl.org/docs/crypto/EVP_DigestInit.html



下面是源代码,您应该自己尝试一下。



源代码:

/ p>

  // *描述* // 
//标题:示例
//作者:Boom Blockhead
//示例程序测试OpenSSL

// *库* //
#include< stdio.h>
#include< cstring>
#include< openssl / evp.h>

// *命名空间* //
using namespace std;

// * Main Method * //
//运行程序并解释命令行
int main(int argc,char * argv [])
{
//初始化消息
char msg1 [] =Test Message\\\
;
char msg2 [] =Hello World\\\
;

//验证参数计数
if(argc!= 2)
{
printf(用法:%s digestname\\\
,argv [0] );
exit(1);
}

//按名称确定消息摘要
OpenSSL_add_all_digests();
const EVP_MD * md = EVP_get_digestbyname(argv [1]);

//确保找到消息摘要类型
if(md == 0)
{
printf(Unknown Message Digest%s\\\
,argv [1]);
exit(1);
}

//创建消息摘要上下文
EVP_MD_CTX * md_ctx = EVP_MD_CTX_create();

//设置消息摘要类型
EVP_DigestInit_ex(md_ctx,md,NULL);

//添加要消化的消息
EVP_DigestUpdate(md_ctx,msg1,strlen(msg1));
EVP_DigestUpdate(md_ctx,msg2,strlen(msg2));

//消化消息
unsigned char md_value [EVP_MAX_MD_SIZE];
int md_length;

EVP_DigestFinal_ex(md_ctx,md_value,& md_length); //< --- ERROR

//销毁消息摘要上下文
EVP_MD_CTX_destroy(md_ctx);

//打印Digested文本
printf(Digest is:);

for(int i = 0; i printf(%02x,md_value [i]);

printf(\\\
);

//清理消息摘要
EVP_cleanup();

//退出程序
exit(0);
return 0;
}

此外,显式转换 *)似乎只是使事情变得更糟,因为我然后得到以下错误:

  cpp :( .text + 0x91):未定义引用`OpenSSl_add_all_digests` 
...
example.cpp :(。text + 0x1ec):未定义引用`EVP_cleanup`



基本上,它抱怨所有OpenSSL函数。



最后, (再次,这只是给你们你可能需要的一切)我不会发送任何有趣的参数编译器。我只是使用:

  gcc example.cpp -o example.out 

因为有一个错误, example.out 从来没有真正创建。

解决方案


作为返回一个int *没有意义,看到计算机在其内存中没有负地址。


您误解了类型名称的含义。



unsigned int * 是指向 unsigned int 的指针。 unsigned不指向指针值。



因此,解决方法是将int更改为unsigned int。


I'm having the strangest issue today. I was working with an example online, and to my lack of surprise, it didn't work (they pretty much never do). I went about fixing it myself, but I seem to be stuck on this error:

Error: Invalid Conversion from int* to unsigned int* [-fpermissive]

I understand this. I'm providing an int*, it wants an unsigned int*. However, I don't actually know why the int* is being generated.

Here's the snippet code throwing the problem:

unsigned char md_value[EVP_MAX_MD_SIZE];
int md_length;

EVP_DigestFinal_ex(md_ctx, md_value, &md_length);

The third argument of that function call, &md_length, is causing the problem. Looking at the documentation for that function call (From OpenSSL, if it matters), it expects the argument to be of unsigned int* type, which makes since, because it wants an address (or at least that how the example I'm working with is using it).

Funny thing is, I thought that the & operator returned an unsigned int*, as returning an int* doesn't make sense, seeing as computers don't have negative addresses in their memory.

Here's the example I'm following along with, if you wish to take a look: https://www.openssl.org/docs/crypto/EVP_DigestInit.html

Below is the Source Code, should you want to try it out yourself. I doubt you'll actually need to read it to solve this problem, but having it here couldn't hurt.

Source Code:

//* Description *//
// Title: Example
// Author: Boom Blockhead
// Example Program to Test OpenSSL

//* Libraries *//
#include <stdio.h>
#include <cstring>
#include <openssl/evp.h>

//* Namespaces *//
using namespace std;

//* Main Method *//
// Runs the Program and Interprets the Command Line
int main(int argc, char* argv[])
{
    // Initialize the Messages
    char msg1[] = "Test Message\n";
    char msg2[] = "Hello World\n";

    // Validate Argument Count
    if(argc != 2)
    {
        printf("Usage: %s digestname\n", argv[0]);
        exit(1);
    }

    // Determine Message Digest by Name
    OpenSSL_add_all_digests();
    const EVP_MD* md = EVP_get_digestbyname(argv[1]);

    // Make sure a Message Digest Type was Found
    if(md == 0)
    {
        printf("Unknown Message Digest %s\n", argv[1]);
        exit(1);
    }

    // Create the Message Digest Context
    EVP_MD_CTX* md_ctx = EVP_MD_CTX_create();

    // Setup the Message Digest Type
    EVP_DigestInit_ex(md_ctx, md, NULL);

    // Add the Messages to be Digested
    EVP_DigestUpdate(md_ctx, msg1, strlen(msg1));
    EVP_DigestUpdate(md_ctx, msg2, strlen(msg2));

    // Digest the Message
    unsigned char md_value[EVP_MAX_MD_SIZE];
    int md_length;

    EVP_DigestFinal_ex(md_ctx, md_value, &md_length); // <--- ERROR

    // Destroy the Message Digest Context
    EVP_MD_CTX_destroy(md_ctx);

    // Print the Digested Text
    printf("Digest is: ");

    for(int i = 0; i < md_length; i++)
        printf("%02x", md_value[i]);

    printf("\n");

    // Clean up the Message Digest
    EVP_cleanup();

    // Exit the Program
    exit(0);
    return 0;
}

Also, putting the explicit cast of (unsigned int*) seems to just make things worse, as I then get the follow error:

example.cpp:(.text+0x91): undefined reference to `OpenSSl_add_all_digests`
...
example.cpp:(.text+0x1ec): undefined reference to `EVP_cleanup`

Basically, it complains about all of the OpenSSL functions.

Lastly, (again, this is just to give you guys everything you could possibly need) I'm not sending in any funny arguments to the compiler. I'm just using:

gcc example.cpp -o example.out

Since there's an error, the example.out never actually gets created.

解决方案

as returning an int* doesn't make sense, seeing as computers don't have negative addresses in their memory.

You are misunderstanding what the type name means.

unsigned int* is a pointer to an unsigned int. unsigned does not refer to the pointer value.

So the solution is to change your int to unsigned int.

这篇关于编译器错误:无效从int *到无符号整数的转换* [-fpermissive]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆