[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Casting from array to pointer...

Posted on 2000-02-03
13
Medium Priority
?
185 Views
Last Modified: 2010-04-15
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!
0
Comment
Question by:dellaporta
  • 6
  • 3
  • 2
  • +2
13 Comments
 

Author Comment

by:dellaporta
ID: 2485137
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
 
LVL 10

Expert Comment

by:rbr
ID: 2485219
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
 

Author Comment

by:dellaporta
ID: 2485229
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
SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

 
LVL 10

Accepted Solution

by:
rbr earned 60 total points
ID: 2485239
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
 
LVL 10

Expert Comment

by:rbr
ID: 2485244
Or write a function

cast_from_char_to_tchar (char *in,t_CHAR *out)
{

while (*in != 0) {
    *out++=*in++;
}
0
 
LVL 18

Expert Comment

by:deighton
ID: 2485252
rbr is showing you the syntax that works

 Why are you limitted to the particular syntax?
0
 
LVL 32

Expert Comment

by:jhance
ID: 2485661
>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
 

Author Comment

by:dellaporta
ID: 2485664
'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
 

Author Comment

by:dellaporta
ID: 2485674
My question wasn't about how to make it works, but if I can make it works!
0
 
LVL 3

Expert Comment

by:3rsrichard
ID: 2486302
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
 
LVL 32

Expert Comment

by:jhance
ID: 2486492
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
 

Author Comment

by:dellaporta
ID: 2486804
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
 

Author Comment

by:dellaporta
ID: 2486826
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question