convertion problem? from char* to unsigned char*?

  • Thread starter HackerisNewKnight
  • Start date
H

HackerisNewKnight

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
 
B

Barry

HackerisNewKnight said:
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.
 
V

Victor Bazarov

HackerisNewKnight said:
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
 
J

James Kanze

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
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};

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.
no such way of expression.

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

HackerisNewKnight

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
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.
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:[email protected]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top