Cast to an array type

  • Thread starter Asbjørn Sæbø
  • Start date
A

Asbjørn Sæbø

For various reasons, I have a number of typedefs to name arrays of
various sizes and for various purposes. I also have functions taking
variables of such typedef-ed types as their input arguments. Now I
want to feed one such function part of a larger array. To have the
correct type in the call to the function, I am trying to use a cast.
But I can not find a cast that does not produce warnings. See the
line with the comment "Gives compilation error" in the example below.
When compiling with "gcc -Wall", it gives the error "tmp.c:24: error:
cast specifies array type". (gcc 3.4.4, under cygwin.)

Is it possible to cast something to an array type? And if so, how do
I do it?

With kind regards
Asbjørn Sæbø


---------------------------------
#include <stdio.h>
#include <stdint.h>

#define STRINGSIZE 4

typedef uint8_t stringtype[STRINGSIZE];

void func(stringtype a_string)
{
uint8_t k;

for( k = 0; k < STRINGSIZE; k++ )
{
printf("The character is %c\n", a_string[k]);
}
}


int main( void )
{
uint8_t alphabet[] = "abcdefghijklmnopqrstuvwxyz";

func( &alphabet[6] ); /* Works */
func( (stringtype)(&alphabet[6]) ); /* Gives compilation error */

return 0;
}
-------------------------------------
 
E

Eric Sosman

Asbjørn Sæbø said:
For various reasons, I have a number of typedefs to name arrays of
various sizes and for various purposes. I also have functions taking
variables of such typedef-ed types as their input arguments. Now I
want to feed one such function part of a larger array. To have the
correct type in the call to the function, I am trying to use a cast.
But I can not find a cast that does not produce warnings. See the
line with the comment "Gives compilation error" in the example below.
When compiling with "gcc -Wall", it gives the error "tmp.c:24: error:
cast specifies array type". (gcc 3.4.4, under cygwin.)

Is it possible to cast something to an array type?

No.
And if so, how do
I do it?

You probably don't need to. Keep in mind that using the
name of an array in an expression produces a value that is a
pointer to the array's first element (except in a couple of
special cases). In particular, when you "pass an array" to
a function you are really not passing the array itself, but
a pointer. One consequence is that the function does not
know the size of the array that was passed, since the function
receives only the element pointer. That is, the function does
not care (and cannot tell) that the pointer is to the start of
a ten-element array or a forty-element array, or to somewhere
in the middle of a hundred-element array; they're all the same
as far as the function is concerned.

Section 6 of the comp.lang.c Frequently Asked Questions
(FAQ) list at <http://www.c-faq.com/> has more information.
 
M

Martin Ambuhl

Asbjørn Sæbø said:
Is it possible to cast something to an array type? And if so, how do
I do it?

No, but your code loses nothing (and gains) by simply replacing
typedef uint8_t stringtype[STRINGSIZE];
with
typedef uint8_t *stringtype;

But some notes:
1) Avoiding identifiers beginning with "str" is a good idea
2) typedefs of pointer or array types usually obscure rather than
clarify code.
 
T

Tomás Ó hÉilidhe

func( (stringtype)(&alphabet[6]) ); /* Gives compilation error */


func( *(stringtype*)(alphabet+6) );


This takes a int*, converts it to an int(*)[STRINGSIZE] and then
dereferences it to yield an int[STRINGSIZE].

I wouldn't use your design tho if you ask me, there's far cleaner and
simpler ways of doing it.
 
A

Asbjørn Sæbø

Martin Ambuhl said:
Asbjørn Sæbø said:
Is it possible to cast something to an array type? And if so, how do
I do it?

No, but your code loses nothing (and gains) by simply replacing
typedef uint8_t stringtype[STRINGSIZE];
with
typedef uint8_t *stringtype;

Well, the latter is a pointer, while the former is an array. The
storage space for the array is automatically allocated if I create a
variable of that type, while for the pointer, I would have to allocate
it myself.

But some notes:
1) Avoiding identifiers beginning with "str" is a good idea
2) typedefs of pointer or array types usually obscure rather than
clarify code.

Ok, thanks!

Asbjørn
 
B

Barry Schwarz

For various reasons, I have a number of typedefs to name arrays of
various sizes and for various purposes. I also have functions taking
variables of such typedef-ed types as their input arguments. Now I
want to feed one such function part of a larger array. To have the
correct type in the call to the function, I am trying to use a cast.
But I can not find a cast that does not produce warnings. See the
line with the comment "Gives compilation error" in the example below.
When compiling with "gcc -Wall", it gives the error "tmp.c:24: error:
cast specifies array type". (gcc 3.4.4, under cygwin.)

Is it possible to cast something to an array type? And if so, how do
No.

I do it?

You don't.
With kind regards
Asbjørn Sæbø


---------------------------------
#include <stdio.h>
#include <stdint.h>

#define STRINGSIZE 4

typedef uint8_t stringtype[STRINGSIZE];

void func(stringtype a_string)

It only looks like func receives an array of four uint8_t. In
reality, the compiler has converted the type of the parameter to
pointer to uint8_t. With three exceptions, this conversion occurs for
all expression that have array type.
{
uint8_t k;

for( k = 0; k < STRINGSIZE; k++ )
{
printf("The character is %c\n", a_string[k]);
}
}


int main( void )
{
uint8_t alphabet[] = "abcdefghijklmnopqrstuvwxyz";

func( &alphabet[6] ); /* Works */

Since the argument is a pointer to uint8_t, it matches exactly the
type the function expects.
func( (stringtype)(&alphabet[6]) ); /* Gives compilation error */

A cast can only be used to convert to a scalar type. stringtype is an
aggregate type. A compiler diagnostic is mandatory for the constraint
violation.
return 0;
}
-------------------------------------


Remove del for email
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top