Can I cast an array type ?

A

absurd

Can I cast an array to another array type with different size ? I
found my first cast statement OK with g++. But second one is not. What
is correct syntax I should use to make the second cast OK ? Thanks !



typedef char S [4];

int main()
{
char t[56];
S & s = reinterpret_cast<S &)>(t); //OK
S & s = reinterpret_cast<char [4] & >(t); //NOT OK
}
 
L

Luc Danton

Can I cast an array to another array type with different size ? I
found my first cast statement OK with g++. But second one is not. What
is correct syntax I should use to make the second cast OK ? Thanks !



typedef char S [4];

int main()
{
char t[56];
S& s = reinterpret_cast<S&)>(t); //OK
S& s = reinterpret_cast<char [4]& >(t); //NOT OK
}

The syntax would be
S& s = reinterpret_cast<char (&)[4]>(t);
(well you'd need to name the variable something else than s)

Also no, it is not a good idea to do that.
 
G

Giaffy

Can I cast an array to another array type with different size ?  I
found my first cast statement OK with g++. But second one is not. What
is correct syntax I should use to make the second cast OK ? Thanks !
typedef char S [4];
int main()
{
     char t[56];
     S&  s = reinterpret_cast<S&)>(t);  //OK
     S&  s = reinterpret_cast<char [4]&  >(t);  //NOT OK
}

The syntax would be
S& s = reinterpret_cast<char (&)[4]>(t);
(well you'd need to name the variable something else than s)

Also no, it is not a good idea to do that.

Hi Luc,
but is there any reasons to specify the array size in the cast ?
sorry but I do not catch it, can you explain a bit more ?
as far i can tell, I would only cast to char [] ...

thanks
Giaffy
 
L

Luc Danton

On 08/11/2010 22:50, absurd wrote:





Can I cast an array to another array type with different size ? I
found my first cast statement OK with g++. But second one is not. What
is correct syntax I should use to make the second cast OK ? Thanks !

typedef char S [4];

int main()
{
char t[56];
S& s = reinterpret_cast<S&)>(t); //OK
S& s = reinterpret_cast<char [4]& >(t); //NOT OK
}

The syntax would be
S& s = reinterpret_cast<char (&)[4]>(t);
(well you'd need to name the variable something else than s)

Also no, it is not a good idea to do that.
Hi Luc,
but is there any reasons to specify the array size in the cast ?
sorry but I do not catch it, can you explain a bit more ?
as far i can tell, I would only cast to char [] ...

thanks
Giaffy

You can't have a reference to an incomplete type, and T[] is an
incomplete type. Perhaps you can use a char* (or pair of char*) instead?
 
G

Goran Pusic

Can I cast an array to another array type with different size ?  I
found my first cast statement OK with g++. But second one is not. What
is correct syntax I should use to make the second cast OK ? Thanks !

typedef char S [4];

int main()
{
    char t[56];
    S & s = reinterpret_cast<S &)>(t);  //OK
    S & s = reinterpret_cast<char [4] & >(t);  //NOT OK

}

As said, it's char(&)[4] in the second cast. You do need a size,
otherwise reinterpret_cast doesn't know how big is your array and will
not do it. Furthermore, if you look at any explanation what
reinterpret_cast does, it always speaks about pointers. But here, you
have arrays. And arrays are not pointers, not in C, not in C++. True,
their their type is often "erased" by the compiler, e.g. as in:

char a[3];
void f(char* p)
{
}
f(a); // "Array-ness" ;-) of a erased here;
// f only sees a pointer.)

So people tend to mix pointers and arrays.

You __can__ use reinterpret_cast the way you do, but I wonder if
that's a good idea in your case. If I were to guess, I'd say that your
"t" is some data stream (or a "raw" data block), and that you want to
"parse" it into some structure that works for you (e.g. a 4-byte
integral, where "source has a different endianness from your system?).

Goran.
 
J

James Kanze

On 08/11/2010 23:32, Giaffy wrote:
You can't have a reference to an incomplete type, and T[] is
an incomplete type. Perhaps you can use a char* (or pair of
char*) instead?

Since when? You can certainly have a reference to some
incomplete types. Is there some special restriction concerning
incomplete array types? If so, where?
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top