【发布时间】:2011-07-08 19:38:16
【问题描述】:
我有以下代码,我想返回 'out' 的值并将其打印到文件中。如何返回“out”的值而不是位置。另外,如何从主函数调用函数 uint32_pack?感谢您的帮助。
#if HAVE_PROTOBUF_C_CONFIG_H
#include "protobuf-c-config.h"
#endif
#include <stdio.h> /* for occasional printf()s */
#include <stdlib.h> /* for abort(), malloc() etc */
#include <string.h> /* for strlen(), memcpy(), memmove() */
#if HAVE_ALLOCA_H
#include <alloca.h>
#elif HAVE_MALLOC_H
#include <malloc.h>
#endif
#include "protobuf-c.h"
int main(void){
uint32_t hexvalue = 0x20;
int gnuvalue;
uint8_t fake_out;
FILE *fp;
fp = fopen("binarydata.txt","w");
gnuvalue = uint32_pack (hexvalue, fake_out);
fprintf(fp,"%x",gnuvalue);
fclose(fp);
}
/* === pack() === */
/* Pack an unsigned 32-bit integer in base-128 encoding, and return the number of bytes needed:
this will be 5 or less. */
static inline size_t
uint32_pack (uint32_t value, uint8_t *out)
{
unsigned rv = 0;
if (value >= 0x80)
{
out[rv++] = value | 0x80;
value >>= 7;
if (value >= 0x80)
{
out[rv++] = value | 0x80;
value >>= 7;
if (value >= 0x80)
{
out[rv++] = value | 0x80;
value >>= 7;
if (value >= 0x80)
{
out[rv++] = value | 0x80;
value >>= 7;
}
}
}
/* assert: value<128 */
out[rv++] = value;
return &out;
}
}
【问题讨论】:
-
我现在收到以下错误,第 56 行的“'uint32_pack' 的类型冲突”和 ling43 的“'uint32_pack' 的先前隐式声明在这里”
-
你必须在使用前声明函数。看来您应该复习 C 的基础知识。
-
确实。我认为@Sam 应该继续发布他的评论作为答案,因为他的评论实际上也先于我们所有的答案。
-
gnuvalue 声明为 int,
return *out将返回 uint8_t;虽然这没有问题,但它可能是你没有按照你的意思做的线索