Thread: Trouble in understanding char/char pointer/string/function parameters

  1. #1
    spaghetticode
    Guest

    Trouble in understanding char/char pointer/string/function parameters

    Hey forum,

    currently I am working my way through "Head First C" and I have some trouble in understanding the whole pointer / char-pointer thing.

    The chapter I am currently working on deals with function pointers, and the example given is the qsort-function.

    In one of the exercizes I was to write comparator functions to pass to qsort(). One of these functions is supposed to compare names. The signature should be the following:

    Code:
    int compare_names(const void* a, const void* b) { }
    My solution was:

    Code:
    int compare_names(const void* a, const void* b)
    {
        char *name_a = (char*)a;
        char *name_b = (char*)b;
        
        return strcmp(*name_a, *name_b);
    }
    but the book uses pointers to pointers:

    Code:
    int compare_names(const void* a, const void* b)
    {
        char **name_a = (char**)a;
        char **name_b = (char**)b;
        
        return strcmp(*name_a, *name_b);
    }
    and I am not sure, why in this special case this is a difference. I'm all confused by this whole pointers stuff, but I try to give you an idea of my thoughts:

    A string variable can be a char array, or it can be a pointer to char. When I pass a pointer to char to the compare_names() function, I pass a pointer to the location, where my string (or better: the first character of my string) is stored.

    Since the argument is a void pointer, I have to cast it to a char pointer again to work with it.

    So why do I need a pointer to a pointer here?

    Any hints or help in understanding this will be appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dennis.cpp
    So why do I need a pointer to a pointer here?
    Because that is what qsort will be passing to your comparison function. qsort passes a pointer to an element of the array, which is then converted to a pointer to (const) void. Your array is an array of pointers to char (presumably). Therefore, you should cast to get back the pointer to a pointer to char in your comparison function.
    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
    spaghetticode
    Guest
    Thanks, laserlight. Actually, two pages later the book explains that. I was so busy figuring this out I did not even think about the possibility that an explanation could follow. :-D

    EDIT: Quite amusing, the book has a couple of "Questions & Answers" sections throughout the chapters, one of them including my question. There's another question included:

    Q: Why does my head hurt?
    A: Don’t worry about it. Pointers are really difficult to use sometimes. If you don’t find them a little confusing, it probably means you aren’t thinking hard enough about them.
    Kind of hits the mark... :-)
    Last edited by spaghetticode; 12-02-2012 at 05:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  3. Having trouble understanding 'for' loop parameters
    By spottedzebra in forum C Programming
    Replies: 11
    Last Post: 06-23-2010, 08:23 PM
  4. Trouble passing char/pointer array
    By -Prime- in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2007, 03:38 AM
  5. String & Pointer to char
    By oranges in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2007, 11:12 PM