Re: iovecs don't print right



nobody+cplusplus <nobody@xxxxxxxxxxxxxxxx> wrote:
nobody+cplusplus wrote:

Stephane CHAZELAS wrote:
2008-07-05, 03:51(+09), nobody+cplusplus:
[...]
printf ("len %d, %d: %s \n", (int) iov[i].iov_len, i, (char *) \
iov[i].iov_base );
[...]
What wrong am i doing here? :/
[...]

You're using printf("%s") on something that is not a
zero-terminated string.

FYI, hence (void*)->(char*) for iov[i].iov_base
anything else?

FYI, casting doesn't append the required terminating '\0' character.
A char array isn't a string per se, only if one of the chars is '\0'
it's also a string.

OK somehow using printf("%.<precision>s",..) does the trick (as the max
characters is 16, <precision> = 16. But this is a ugly hack at best. Is
there better way of doing it?

Why should that be an "ugly hack"? If you don't have a string you
can't use printf() with "%s" otherwise. But you shouldn't use 16 but
the number of bytes you received (use "%*s" and put the number of
bytes you read into the arguments before the char array). Or append
the required '\0' char to make it a string.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.



Relevant Pages

  • Re: Delimited String to Array
    ... The casting is not required, and neither is the parsing of the string ... to make explicit what the compiler does implicitly: ... new char array, then loading it with the first character of a string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Fast and Safe C Strings: User friendly C macros to Declare and use C Strings.
    ... simply a char array - and passed it to a function that expects a string. ... One could also say that an unsheathed sword is not dangerous. ... no danger attached to a sword does not make it safer. ...
    (comp.lang.c)
  • Re: pcl: is LIST the same as ?
    ... >'casting away const' (although most compilers seem to be in denial;) ... >Then, to modify the string, you're supposed to be very explicit about ... that the first creates a pointer to an unnamed char array (the string ... many compilers will, in the first case, relocate the string constant ...
    (comp.lang.lisp)
  • Re: new String ( byte[] , encoding ) under the hood
    ... >characters to make an immutable char array, the place to achieve savings ... I wonder if there could be some way to hand off a char array to be ... inserted in a string. ...
    (comp.lang.java.programmer)
  • Re: could anybody help me with this simple exercise?
    ... > -A palindrome is a string that is spelled the same way ... > Write a function that uses pointersto check if the input char array is ... have an appropriate newsgroups line in your header for your mail to be seen, ...
    (comp.lang.c.moderated)