questions about pointers

This is a discussion on questions about pointers within the C Programming forums, part of the General Programming Boards category; hey guys, im kind of new in c and i have two questions, first what is the void * v[] ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    questions about pointers

    hey guys, im kind of new in c and i have two questions, first what is the void * v[] for? second what is the "comp" in this line? "int (*comp)(void *,void *);" its part of an example program for qsort...is it casting of some kind?
    appreciate any help.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    1) It is an array of pointers that point to a void (can point to anything). So v[1] is a pointer, v[3] is also a pointer (a *void)
    2) It is a function pointer that points to a function that is like int fun(void *, void* )
    Example:
    Code:
    int fun(void* a, void* b)
    {
       ...
       return 0;
    }
    
    int (*comp)(void*, void*);
    
    comp = &fun; //comp will point to fun
    comp(a, b); //like fun(a,b)

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    comp is a pointer to a function returning int
    v is an array of void pointers
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "int (*comp)(void *,void *);" is a function pointer. One use for that is so you can include a function as a parameter to another function:
    Code:
    #include <stdio.h>
    
    void one() {
    	printf("Function one\n");
    }
    
    void two() {
    	printf("Function two\n");
    }
    
    void dowhat (void (somefunction)()) {
    	somefunction();
    }
    
    int main() {
    	dowhat(one);
    	dowhat(two);
    	return 0;
    }
    I've never used C's qsort but apparently you can pass it your own comparison function, which accepts two pointers (to the things to compare) and returns an int.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 09:33 PM
  5. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 04:55 PM

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