Thread: Void* in Function Prototype & Passing In an Array - Question

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    24

    Void* in Function Prototype & Passing In an Array - Question

    Hello,

    I'm experimenting with using void pointers as function parameters but I've hit upon a bit of a problem when I pass in an array to a function which uses such pointers. Here's my code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define ARRAYSIZE 10
    
    void voidfunc(void*);
    
    const int SIZEOFINT = sizeof(int);
    
    int main(int argc, char **argv){
    	int number[ARRAYSIZE] = {0,1,2,3,4,5,6,7,8,9};
    	int i;
    	printf("The Size of int is %i\n",sizeof(int));
    	printf("From the Main():\n");
    	for (i = 0; i<ARRAYSIZE;i++){
    		printf("number[%i] is %i\n",i,number[i]);
    	}
    	voidfunc(number);
    	printf("From the Main():\n");
    	for (i = 0; i<ARRAYSIZE;i++){
    		printf("number[%i] is %i\n",i,number[i]);
    	}
    }
    
    void voidfunc(void* array){
    	int i;
    	int intarray[ARRAYSIZE];
    	//intarray[] = (int*)array;
    	printf("From the voidfunc():\n");
    	for (i = 0; i<ARRAYSIZE;i++){
    		*(int*)(array+(i*SIZEOFINT)) = *(int*)((int)array+(i*SIZEOFINT)) + 1;
    		printf("number[%i] is %i\n",i,*(int*)((int)array+(i*SIZEOFINT)));
    		//printf("number[%i] is %i\n",i,intarray[i]);
    	}
    }
    Now the 'problem' occurs in voidfunc(). As you can see I have to use pointer arithmetic to get at the array values i.e.
    *(int*)(array+(i*SIZEOFINT)) = *(int*)((int)array+(i*SIZEOFINT)) + 1;
    i*SIZEOFINT takes care of the size of each array element but I'd like to try and get away from this kind of coding if possible and use typical array access methods i.e. array[index].

    So is there anyway in voidfunc() that I can cast array as a pointer to an array. Everything I try to does not produce the correct result. Using [] typically indexes the array with the incorrect element size or just won't compile.

    Can anyone give me any advice?

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It should be enough to write:
    Code:
    int *intarray = array;
    Then use intarray[i], etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    24
    Quote Originally Posted by laserlight View Post
    It should be enough to write:
    Code:
    int *intarray = array;
    Then use intarray[i], etc.
    Sigh... of course so simple !

    And once again Laserlight you have saved me from hours of bashing my head against the wall !

    thankyou !

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Also sizeof() is evaluated at compile time to a constant value, so no need to assign it to a constant like you're doing in the original code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a dynamic array to a function
    By esmeco in forum C Programming
    Replies: 15
    Last Post: 06-05-2010, 04:25 PM
  2. Newbie Question: sizeof() problem in function
    By Xeyide in forum C Programming
    Replies: 3
    Last Post: 09-04-2009, 12:05 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM