• C

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!
dellaportaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dellaportaAuthor Commented:
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.
0
rbrCommented:
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);


   
0
dellaportaAuthor Commented:
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.
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

rbrCommented:
So what result do you want to have. In C an assigment like


char var[10]="1232";

is not allowed.

you can use in your case

t_CHAR prova[50] =
{'H','e','l','l','o','!',0};
if you want that every char is on a different index.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rbrCommented:
Or write a function

cast_from_char_to_tchar (char *in,t_CHAR *out)
{

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

 Why are you limitted to the particular syntax?
0
jhanceCommented:
>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.
0
dellaportaAuthor Commented:
'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!)
0
dellaportaAuthor Commented:
My question wasn't about how to make it works, but if I can make it works!
0
3rsrichardCommented:
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.
0
jhanceCommented:
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.
0
dellaportaAuthor Commented:
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()
{

}
0
dellaportaAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.