PDA

View Full Version : porting weaker to stronger type checking C?


WhatMeWorry
2005.01.13, 01:45 PM
My C legs aren't what the used to be. This straightforward C function
is giving me fits when I try to bring it over to my Codewarrior (ANSI?)
C compiler.

Codewarrior is complaining about an "illegal type" for the second parameter
of the memcpy.

memcpy(rowBuffer, image, rowSize);

compiles fine, so it is probably the pointer/address arthmetic? Or the const
void * of the second parameter? Whatever it is I can't work around it
and I've tried alot of things: Casting, moving image into a const void *,
replacing the pointer form with an array index.

It's been an hour and a half and I figured somebody can probably take a look
and find a solution in 30 seconds. You'll be saving alot of my hair.

thanks.


void flipImageY(void * image, int rowSize, int rowCount)
{
int rowIndex, halfHeight;
void * rowBuffer;

rowBuffer = malloc(rowSize);
halfHeight = (rowCount / 2);
for (rowIndex = 0; rowIndex < halfHeight; rowIndex++)
{
memcpy(rowBuffer, (image + (rowIndex * rowSize)), rowSize);
// other memcpys cut out for simplicity, but same problem as above
}
free(rowBuffer);
}

ThemsAllTook
2005.01.13, 04:39 PM
Hey, that's my code! :D

Maybe Codewarrior doesn't like pointer arithmetic on void * for some reason. You might try typecasting it to a char * and treating it as an array, like so:

memcpy(rowBuffer, &((char *) image)[rowIndex * rowSize], rowSize);

- Alex Diener

Zekaric
2005.01.13, 04:48 PM
I'm surprised that it would compile with other compilers as well. void has no size so the compiler doesn't know how to add 1 to it. Does 1 mean 1 byte, 1 word, 1 long word, 1 stucture size etc.

ThemsAllTook has the right idea by casting the void * to a known sized type.

DoG
2005.01.13, 05:47 PM
void* should use regular pointer arithmetic as you'd expect for char*, afaik.

Zekaric
2005.01.13, 06:04 PM
I'm not sure if that's standard. Most compilers I've used complain in situations like this. But then I set the compiler at max warning level by default and clean up stuff like this so that it's not ambiguous.

WhatMeWorry
2005.01.13, 11:26 PM
Hey, that's my code! :D

Maybe Codewarrior doesn't like pointer arithmetic on void * for some reason. You might try typecasting it to a char * and treating it as an array, like so:

memcpy(rowBuffer, &((char *) image)[rowIndex * rowSize], rowSize);

- Alex Diener


Yes! That does the trick. I guess I should have credited your up front.
Enjoying you're code (well not this one line) and web site.

I must have been hung up on the K&R book. I see memcpy(s,ct,n) where ct is
of type const void *. I interpreted this to mean that memcpy demanded
that the second parameter be a void *, like the function argument. I guess
I should have taken this to mean that the parameter can be a pointer of any
type. In this, case char.

thanks.