How to use void* as an arguement?

This is a discussion on How to use void* as an arguement? within the C Programming forums, part of the General Programming Boards category; I was wondering how qsort works with the combination of typeSize and void* input. I want to do arithmetic, but ...

  1. #1
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

    How to use void* as an arguement?

    I was wondering how qsort works with the combination of typeSize and void* input. I want to do arithmetic, but I'm not totally sure how to work this out...I'm thinking I could do a switch( typeSize ) and then convert to the appropriate type based on that, but I wouldn't be able to distinguish the difference between an int and an unsigned int.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    /*
    	I want the input to be void*..not long.
    */
    void hmmm( void *v, size_t typeSize )
    {
    	/*
    		void*.c: In function `hmmm':
    		void*.c:7: warning: dereferencing `void *' pointer
    		void*.c:7: error: invalid use of void expression
    	*/
    	*v = 5;
    }
    
    int main( void )
    {
    	int n = 4;
    	char c = 4;
    	
    	hmmm( &n, sizeof( n ) );
    	hmmm( &c, sizeof( c ) );
    	
    	assert( n == 5 );
    	assert( c == 5 );
    	
    	return 0;
    }
    Last edited by Kleid-0; 05-11-2005 at 06:52 PM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You'll have to cast the void* to another pointer type prior to dereferencing it.

    As a hint:

    sizeof(char) == 1

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I was so sure unsigned and signed would cause problems, but it doesn't! Thanks Thantos!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    void hmmm( void *v, size_t typeSize )
    {	
    	char *cv;
    	int *nv;
    
    	switch( typeSize )
    	{
    		case 1:
    			cv = ( char* )v;
    			*cv = 5;
    			break;
    		case 4:
    			nv = ( int* )v;
    			*nv = 5;
    			break;
    	}
    }
    
    int main( void )
    {
    	unsigned int un = 2;
    	int n = 3;
    	char c = 4;
    	
    	hmmm( &un, sizeof( un ) );
    	hmmm( &n, sizeof( n ) );
    	hmmm( &c, sizeof( c ) );
    	
    	assert( un == 5 );
    	assert( n == 5 );
    	assert( c == 5 );
    	
    	return 0;
    }

  4. #4
    FOX
    Join Date
    May 2005
    Posts
    188
    Is it really necessary to cast the void pointer when you're not dereferencing it? GCC does not complain if you remove the cast in the code marked red. An address is an address after all, no matter what it points to.

    Code:
    void hmmm( void *v, size_t typeSize )
    {	
    	char *cv;
    	int *nv;
    
    	switch( typeSize )
    	{
    		case 1:
    			cv = ( char* )v;
    			*cv = 5;
    			break;
    		case 4:
    			nv = ( int* )v;
    			*nv = 5;
    			break;
    	}
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're not dereferencing it there. You're making a pointer assignment.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    FOX
    Join Date
    May 2005
    Posts
    188
    > You're not dereferencing it there. You're making a pointer assignment.
    I know, that's why I wrote "when you're not dereferencing it".

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Hey ^xor, I think quzah was just clarifying it for me. Thank you for your comments though :], the more comments the better :].

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ^xor
    > You're not dereferencing it there. You're making a pointer assignment.
    I know, that's why I wrote "when you're not dereferencing it".
    As I have said many many many times: You never need to typecast a void * when making a pointer assignment. You only ever need to typecast it if you're trying to dereference the void pointer itself.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    main(void)
        {
    
    int a;
    a=5;
    
    void* pa;
    pa = &a;
    
    void* ppa;
    ppa = &pa;
    
    void* pppa;
    pppa = &ppa;
    
    void* ppppa;
    ppppa = &pppa;
    
    void* pppppa;
    pppppa = &ppppa;
    
    system("clear");
    
    printf("\n%d \n", a);
    
    printf("\n%p ", pa);
    
    printf("\n%d \n", *(int*) pa);
    
    printf("\n%p ", ppa);
    
    printf("\n%p ", *(void**) ppa);
    
    printf("\n%d \n", **(int**) ppa);
    
    printf("\n%p ", pppa);
    
    printf("\n%p ", *(void***) pppa);
    
    printf("\n%p ", **(void***) pppa);
    
    printf("\n%d \n", ***(int***) pppa);
    
    printf("\n%p ", ppppa);
    
    printf("\n%p ", *(void****) ppppa);
    
    printf("\n%p ", **(void****) ppppa);
    
    printf("\n%p ", ***(void****) ppppa);
    
    printf("\n%d \n", ****(int****) ppppa);
    
    printf("\n%p ", pppppa);
    
    printf("\n%p ", *(void*****) pppppa);
    
    printf("\n%p ", **(void*****) pppppa);
    
    printf("\n%p ", ***(void*****) pppppa);
    
    printf("\n%p ", ****(void*****) pppppa);
    
    printf("\n%d \n", *****(int*****) pppppa);
    
    }
    this is my solution for this matter.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    miroslavgojic, the thread is over 5 YEARS old - please don't dig up the graveyard of old posts.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template arguement probelm.
    By King Mir in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2007, 02:18 PM
  2. function with array arguement
    By timhxf in forum C Programming
    Replies: 5
    Last Post: 01-05-2007, 02:20 PM
  3. passing a vector as an arguement
    By gL_nEwB in forum C++ Programming
    Replies: 7
    Last Post: 05-13-2006, 10:11 PM
  4. function call as a 'case' arguement
    By rc7j in forum C Programming
    Replies: 2
    Last Post: 02-17-2002, 07:09 PM
  5. an intersting arguement
    By iain in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 01-21-2002, 12:39 AM

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