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;
}
-------------------------------------
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;
}
-------------------------------------