Help me cast this!
Walter Murray
walter at hpcllz2.HP.COM
Thu May 5 02:04:42 AEST 1988
Bill Daniels writes:
> How do I cast the malloc() in the following program to avoid
> the lint cries of "warning: illegal pointer combination" et al?
[program abbreviated]
> #include <stdio.h>
> main()
> {
> char *malloc();
> struct outfile {
> int j;
> int k;
> } (*output)[];
> output = malloc(sizeof(struct outfile) * 3);
> }
> THIS IS MY LINT'S OUTPUT:
> trial.c
> ==============
> warning: illegal pointer combination
> (12)
> warning: possible pointer alignment problem
> (12)
To avoid the "illegal pointer combination", try:
output = (struct outfile (*)[])malloc(sizeof(struct outfile) * 3);
The "possible pointer alignment problem" warning comes from the
fact that malloc(3) is declared to return char*, even though we
know that the alignment of its return pointer is such that it can
be used for anything. To get rid of that warning, try:
output = (struct outfile (*)[])(int)malloc(sizeof(struct outfile) * 3);
Conversions of pointers to integral types and back are implementation-
defined, but this should work if int on your machine is wide enough
to hold a pointer. If not, try long instead.
--Walter Murray
More information about the Comp.lang.c
mailing list