By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,964 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,964 IT Pros & Developers. It's quick & easy.

UnicodeQuery

P: n/a
Hi ,

I am trying some sample programs with unicode strings.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
double* my_len;
wchar_t* my_string = (wchar_t *) malloc(sizeof(wchar_t) *100);
wchar_t* my_string2 = (wchar_t *) malloc(sizeof(wchar_t) *100);

double* my_len1 = (double*)my_string2;

swprintf(my_string,100,L"ABCDEFGH");
my_len = (double*)(my_string);

*(my_len1) = *my_len;

wprintf(L"my string2 is %s\n",my_string2);

wprintf(L"my string is %s\n",my_string);

}

This program gives an output

my string2 is
ABCD?????????????????????????????????????????????? ????????????????
????????????????????????????????????
my string is ABCDEFGH
How can i make my_string2 to print "ABCDEFGH"?
Regards
Rajesh

Apr 23 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ra*******@gmail.com wrote:
Hi ,

I am trying some sample programs with unicode strings.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
double* my_len;
wchar_t* my_string = (wchar_t *) malloc(sizeof(wchar_t) *100);
wchar_t* my_string2 = (wchar_t *) malloc(sizeof(wchar_t) *100);

double* my_len1 = (double*)my_string2;

swprintf(my_string,100,L"ABCDEFGH");
my_len = (double*)(my_string);

*(my_len1) = *my_len;

wprintf(L"my string2 is %s\n",my_string2);

wprintf(L"my string is %s\n",my_string);

}

This program gives an output

my string2 is
ABCD?????????????????????????????????????????????? ????????????????
????????????????????????????????????
my string is ABCDEFGH
How can i make my_string2 to print "ABCDEFGH"?
Regards
Rajesh


I don't recognize 'windows.h' and 'wchar.h'. And you haven't shown
what's in "stdafx.h".

Perhaps you should post this in comp.windows.programmer or some such group.
Apr 23 '06 #2

P: n/a
"ra*******@gmail.com" wrote:

I am trying some sample programs with unicode strings.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])


So far you have used the non-existant (in standard C) headers
stdafx.h and windows.h, the reserved for the implementation
identifiers _tmain and _TCHAR. This newsgroup deals with the
standard C language, as defined by the various ISO standards, only.

Either confine yourself to standard C, or go to a newsgroup that
deals with your peculiar system.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Apr 23 '06 #3

P: n/a
Andrew Poelstra wrote:
ra*******@gmail.com wrote:
Hi ,

I am trying some sample programs with unicode strings.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
double* my_len;
wchar_t* my_string = (wchar_t *) malloc(sizeof(wchar_t) *100);
wchar_t* my_string2 = (wchar_t *) malloc(sizeof(wchar_t) *100);

double* my_len1 = (double*)my_string2;

swprintf(my_string,100,L"ABCDEFGH");
my_len = (double*)(my_string);

*(my_len1) = *my_len;

wprintf(L"my string2 is %s\n",my_string2);

wprintf(L"my string is %s\n",my_string);

}

This program gives an output

my string2 is
ABCD?????????????????????????????????????????????? ????????????????
????????????????????????????????????
my string is ABCDEFGH

How can i make my_string2 to print "ABCDEFGH"?


I don't recognize 'windows.h' and 'wchar.h'. And you haven't shown
what's in "stdafx.h".

Well, wchar.h is standard as of Amendment 1 to ANSI X3.159-1989. This,
of course, does not address the problem that my_string2 is pointing at
garbage. I admit that I have no clear idea why someone would write code
like this.
Apr 24 '06 #4

P: n/a
<ra*******@gmail.com> wrote:

code deleted, a few interesting statements left:
double* my_len;
wchar_t* my_string = (wchar_t *) malloc(sizeof(wchar_t) *100);
wchar_t* my_string2 = (wchar_t *) malloc(sizeof(wchar_t) *100);
double* my_len1 = (double*)my_string2;
swprintf(my_string,100,L"ABCDEFGH");
my_len = (double*)(my_string);
*(my_len1) = *my_len;
wprintf(L"my string2 is %s\n",my_string2);
wprintf(L"my string is %s\n",my_string); my string2 is
ABCD????????????????????????????????????????????? ?????????????????
????????????????????????????????????
my string is ABCDEFGH
How can i make my_string2 to print "ABCDEFGH"?


You initialize my_string, then copy sizeof(double)/sizeof(wchar_t)
chars from my_atring into my_string2 without properly terminating it
and leaving the rest uninitialized.

What are you trying to accomplish? If you want to copy strings, look
for a library function that does just that.
Apr 24 '06 #5

P: n/a
"ra*******@gmail.com" <ra*******@gmail.com> wrote:
int _tmain(int argc, _TCHAR* argv[])
{
double* my_len;
wchar_t* my_string = (wchar_t *) malloc(sizeof(wchar_t) *100);
wchar_t* my_string2 = (wchar_t *) malloc(sizeof(wchar_t) *100);

double* my_len1 = (double*)my_string2;

swprintf(my_string,100,L"ABCDEFGH");
my_len = (double*)(my_string);

*(my_len1) = *my_len;


Apart from all other problems, what makes you think using pointers to
double for copying wide character strings is a reasonable thing to do?

Richard
Apr 25 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.