Like Tree1Likes
  • 1 Post By laserlight

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

This is a discussion on Trouble in understanding char/char pointer/string/function parameters within the C Programming forums, part of the General Programming Boards category; Hey forum, currently I am working my way through "Head First C" and I have some trouble in understanding the ...

  1. #1
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    199

    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.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: openSUSE 13.2 / KDE SC 4.14.6
    Compiler: gcc 4.8-7.1.2 with KDevelop 4.7.1-4.1

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,616
    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.
    dennis.cpp likes this.
    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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    199
    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 dennis.cpp; 12-02-2012 at 04:27 AM.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: openSUSE 13.2 / KDE SC 4.14.6
    Compiler: gcc 4.8-7.1.2 with KDevelop 4.7.1-4.1

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, 10:12 PM

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