C pointers void * buffer problem
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)
{
// 开发者_StackOverflow社区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
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!
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
You need to use a char * to reference a string:
char * cp = "sindhu";
printf("\n%d %s", *(x+2), cp);
would be better.
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.
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.
精彩评论