Link to home
Start Free TrialLog in
Avatar of dellaporta
dellaporta

asked on

Casting from array to pointer...

typedef unsigned short t_CHAR;
In the following code:

#define P                     (t_CHAR*)

void main (void)
{
      t_CHAR prova[50] = P"Hello!";

      return;
}

Visual C++ returns a compile time error trying to cast from char[7] to unsigned short*. I really have to maintain such a syntax, so is there any workaround?
Thanks!
Avatar of dellaporta
dellaporta

ASKER

ops... xcuse me, the real question is about the conversion from pointer to array (not from array to pointer), since visual treats this conversion (and conversion from one type of array to another) such as an error.
This can't work use

#define P    (t_CHAR*)

void main (void)
{
  t_CHAR *prova = P"Hello!";


or
 
t_CHAR *prova;
char msg[50];

strcpy (msg,"Hello!");
prova=P msg;

or

t_CHAR prova[50];

memcpy (prova,"Hello!",7);


   
OK, but in my question I've stated that the syntax has to be:
type var[index] = (some #define)"string";

The only syntax can answer my question is:
t_CHAR *prova = P"Hello!";

but in this case try to assign some values to prova[some index] and watch out what happen.
ASKER CERTIFIED SOLUTION
Avatar of rbr
rbr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or write a function

cast_from_char_to_tchar (char *in,t_CHAR *out)
{

while (*in != 0) {
    *out++=*in++;
}
rbr is showing you the syntax that works

 Why are you limitted to the particular syntax?
>Why are you limitted to the particular syntax?

A BETTER question might be:

Why are you limited to the particular INCORRECT syntax?


Perhaps if you stated what you are trying to accomplish and what constraints you are working under, someone could offer a helpful solution.

Your question is akin to asking how to drive a screw into a board using a television broadcast.  There's just no relationship between the two concepts that makes any conversion possible.  If you asked, how can I drive a screw into a board with a brick, at least someone could suggest bashing the screw with the brick to drive it in.  In this case at least SOME conversion is possible.
'cause I'm porting thousands lines of code and have to make the fewer changes.
(char var[10]="1234"; is not perfect in terms of space used but works perfectly!)
My question wasn't about how to make it works, but if I can make it works!
I am not trying to irritate you dellaporta, but what is it you want "porta" to be?  Is it supposed to be
an unsigned short, a string, an array of strings?

C will allow you to assign something like "1234" to a string or to a char or byte array, but it's not going to be happy if you try to cast a string as a numeric type of mismatched length.
dellaporta,

t_CHAR prova[50] = P"Hello!";

is not the same as:

char var[10]="1234";

The char var[10]="1234"; is LEGAL (but ugly) and will be treated by the compiler as an INITIALIZER for the char array var[10].  Note that the following similar code WON't work:

char var[10];
var = "1234";

This is not recognized by the compiler as an initializer and therefore fails.  You cannot assign a static string ("1234") to a char[10].  All you can do at this point is COPY the string using strcpy() or similar function.

To be more like your original example, you should say:

char var[10]= (char *)"1234";

This also keeps the compiler from generating an initializer for the string into the array and also generates an error.
OK, thank you all for these answers and I apologize you of being so little explanatory. Perhaps because I'm actually cold. Since I've to change a lot of code I need to maintain the structure of the software (i.e. I cannot add strcpy(), etc.) and these unsigned short variables should be passed to routines that accept unsigned short pointers, the only way out is to make all of them to be void. Ex.:

The code actually looks like the following snippet:

t_CHAR var[50] = "Hello";  

void test(t_CHAR *str)
{
}

void main()
{

}
OK, thank you all for these answers and I apologize you of being so little explanatory. Perhaps because I'm actually cold. Since I've to change a lot of code I need to maintain the structure of the software (i.e. I cannot add strcpy(), etc.) and these unsigned short variables should be passed to routines that accept unsigned short pointers, the only way out is to make all of them to be void. Ex.:

The code actually looks like the following snippet:

#define P (t_CHAR*)
typedef unsigned short t_CHAR;
t_CHAR var[50] = P"Hello";  

void test(t_CHAR *str)
{
}

void main()
{
  test(Pstr);
}

should became:

#define P
typedef char t_CHAR;
t_CHAR var[50] = P"Hello";  

void test(void *str)
{
   unsigned short* s = (unsigned short*)str;
}

void main()
{
  test(Pstr);
}

if you want the historical reason for such a structure you've to know I must implement a huge Rockwell-native software and port it to WinNT. Since Win uses 8 bit char (forget about w_CHAR, string manipulation routines have to be the same ,i.e. I've re-coded them all!) and Rockwell Scorpio uses 16bit packed char, you may guess what the hell is it.
'bye