* wcscat problem
@ 2006-01-19 15:38 Patrick Leslie Polzer
2006-01-19 17:57 ` Markus Rechberger
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Patrick Leslie Polzer @ 2006-01-19 15:38 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 794 bytes --]
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
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ 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 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 17:57 ` Markus Rechberger
@ 2006-01-19 18:24 ` Patrick Leslie Polzer
0 siblings, 0 replies; 5+ messages in thread
From: Patrick Leslie Polzer @ 2006-01-19 18:24 UTC (permalink / raw)
To: Markus Rechberger; +Cc: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 919 bytes --]
On Thu, 19 Jan 2006 18:57:57 +0100
Markus Rechberger <Markus Rechberger <mrechberger@gmail.com>> wrote:
| some headers are missing within your source too.
Obviously, but this was intended to be a minimal example, and
most compilers will get it without the headers.
| it should look like the code attached then it will work.
I'm not sure whether you understood my problem.
Perhaps I should have made myself clearer:
The code compiles and runs.
The problem ist that the wcscat operation seems to not overwrite
the terminating null byte of the first string "abc" and thus
printf will only print "abc" instead of "abcdef". To clarify this
I added 4 to the string -- and lo, the missing half is there, but a
null bytes separates both halves.
So my question is: why does strcat remove the \0, but the wide char
pendant does not?
Leslie
--
gpg --keyserver pgp.mit.edu --recv-keys 0x52D70289
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ 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
end of thread, other threads:[~2006-01-22 10:30 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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).