【发布时间】:2013-08-30 18:59:28
【问题描述】:
我讨厌发布这个,因为其中有很多,但似乎没有一个能解决我所看到的问题。正常问题(未声明的函数、无意的强制转换、对基本指针的误解)似乎不适用于这里。这是我的代码的精简版:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
extern void* malloc ( size_t size );
typedef struct {
size_t size;
uint8_t* buffer, curr, next;
} buffer_t;
void init( buffer_t* b, int size ) {
b->size = (size_t) size;
b->buffer = (uint8_t*) malloc( sizeof(uint8_t) * b->size + 1 );
b->curr = (uint8_t*) b->buffer; // warning: assignment makes integer from pointer without a cast [enabled by default]
b->next = (uint8_t*) b->buffer; // warning: assignment makes integer from pointer without a cast [enabled by default]
}
int main ( int argc, char* argv[] ) {
buffer_t buf;
init( &buf, 16 );
return 0;
}
如果没有演员表,这会失败,但将它们放入会使它更加明显。
我正在使用 gcc 4.7.2 在 MinGW/MSYS 下的 WinXP(是的,是的,是的)上编译。使用以下命令:
gcc -std=c99 -Wall -o testing test.c
有什么帮助吗?
【问题讨论】:
-
malloc在<stdlib.h>中声明。你为什么还要自己声明呢? (这与您的问题无关。) -
另外,you shouldn't cast the return value from
malloc,你绝对不需要将b->buffer转换为uint8_t*,因为它已经是那种类型了。
标签: c pointers gcc-warning