471,337 Members | 921 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

convertion problem? from char* to unsigned char*?

Hello,
1) First problem.
In my project i need to convert from char* to BYTE*( BYTE is unsigned
char),
is there any way to make the convertion?

BYTE* bite;
char* ch="help me, please";
bite = (BYTE*)ch; ? is this not right? what shoul i do here?

2) Second Problem

I defined array of WORD in c++;

WORD ar[7];
when how i can give it default value?
default values must be NULL(or zero).

i mean the arrays addres must be clean, must be NULL,

how should i do for it?

thanks

Sep 6 '07 #1
4 2590
HackerisNewKnight wrote:
Hello,
1) First problem.
In my project i need to convert from char* to BYTE*( BYTE is unsigned
char),
is there any way to make the convertion?

BYTE* bite;
char* ch="help me, please";
bite = (BYTE*)ch; ? is this not right? what shoul i do here?
right, but not good.

static_cast<BYTE*>(ch);

But don't cast it to unsigned char*, if it's only a string literal, it's
useless. Or that's only your demo.
2) Second Problem

I defined array of WORD in c++;

WORD ar[7];
when how i can give it default value?
no default value actually,
you may say, how to give initial value.

WORD ar[7] = {1,2,3,4,5,6,7};

or

WORD ar[] = {1,2,3,4,5,6,7};
default values must be NULL(or zero).
the value is random.
>
i mean the arrays addres must be clean, must be NULL,
no such way of expression.
>
how should i do for it?

thanks

--
Thanks
Barry
Sep 6 '07 #2
HackerisNewKnight wrote:
Hello,
1) First problem.
In my project i need to convert from char* to BYTE*( BYTE is unsigned
char),
is there any way to make the convertion?

BYTE* bite;
char* ch="help me, please";
That's a C-ism. 'ch' should be actually declared

char const *ch = ...; // note the 'const'
bite = (BYTE*)ch; ? is this not right? what shoul i do here?
Depends on what you're going to do with it. A cast to a pointer to
non-const BYTE is in most cases wrong. You probably want to cast it
to a pointer to const BYTE.
>
2) Second Problem

I defined array of WORD in c++;

WORD ar[7];
when how i can give it default value?
default values must be NULL(or zero).

WORD ar[7] = {};
i mean the arrays addres must be clean, must be NULL,

how should i do for it?
You cannot control the address of the array. It's allocated when
you define it, and it gets its address there and then.

Again, what do you need 'ar' for? Do you really need it to be
an array of 7 'WORD's?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 6 '07 #3
On Sep 6, 5:07 pm, Barry <dhb2...@gmail.comwrote:
HackerisNewKnight wrote:
1) First problem.
In my project i need to convert from char* to BYTE*( BYTE is unsigned
char),
is there any way to make the convertion?
BYTE* bite;
char* ch="help me, please";
bite = (BYTE*)ch; ? is this not right? what shoul i do here?
right, but not good.
static_cast<BYTE*>(ch);
That shouldn't compile; you need a reinterpret_cast here. Or
static_cast to void*, and then to BYTE*. And as Victor has
pointed out, ch should really be a char const*, so you'd also
need a const_cast.
But don't cast it to unsigned char*, if it's only a string
literal, it's useless. Or that's only your demo.
That would be my real question as well: why the conversion to
begin with? If you're dealing with text, char is the type, not
unsigned char, and while some legacy interfaces in the C library
do require converting an individual char to an unsigned char
(those in <ctype.h>), the need to convert char* to unsigned
char* should be exceedingly rare.
2) Second Problem
I defined array of WORD in c++;
WORD ar[7];
when how i can give it default value?
It depends, of course, on the type. He doesn't say what WORD
is.
no default value actually,
you may say, how to give initial value.
WORD ar[7] = {1,2,3,4,5,6,7};
or
WORD ar[] = {1,2,3,4,5,6,7};
default values must be NULL(or zero).
the value is random.
He can always write something like:

WORD ar[7] = {} ;

to get zero initialization for all of the elements.
i mean the arrays addres must be clean, must be NULL,
no such way of expression.
I'm not even sure what he means. The address of ar can't be
null, ever.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 7 '07 #4
On Sep 7, 10:26 am, James Kanze <james.ka...@gmail.comwrote:
On Sep 6, 5:07 pm, Barry <dhb2...@gmail.comwrote:
HackerisNewKnight wrote:
1) First problem.
In my project i need to convert from char* to BYTE*( BYTE is unsigned
char),
is there any way to make the convertion?
BYTE* bite;
char* ch="help me, please";
bite = (BYTE*)ch; ? is this not right? what shoul i do here?
right, but not good.
static_cast<BYTE*>(ch);

That shouldn't compile; you need a reinterpret_cast here. Or
static_cast to void*, and then to BYTE*. And as Victor has
pointed out, ch should really be a char const*, so you'd also
need a const_cast.
But don't cast it to unsigned char*, if it's only a string
literal, it's useless. Or that's only your demo.

That would be my real question as well: why the conversion to
begin with? If you're dealing with text, char is the type, not
unsigned char, and while some legacy interfaces in the C library
do require converting an individual char to an unsigned char
(those in <ctype.h>), the need to convert char* to unsigned
char* should be exceedingly rare.
2) Second Problem
I defined array of WORD in c++;
WORD ar[7];
when how i can give it default value?

It depends, of course, on the type. He doesn't say what WORD
is.
no default value actually,
you may say, how to give initial value.
WORD ar[7] = {1,2,3,4,5,6,7};
or
WORD ar[] = {1,2,3,4,5,6,7};
default values must be NULL(or zero).
the value is random.

He can always write something like:

WORD ar[7] = {} ;

to get zero initialization for all of the elements.
i mean the arrays addres must be clean, must be NULL,
no such way of expression.

I'm not even sure what he means. The address of ar can't be
null, ever.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


Sorry for my mistake,
i mean by "arrays addres must be clean, must be NULL" is to get zero
initialization for all elements of The array,

now it is clear for me.

thanks for your help
Sep 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Chiller | last post: by
5 posts views Thread by Tim Wong | last post: by
1 post views Thread by tsunami | last post: by
reply views Thread by Yodai | last post: by
reply views Thread by Yodai | last post: by
2 posts views Thread by Fernando Barsoba | last post: by
39 posts views Thread by Martin Jørgensen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.