C 指针 void * 缓冲区问题

发布于 2024-12-04 11:32:53 字数 665 浏览 2 评论 0原文

很抱歉用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

別甾虛僞 2024-12-11 11:32:53

通过强制转换为有效类型:

printf("\n%d %s",*(x+2),(char*)(x+3)); 

注意:您所做的事情看起来很邪恶。我会重新考虑这个设计!

By casting to a valid type:

printf("\n%d %s",*(x+2),(char*)(x+3)); 

Note: What you're doing looks evil. I'd reconsider this design!

同展鸳鸯锦 2024-12-11 11:32:53

很简单:按照错误提示进行操作。不要将整数传递给字符串格式化序列。

printf("\n%d %d", *(x+2), *(x+3));
              ^--- note the change

Quite simply: do as the error says. Do not pass an integer to a string formatting sequence.

printf("\n%d %d", *(x+2), *(x+3));
              ^--- note the change
等风来 2024-12-11 11:32:53

您需要使用 char * 来引用字符串:

char * cp = "sindhu";
printf("\n%d %s", *(x+2), cp);

会更好。

You need to use a char * to reference a string:

char * cp = "sindhu";
printf("\n%d %s", *(x+2), cp);

would be better.

你穿错了嫁妆 2024-12-11 11:32:53

您的问题实际上有几个有趣的点。首先,我很惊讶 printf 生成的警告对编译器很有帮助,因为 printf 本质上不是类型安全的,因此不需要警告。其次,我实际上很惊讶你的编译器允许这样做:

*(x+3) = "sindhu";

我很确定这应该是一个错误,或者至少是一个警告,没有显式的强制转换。请注意,"sindhu" 的类型为 const char*,而您的数组是 int 类型的数组。因此,本质上您在这里所做的是将字符串的内存地址放入数组中的第四个整数中。现在重要的是,这做出了一个非常危险的假设:

sizeof(int) == sizeof(char*)

情况很可能不是这样;最值得注意的是,许多 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 inherently printf is not type safe so no warning is necessary. Secondly, I am actually amazed that your compiler is allowing this:

*(x+3) = "sindhu";

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 type const char* and your array is an array of type int. 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:

sizeof(int) == sizeof(char*)

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 to x[2] which is rather easier on the eyes when reading the code.

满天都是小星星 2024-12-11 11:32:53
int *x=(int*)malloc(1024);

失去演员阵容;这是没有必要的,如果您忘记 #include stdlib.h 或者在范围内没有对 malloc 进行强制转换,它将抑制有用的诊断。其次,从可读性的角度来看,指定特定类型所需的元素数量通常比指定字节数更好。你可以这样做:

int *x = malloc(N * sizeof *x);

“分配足够的内存来存储 N 个 int 值”。

*(x+2)=3192;

好的。您将整数值 3192 分配给 x[2]

*(x+3)="sindhu";

juju;我很惊讶编译器没有在这一行上发出警告。您尝试将 char * 类型的值存储到 int(因为 x 的类型是 int *,*(x + 3) 的类型是 int)。我不确定你想在这里完成什么;如果您尝试将指针的值存储在 x[3] 处,请注意,指针值不一定可以表示为 int(例如,假设一个char * 为 4 个字节宽,但 int 为 2 个字节宽)。在任何一种情况下,类型都不兼容,并且需要强制转换:

*(x + 3) = (int) "sindhu"; // equivalent to writing x[3] = (int) "sindhu"

如果您尝试将字符串的内容复制到从x[3]开始的缓冲区,这绝对是错误的做法; ”的适当宽松的定义),您需要使用 strcpymemcpy 库函数:

strcpy((char *) (x + 3), "sindhu"); // note the cast, and the fact that
                                    // x + 3 is *not* dereferenced.  

为了使这个“工作”(对于“工作 printf语句,*(x + 3)的类型是int,而不是char *,这不是与 %s 转换说明符兼容。同样,为了使这个“工作”,你会做类似的事情

printf("%d %s\n", *(x + 2), (char *) (x + 3));

你真的不想以这种非结构化的方式在同一个内存缓冲区中存储不同类型的数据;除非您真正知道自己在做什么,否则会导致严重胃灼热。

int *x=(int*)malloc(1024);

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 for malloc 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:

int *x = malloc(N * sizeof *x);

which says "allocate enough memory to store N int values".

*(x+2)=3192;

Okay. You're assigning the integer value 3192 to x[2].

*(x+3)="sindhu";

Bad juju; I'm surprised the compiler didn't yak on this line. You're attempting to store a value of type char * to an int (since the type of x is int *, the type of *(x + 3) is an int). I'm not sure what you're trying to accomplish here; if you're trying to store the value of the pointer at x[3], note that pointer values may not necessarily be representable as an int (for example, suppose an char * is 4 bytes wide but an int is 2 bytes wide). In either case the types are not compatible, and a cast is required:

*(x + 3) = (int) "sindhu"; // equivalent to writing x[3] = (int) "sindhu"

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 the strcpy or memcpy library functions:

strcpy((char *) (x + 3), "sindhu"); // note the cast, and the fact that
                                    // x + 3 is *not* dereferenced.  

As for the problem in the printf statement, the type of *(x + 3) is int, not char *, which is not compatible with the %s conversion specifier. Again, to make this "work", you'd do something like

printf("%d %s\n", *(x + 2), (char *) (x + 3));

You 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文