gpt4 book ai didi

c - 警告——赋值使指针来自整数而不进行强制转换

转载 作者:行者123 更新时间:2023-11-30 17:30:04 25 4
gpt4 key购买 nike

我知道这个问题经常被问到,但我读了十几个问题,但仍然没有找到解决方案。编译时我收到警告:

warning: assignment makes pointer from integer without a cast

此警告引用的代码是:

unsigned char *WebPrintReturnLine(BIO *bio) {

long int i;
unsigned int turn = 0;
unsigned char *ptr = 0;
int size;
unsigned char buffer[4096];

i = (long int) WebRead(bio, buffer, sizeof(buffer));
if( (ptr = (unsigned char *) malloc(strlen(buffer))) == 0 ) {
printf("Error: Couldn't allocate memory in WebPrintReturnLine\n");
return ptr;
}

//strlen does not care about '\0' but as array begin at 0 it nulifies
size = strlen(buffer);
//strcpy the buffer into the allocated memory
strcpy(ptr, buffer);
printf("%d\n", isend(buffer, sizeof(buffer)));

while( (i > 0) && (!isend(buffer, sizeof(buffer))) ) {
i = (long int) WebRead(bio, buffer, sizeof(buffer));
size += strlen(buffer);
if( (ptr = (unsigned char*) realloc(ptr, size)) == 0 ) {
printf("Error: Couldn't reallocate memory in WebPrintReturnLine\n");
ptr = 0;
return ptr;
}//End if
//Strcat the original string and the buffer together
strcat(ptr, buffer);
}//End while

//Now finally print the line
printf("%s", ptr);

return ptr;
}

这被调用了:

unsigned char *ptr;    
if( (ptr = WebPrintReturnLine(bio)) == 0 )
return -1;

我首先想在这个问题中缩短该代码,但我担心我可能会监督导致此警告的某些内容。

最佳答案

因此警告位于您问题中的第二个代码片段上。 WebPrintReturnLine 的声明在调用时不可见。根据 C90 规则,编译器假定它返回 int (在本例中是错误的)。根据C99规则,该调用是非法的。您需要为声明 WebPrintReturnLine 的 header 添加 #include。 ——基思·汤普森

关于c - 警告——赋值使指针来自整数而不进行强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25311166/

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