* Re: wcscat problem
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
2 siblings, 1 reply; 5+ messages in thread
From: Markus Rechberger @ 2006-01-19 17:57 UTC (permalink / raw)
To: Patrick Leslie Polzer; +Cc: linux-c-programming
Hi,
some headers are missing within your source too.
Try to compile your version with -Wall
it should look like the code attached then it will work.
Markus
#define _GNU_SOURCE
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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, 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;
}
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 [*].
>
> Leslie
>
>
> --
> gpg --keyserver pgp.mit.edu --recv-keys 0x52D70289
>
>
>
--
Markus Rechberger
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: wcscat problem
2006-01-19 15:38 wcscat problem Patrick Leslie Polzer
2006-01-19 17:57 ` Markus Rechberger
@ 2006-01-19 20:52 ` Steve Graegert
2006-01-22 10:30 ` Glynn Clements
2 siblings, 0 replies; 5+ messages in thread
From: Steve Graegert @ 2006-01-19 20:52 UTC (permalink / raw)
To: linux-c-programming
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
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: wcscat problem
2006-01-19 15:38 wcscat problem Patrick Leslie Polzer
2006-01-19 17:57 ` Markus Rechberger
2006-01-19 20:52 ` Steve Graegert
@ 2006-01-22 10:30 ` Glynn Clements
2 siblings, 0 replies; 5+ messages in thread
From: Glynn Clements @ 2006-01-22 10:30 UTC (permalink / raw)
To: Patrick Leslie Polzer; +Cc: linux-c-programming
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>
^ permalink raw reply [flat|nested] 5+ messages in thread