Odd Pointer Problem

This is a discussion on Odd Pointer Problem within the C Programming forums, part of the General Programming Boards category; Ok i am trying to return an int from a void pointer, that points to a bitmap file, and this ...

  1. #1
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195

    Odd Pointer Problem

    Ok i am trying to return an int from a void pointer, that points to a bitmap file, and this is my function


    note: the file is in little endian and the computers we are using are in big endian so this function also converts it to big endian
    Code:
    int return_int(void *vp)
    {
    	int num;
    	void *big_endian;
    	int i;
    	vp = vp+3;
    	
    	for(i = 0; i < 4; i++)
    	{
    		*((char*)big_endian+i) = *((char*)vp-i);
    	}
    	vp = vp+1;
    	
    	num = *((int*)big_endian);
    	return num;
    }
    so i increment vp by 3 so that i am pointing to the last byte in the bitmap int chunck and use big_endian for scratch space
    as the counter moves they switch the byte order
    then i increment vp once more so that i will be pointing to the first byte of the next int

    the switch from little endian to big endian works fine and the number returns fine but for some crazy reason when i call this function twice like so
    Code:
    data_size = return_int(file);
    height = return_int(file);
    data_size is 196662, what it is supposed to be
    height is also 196662, not what it is supposed to be but the same as data_size

    so when i do this
    Code:
    data_size = return_int(file);
    file=file+4;
    height = return_int(file);
    data_size is 196662, what it is supposed to be
    height is also 256, what it is supposed to be

    for some insane reason the pointer arrithmetic happening in the function is localized, its been a while since i did pointer arrithmetic but this to be makes no sence to me, help please.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    This is nothing to do with pointer arithmetic.

    This is how C transferres parameters to the functions - by value. All changes made to the parameter variables inside the function are hidden from the calling function.

    If you want that your file pointer will be updated inside the function - you should past the pointer to it into the function. In this case its value can be modified inside the function...

  3. #3
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    do you mean like this:
    int return_int(void **vp);

    instead of that i already have:
    int return_int(void *vp)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > void *big_endian;
    Maybe because this isn't pointing anywhere.

    > vp = vp+3;
    Remember, call by value.
    This does NOT change the value of the pointer used to call this function.
    Also, arithmetic on void pointers is undefined anyway, since you need to know the type in order to figure out how much to advance by. sizeof(void) is meaningless.

    The whole pointer arithmetic thing sucks anyway.

    How about this?
    Code:
    #include <stdio.h>
    
    int return_int(void **vp)
    {
      int num = 0;
      unsigned char *p = *vp;
      int i;
      
      for(i = 0; i < 4; i++)
      {
        num = ( num << 8 ) | p[3-i];
      }
      *vp = p + 4;
      return num;
    }
    
    int main ( ) {
      unsigned char buff[] = {
        0x11, 0x22, 0x33, 0x44,
        0x55, 0x66, 0x77, 0x88
      };
      void *p = buff;
      printf( "%x\n", return_int(&p) );
      printf( "%x\n", return_int(&p) );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by loopshot
    note: the file is in little endian and the computers we are using are in big endian so this function also converts it to big endian
    Are you familiar with htonl, htons, ntohl, ntohs?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  2. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 04:50 AM
  3. Pointer problem with FILE *f in function
    By mabuhay in forum C Programming
    Replies: 6
    Last Post: 02-14-2006, 04:15 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21