gpt4 book ai didi

c - 如何正确初始化指针

转载 作者:太空宇宙 更新时间:2023-11-04 02:16:39 25 4
gpt4 key购买 nike

我有以下调用函数 uint32_pack 的代码。该程序在 Dev-C++ 中编译时没有错误,但当“标签”为偶数时不会产生正确的结果。在 Visual Studio 中,程序可以编译但会产生错误,我猜这些错误就是为什么当“标记”为偶数时我没有得到正确的输出。我仍在努力理解指针,但我不确定在声明它们时哪里出错了。感谢您的帮助。

这是错误来源的代码:

 1  int uint32_pack (uint8_t *fieldnumber, uint32_t value, uint8_t *out);
2 int main(){
3 uint32_t initvalue = 2;
4 int return_rv;
5 uint8_t *tag = (uint8_t *) malloc(sizeof(uint8_t));
6 *tag = 38;
7 uint8_t *tempout= (uint8_t *) malloc(30);
8 return_rv = uint32_pack (tag, initvalue, tempout);
9 free(tempout);
10 free(tag);
11 }

VS 报错如下:

error C2143: syntax error : missing ';' before 'type' (on line 7)
error C2065: 'tempout' : undeclared identifier (on line 8)
warning C4047: 'function' : 'unsigned char *' differs in levels of indirection from 'int' (on line 8)
warning C4024: 'uint32_pack' : different types for formal and actual parameter 3 (on line 8)
error C2065: 'tempout' : undeclared identifier (on line 9)
warning C4022: 'free' : pointer mismatch for actual parameter 1 (on line 9)

最佳答案

最后三个错误是第二个的结果,第二个是第一个的结果。只有第一个和第三个无法解释。

第一个错误是因为您使用的是 C89 而不是 C++ 或 C99;你不能在 C89 的代码之后声明变量。

将第 6 行和第 7 行的顺序颠倒一下,您应该就没问题了。 (我认为第三个错误也是第一个错误的结果,但这并不确定。)

关于c - 如何正确初始化指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6779004/

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