porting weaker to stronger type checking C?

Member
Posts: 321
Joined: 2004.10
Post: #1
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.


Code:
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);
}
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
Hey, that's my code! Grin

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:

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

- Alex Diener
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #3
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.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #4
void* should use regular pointer arithmetic as you'd expect for char*, afaik.
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #5
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.
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #6
ThemsAllTook Wrote:Hey, that's my code! Grin

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:

Code:
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Checking if the app is being run from a locked volume ravuya 20 6,151 Jun 7, 2006 05:21 PM
Last Post: ravuya