C 指针 void * 缓冲区问题
很抱歉用 C 的东西打扰了大家。 write() 需要 void * buff。我需要通过提供所需的数据从 main() 调用此函数。
但是当我打印时它会抛出错误。朋友们帮帮我吧。 代码如下。
void write(int fd, void *buff,int no_of_pages)
{
// some code that writes buff into a file using system calls
}
现在我需要发送带有我需要的数据的buff。
#include "stdio.h"
#include "malloc.h"
int main()
{
int *x=(int*)malloc(1024);
*(x+2)=3192;
*(x+3)="sindhu";
printf("\n%d %s",*(x+2),*(x+3));
write(2,x,10); //(10=4bytes for int + 6 bytes for char "sindhu");
}
它警告我
warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
如何删除此警告
Sorry for messing you all with the C stuff.
The write() takes void * buff. And i need to call this function from main() by giving the required data.
But when i am printing it throws an error. Help me out friends.
Code is as follows.
void write(int fd, void *buff,int no_of_pages)
{
// some code that writes buff into a file using system calls
}
Now i need to send the buff with the data i need.
#include "stdio.h"
#include "malloc.h"
int main()
{
int *x=(int*)malloc(1024);
*(x+2)=3192;
*(x+3)="sindhu";
printf("\n%d %s",*(x+2),*(x+3));
write(2,x,10); //(10=4bytes for int + 6 bytes for char "sindhu");
}
It warns me
warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
How can i remove this warning
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通过强制转换为有效类型:
注意:您所做的事情看起来很邪恶。我会重新考虑这个设计!
By casting to a valid type:
Note: What you're doing looks evil. I'd reconsider this design!
很简单:按照错误提示进行操作。不要将整数传递给字符串格式化序列。
Quite simply: do as the error says. Do not pass an integer to a string formatting sequence.
您需要使用 char * 来引用字符串:
会更好。
You need to use a char * to reference a string:
would be better.
您的问题实际上有几个有趣的点。首先,我很惊讶 printf 生成的警告对编译器很有帮助,因为 printf 本质上不是类型安全的,因此不需要警告。其次,我实际上很惊讶你的编译器允许这样做:
我很确定这应该是一个错误,或者至少是一个警告,没有显式的强制转换。请注意,
"sindhu"
的类型为const char*
,而您的数组是int
类型的数组。因此,本质上您在这里所做的是将字符串的内存地址放入数组中的第四个整数中。现在重要的是,这做出了一个非常危险的假设:情况很可能不是这样;最值得注意的是,许多 64 位系统不具备此属性。
Bitmask 的答案将消除您收到的警告,但是正如他建议的那样,我强烈建议您更改程序的设计,这样就没有必要了。
最后一点,请记住,C 中的数组和指针在很大程度上是相同的,这并不完全正确,但足以说明
*(x+2)
相当于x[2]
阅读代码时更容易看到。There are actually a couple of interesting points in your question. Firstly, I am surprised that the
printf
is generating a warning that is rather helpful of your compiler as inherentlyprintf
is not type safe so no warning is necessary. Secondly, I am actually amazed that your compiler is allowing this:I am pretty sure that should be an error or at the very least a warning, without an explicit cast. Note that
"sindhu"
is of typeconst char*
and your array is an array of typeint
. So essentially what you are doing here is putting the memory address of the string into the 4th integer in your array. Now the important thing here is that this makes the very dangerous assumption that:This can be easily not be the case; most notably many 64-bit systems do not exhibit this property.
Bitmask's answer will eliminate the warning you are receiving, however as he suggests, I strongly advise that you change the design of your program such that this is not necessary.
Also as one final stylistic point remember that for the most part arrays and pointers in C are the same, this is not entirely true but sufficed to say that
*(x+2)
is equivalent tox[2]
which is rather easier on the eyes when reading the code.失去演员阵容;这是没有必要的,如果您忘记
#include
stdlib.h 或者在范围内没有对malloc
进行强制转换,它将抑制有用的诊断。其次,从可读性的角度来看,指定特定类型所需的元素数量通常比指定字节数更好。你可以这样做:“分配足够的内存来存储 N 个 int 值”。
好的。您将整数值
3192
分配给x[2]
。坏 juju;我很惊讶编译器没有在这一行上发出警告。您尝试将
char *
类型的值存储到int
(因为x
的类型是int *,
*(x + 3)
的类型是int
)。我不确定你想在这里完成什么;如果您尝试将指针的值存储在x[3]
处,请注意,指针值不一定可以表示为int
(例如,假设一个char *
为 4 个字节宽,但int
为 2 个字节宽)。在任何一种情况下,类型都不兼容,并且需要强制转换:如果您尝试将字符串的内容复制到从
x[3]
开始的缓冲区,这绝对是错误的做法; ”的适当宽松的定义),您需要使用strcpy
或memcpy
库函数:为了使这个“工作”(对于“工作
printf
语句,*(x + 3)
的类型是int
,而不是char *
,这不是与%s
转换说明符兼容。同样,为了使这个“工作”,你会做类似的事情你真的不想以这种非结构化的方式在同一个内存缓冲区中存储不同类型的数据;除非您真正知道自己在做什么,否则会导致严重胃灼热。
Lose the cast; it's not necessary, and it will suppress a useful diagnostic if you forget to
#include
stdlib.h or otherwise don't have a cast formalloc
in scope. Secondly, it's generally better from a readability standpoint to specify the number of elements you need of a specific type, rather than a number of bytes. You'd do that like so:which says "allocate enough memory to store N
int
values".Okay. You're assigning the integer value
3192
tox[2]
.Bad juju; I'm surprised the compiler didn't yak on this line. You're attempting to store a value of type
char *
to anint
(since the type ofx
isint *
, the type of*(x + 3)
is anint
). I'm not sure what you're trying to accomplish here; if you're trying to store the value of the pointer atx[3]
, note that pointer values may not necessarily be representable as anint
(for example, suppose anchar *
is 4 bytes wide but anint
is 2 bytes wide). In either case the types are not compatible, and a cast is required:If you're trying to copy the contents of the string to the buffer starting at
x[3]
, this is definitely the wrong way to go about it; to make this "work" (for suitably loose definitions of "work"), you would need to use either thestrcpy
ormemcpy
library functions:As for the problem in the
printf
statement, the type of*(x + 3)
isint
, notchar *
, which is not compatible with the%s
conversion specifier. Again, to make this "work", you'd do something likeYou really don't want to store different types of data in the same memory buffer in such an unstructured way; unless you really know what you're doing, it leads to massive heartburn.