【GCC警告&报错1】warning: initialization makes pointer from integer without a cast

本文根据笔者个人工作/学习经验整理而成,如有错误请留言。
文章为付费内容,已加入原创保护,禁止私自转载,违者必究。
文章发布于:《C语言编译报错&警告合集

例子:

#include <stdio.h>

int main() {
    int num = 10;
    int *ptr = num; // 这里将整型变量num赋值给指针变量ptr
    printf("%!d(MISSING)", *ptr);
    return 0;
}

报错:
在这里插入图片描述

原因:

想要给自定义的指针变量分配指定的内存空间,将整形直接赋给指针了;

分析:

将整型变量num赋值给指针变量ptr时,没有进行强制类型转换。虽然在编译的时候编译器可能会帮助强转不过也有可能不同编译器操作不同导致地址出错。

应该将语句修改为:

int *ptr = (int *)num;

Note:

指针的常用分配内存函数用法就有强制转换,在实际运用中可能就容易忽略这一要点;

int *p = (int *)malloc(sizeof(int)); //malloc函数返回值是void *

In file included from tpm_sign_example.c:1: /usr/include/tss2/tss2_esys.h:954:25: note: expected &lsquo;TPMT_TK_HASHCHECK **&rsquo; {aka &lsquo;struct &lt;anonymous&gt; **&rsquo;} but argument is of type &lsquo;TPM2B_DIGEST *&rsquo; {aka &lsquo;struct &lt;anonymous&gt; *&rsquo;} 954 | TPMT_TK_HASHCHECK **validation); | ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~ tpm_sign_example.c: In function &lsquo;verify_signature&rsquo;: tpm_sign_example.c:89:5: error: unknown type name &lsquo;RSA&rsquo; 89 | RSA *rsa = RSA_new(); | ^~~ tpm_sign_example.c:89:16: warning: implicit declaration of function &lsquo;RSA_new&rsquo; [-Wimplicit-function-declaration] 89 | RSA *rsa = RSA_new(); | ^~~~~~~ tpm_sign_example.c:89:16: warning: initialization of &lsquo;int *&rsquo; from &lsquo;int&rsquo; makes pointer from integer without a cast [-Wint-conversion] tpm_sign_example.c:90:8: error: request for member &lsquo;n&rsquo; in something not a structure or union 90 | rsa-&gt;n = BN_bin2bn(pubKey-&gt;publicArea.unique.rsa.buffer, pubKey-&gt;publicArea.unique.rsa.size, NULL); | ^~ tpm_sign_example.c:90:14: warning: implicit declaration of function &lsquo;BN_bin2bn&rsquo; [-Wimplicit-function-declaration] 90 | rsa-&gt;n = BN_bin2bn(pubKey-&gt;publicArea.unique.rsa.buffer, pubKey-&gt;publicArea.unique.rsa.size, NULL); | ^~~~~~~~~ tpm_sign_example.c:91:8: error: request for member &lsquo;e&rsquo; in something not a structure or union 91 | rsa-&gt;e = BN_new(); | ^~ tpm_sign_example.c:91:14: warning: implicit declaration of function &lsquo;BN_new&rsquo; [-Wimplicit-function-declaration] 91 | rsa-&gt;e = BN_new(); | ^~~~~~ tpm_sign_example.c:92:5: warning: implicit declaration of function &lsquo;BN_set_word&rsquo; [-Wimplicit-function-declaration] 92 | BN_set_word(rsa-&gt;e, pubKey-&gt;publicArea.parameters.rsaDetail.exponent); | ^~~~~~~~~~~ tpm_sign_example.c:92:20: error: request for member &lsquo;e&rsquo; in something not a structure or union 92 | BN_set_word(rsa-&gt;e, pubKey-&gt;publicArea.parameters.rsaDetail.exponent); | ^~ tpm_sign_example.c:95:15: warning: implicit declaration of function &lsquo;RSA_verify&rsquo; [-Wimplicit-function-declaration] 95 | int ret = RSA_verify( | ^~~~~~~~~~ tpm_sign_example.c:96:9: error: &lsquo;NID_sha256&rsquo; undeclared (first use in this function) 96 | NID_sha256, | ^~~~~~~~~~ tpm_sign_example.c:96:9: note: each undeclared identifier is reported only once for each function it appears in tpm_sign_example.c:106:5: warning: implicit declaration of function &lsquo;RSA_free&rsquo; [-Wimplicit-function-declaration] 106 | RSA_free(rsa); | ^~~~~~~~ tpm_sign_example.c: In function &lsquo;main&rsquo;: tpm_sign_example.c:124:77: warning: implicit declaration of function &lsquo;strlen&rsquo; [-Wimplicit-function-declaration] 124 | TPMT_SIGNATURE *signature = tpm_sign(ctx, keyHandle, (uint8_t*)message, strlen(message)); | ^~~~~~ tpm_sign_example.c:124:77: warning: incompatible implicit declaration of built-in function &lsquo;strlen&rsquo; tpm_sign_example.c:4:1: note: include &lsquo;&lt;string.h&gt;&rsquo; or provide a declaration of &lsquo;strlen&rsquo; 3 | #include &lt;stdlib.h&gt; +++ |+#include &lt;string.h&gt;
最新发布
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑猫学长呀

有帮助到你就来打个赏呗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值