linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Glynn Clements <glynn@gclements.plus.com>
To: Patrick Leslie Polzer <leslie.polzer@gmx.net>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: wcscat problem
Date: Sun, 22 Jan 2006 10:30:22 +0000	[thread overview]
Message-ID: <17363.24254.327246.930171@cerise.gclements.plus.com> (raw)
In-Reply-To: <20060119163800.1b45d984.leslie.polzer@gmx.net>


Patrick Leslie Polzer wrote:

> why does wcscat not work like strcat does?

Because it works on wide characters, not bytes.

> It seems to be leaving the null byte in (C99 source):

Wide character strings don't have a "null byte" they have a null wide
character.

> #define _GNU_SOURCE
> #include <wchar.h>
> 
> 
> int main (int ac, char** av)
> {
>         wchar_t* ws = malloc(8);

If you want a wide string with space for 8 wide characters, you need
8*sizeof(wchar_t), or 32 bytes:

	wchar_t* ws = malloc(8*sizeof(wchar_t));

>         wcscpy(ws, (wchar_t*)"abc");
>         wcscat(ws, (wchar_t*)"def");

This is completely bogus. The expression (wchar_t*)"abc" evaluates to
a pointer to a wide string whose first wide character is either
0x00636261 or 0x61626300 (depending upon endianness), and whose
following wide characters are whatever was in memory after that point. 
The wide string will terminate at the first completely null word,
wherever that occurs.

What you should probably be doing is:

        wcscpy(ws, L"abc");
        wcscat(ws, L"def");

>         printf("%s/%s\n", ws, (void*)ws+4); /* "abc/def" [*] */

Huh? "%s" is for a byte string, not a wide string. You need:

	printf("%ls\n", ws);

> 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 [*].

I totally don't understand what you are saying here.

-- 
Glynn Clements <glynn@gclements.plus.com>

      parent reply	other threads:[~2006-01-22 10:30 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
2006-01-22 10:30 ` Glynn Clements [this message]

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=17363.24254.327246.930171@cerise.gclements.plus.com \
    --to=glynn@gclements.plus.com \
    --cc=leslie.polzer@gmx.net \
    --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).