[Linux]wchar_t[]/L"string" --> wchar_t* undesired result

Hello everybody.
First of all I put this post here cuz it might be trivial compared to what's in the UNIX-subforum.

Now to the actual issue. I have a problem using widechar strings in Linux (Debian 5, G++ [Debian 4.3.2-1.1], Gnu GDB [6.8-debian]). Here's an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdlib.h>
#include <iostream>
#include <wchar.h>

int main(int argc, char** argv) {
    //Set output to normal output, doesn't work properly without
    FILE* openres=freopen(NULL,"w",stdout);
    int res = fwide(stdout, -1);


    printf("-------Char-------------\n");

    //This should deprecated according to this doc http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1996/N0896.asc
    char* deprecated = "This is deprecated";
    printf("Using deprecated:\t%s\n", deprecated);

    char testArray[] = "Standard char string/array.";
    printf("Using char-array:\t%s\n", testArray);

    char* test = &testArray[0];
    printf("Using first element:\t%s\n", test);

    test=testArray;
    printf("Using array pointer:\t%s\n", test);


    //Set stdout to wide-char mode
    openres=freopen(NULL,"w",stdout);
    res = fwide(stdout, 1);
    if (res < 1) {
        res=fwide(stdout,0);
        printf("Couldn't switch to wide-char!");
        return (EXIT_FAILURE);
    }

    wprintf(L"-------Wide-char--------\n");
    wchar_t* wdeprecated = L"This is deprecated";
    wprintf(L"WCHAR Using deprecated:\t%s\n", wdeprecated);

    wchar_t wtestArray[] = L"Widestring string/array.";
    wprintf(L"WCHAR Using char-array:\t%s\n",wtestArray);

    wchar_t* wtest = &wtestArray[0];
    wprintf(L"WCHAR first element:\t%s\n",wtest);

    wtest=wtestArray;
    wprintf(L"WCHAR array pointer:\t%s\n",wtest);

    return (EXIT_SUCCESS);
}

and here's the output:
-------Char-------------
Using deprecated:	This is deprecated
Using char-array:	Standard char string/array.
Using first element:	Standard char string/array.
Using array pointer:	Standard char string/array.
-------Wide-char--------
WCHAR Using deprecated:	T
WCHAR Using char-array:	W
WCHAR first element:	W
WCHAR array pointer:	W

The wprintf-output isn't an individual case the same happened with an .so library I've been trying to use.
The same code works perfectly in windows without even needing to use freopen().

Question:
How do I correctly reference,point-to and use wide-char strings in Linux ? If I can read this up somewhere please point me in the right direction :-)

Thank you

Looks like there's something wrong with wprintf(). That code should work, AFAICT.

The proper way to declare pointers to string literals (i.e. to string that don't need to be modified) is:
1
2
const char *p="string literal";
const wchar_t *wp=L"wide string literal";

That's what the warning is complaining about.
Hello.

I added "const" to the pointers and tried it again on Ubuntu (I know it's part of the Debian family) with the same result. I mean same output. The warning is now gone (ty helios)

Here are the specs and compiler info:
Linux ubuntu-laptop 2.6.31-19-generic #56-Ubuntu SMP Thu Jan 28 02:39:34 UTC 2010 x86_64 GNU/Linux
gcc (Ubuntu 4.4.1-4ubuntu9) 4.4.1
g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1
GNU gdb (GDB) 7.0-ubuntu
GNU assembler (GNU Binutils for Ubuntu) 2.20

What am I doing wrong ?
Last edited on

Maybe your console dosnt support the wchar.
I had the same problem a Ive converted the wchar to char and everything working fine
The fix is to change the wprintf format specification for wide character strings from "%s" to "%ls". This generates the expected output (at least on my Gentoo Unix, using GCC):

-------Char-------------
Using deprecated: This is deprecated
Using char-array: Standard char string/array.
Using first element: Standard char string/array.
Using array pointer: Standard char string/array.
-------Wide-char--------
WCHAR Using deprecated: This is deprecated
WCHAR Using char-array: Widestring string/array.
WCHAR first element: Widestring string/array.
WCHAR array pointer: Widestring string/array.
SimonH you're right. Wow for a while I thought there was problem with my gcc installation.
Ty for your response.
Also, in this case, you do not have to use wprintf to print wide character strings.

This will work:
1
2
const wchar_t * ws = L"Wide string";
printf( "Regular string here and wide character string in here: [%ls]", ws );


Use wprintf when the format string is also wide.
Topic archived. No new replies allowed.