linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steve Graegert <graegerts@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: wcscat problem
Date: Thu, 19 Jan 2006 21:52:01 +0100	[thread overview]
Message-ID: <6a00c8d50601191252j326bdef1s9520ede0bfd27f70@mail.gmail.com> (raw)
In-Reply-To: <20060119163800.1b45d984.leslie.polzer@gmx.net>

On 1/19/06, Patrick Leslie Polzer <leslie.polzer@gmx.net> wrote:
>
> Hello list,
>
> why does wcscat not work like strcat does?
> It seems to be leaving the null byte in (C99 source):
>
>
> #define _GNU_SOURCE
> #include <wchar.h>
>
>
> int main (int ac, char** av)
> {
>         wchar_t* ws = malloc(8);
>         wcscpy(ws, (wchar_t*)"abc");
>         wcscat(ws, (wchar_t*)"def");
>         printf("%s/%s\n", ws, (void*)ws+4); /* "abc/def" [*] */
>
>
>         char* s = malloc(8);
>         strcpy(s, "abc");
>         strcat(s, "def");
>         printf("%s/%s\n", s, s+4); /* "abcdef/ef" */
>
>         return 0;
> }
>
> Why is this?  I also noticed the compiler (gcc4) takes "string steps"
> when performing pointer arithmetics with wchar_t pointers, that's why
> I needed to cast to void* in line [*].

Patrick,

as I've mailed you privately, I was not able to reproduce the results.
 Running your code causes weird output due to (a) wrong qualifiers in
printf (use %S or %ls instead of %s) and (b) wrong initialization of
wide-character strings.  Do not simply cast to wchar_t *, use mbstowcs
instead, which converts a character string to its wide-character
equivalent.  The following code shows, that wcscat works perfectly
well.

BTW: compiling with -Wall, as suggested by Markus, shows, that wcscat
does not like the semantic given in line [+].

--- START OF CODE ---

#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>

int main (int argc, char **argv) {
  wchar_t* ws = malloc(8);
  wchar_t* ws2 = malloc(4);

  /* works */
  mbstowcs(ws, "abc", 4);
  mbstowcs(ws2, "def", 4);
  wcscat(ws, ws2);
  printf("1: %S/%S\n", ws, ws + 4);
  printf("1: %ls/%ls\n", ws, ws + 4);

  /* does not work */
  wcscpy(ws, (wchar_t *)"abc");
  wcscat(ws, (wchar_t *)"def");
  printf("2: %S/%S\n", ws, (void *)ws + 4); /* [+] */

  /* works, of course */
  char* s = malloc(8);
  strcpy(s, "abc");
  strcat(s, "def");
  printf("3: %s/%s\n", s, s + 4);

  return 0;
}

--- END OF CODE ---

	\Steve

--

Steve Graegert <graegerts@gmail.com>
Software Consultant {C/C++ && Java && .NET}
Office: +49 9131 7123988
Mobile: +49 1520 9289212

  parent reply	other threads:[~2006-01-19 20:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-19 15:38 wcscat problem Patrick Leslie Polzer
2006-01-19 17:57 ` Markus Rechberger
2006-01-19 18:24   ` Patrick Leslie Polzer
2006-01-19 20:52 ` Steve Graegert [this message]
2006-01-22 10:30 ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6a00c8d50601191252j326bdef1s9520ede0bfd27f70@mail.gmail.com \
    --to=graegerts@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).