麻烦请问下,下面C语言代码的不合理之处是在哪里,谢谢了。
typedef struct{
unsigned int len;
char *data;
}Buffer_T;
void demo(int x){
Buffer_T * buffer = NULL;
buffer = (Buffer_T *)malloc(sizeof(Buffer_T));
buffer->data = (char *)malloc(50);
if (NULL == (buffer->data)){
free(buffer);
return;
}
buffer->len = strlen("hello\n");
strcpy(buffer->data, "hello\n");
if (buffer->len > x)
return;
else{
printf("%d\n", buffer->len);
printf("%s", buffer->data);
}
free(buffer->data);
free(buffer);
return;
}
这个程序并没有什么实质性的功能,传入的参数x如果大于等于6,那么输出hello和6,否则不输出。
绕了这么一大圈。
您的程序确实是以x的值来决定是否输出
我用下面的主函数调用了您的函数
结果截图了。
int main()
{
int x=0;
for(int i=0;i<10;i++)
{ x=i;
printf("\n数据x=%d\n",x);
demo(x);
}
}