Thread: a pointer to pointer

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    a pointer to pointer

    Hello yet again for some happy fun time! woohoo!

    Today's topic: pointers to pointers

    And here we go...
    So, I'm slightly confused (who is it on the forums who's little quote is "eternally confuzzled"? Or something like that...well it's applying to me quite a bit at the moment haha). Anyways, I wrote this little program that creates an array of pointer to strings for the days of the week...then it sorts them alphabetically using strcmp(). Now, I got the program working and everything but that was simply a result of trial and error and no pure skill on my part. What got me was the bold line in the code (in the order() function):

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    const int DAYS = 7;
    
    void bsort(char**, const int);
    void order(char**, char**);
    
    int main()
    {
    	char* arrweek[DAYS] = { "Monday", "Tuesday", "Wednesday",
    	                              "Thursday", "Friday", "Saturday", "Sunday" };
    	cout << "Presorted list:\n";
    	for (int i = 0; i < DAYS; i++)
    		cout << arrweek[i] << endl;
    	
    	bsort(arrweek, DAYS);
    
    	cout << "\n\nSorted list:\n";
    	for (int i = 0; i < DAYS; i++)
    		cout << arrweek[i] << endl;
    	cout << endl;
    	return 0;
    }
    
    void bsort(char** arr, const int n)
    {
    	int i, j;
    	for (i = 0; i < n-1; i++)
    		for (j = i+1; j < n; j++)
    			order(arr+i, arr+j);
    }
    
    void order(char** day1, char** day2)
    {	
    	if ( (strcmp( (*day1), (*day2)))  > 0 )    //<-- THIS is kind of weird...
    	{
    		char* temp = *day1;
    		*day1 = *day2;
    		*day2 = temp;
    	}
    }
    In that line of code, I was expecting to have to dereference one more level to get to the string contained by the pointer being pointed to....much to my surprise (read: dismay) I only had to dereference one level to make () work...wait a second! Does that mean that strcmp() accepts pointers as arguments and I really only had to pass the addresses of the strings I need compared, not the strings themselves? Please tell me I'm right...after a whole night of being wrong, that would make things look much brighter...

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    28
    Yes, you're right. A variable representing a string (in old-school C style, which you're using) is not really a string per se but a pointer to a character array. If you were to double-dereference day (**day), you would end up with the first character of the string.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    (strcmp( (*day1), (*day2))) > 0 ) //<-- THIS is kind of weird...
    strcmp() takes two char*'s as parameters. If day1 and day2 are of type char**, then dereferencing them once gets you what strcmp() needs. You can't call functions with parameters the functions aren't expecting.

    All the <cstring> functions take char*'s as their parameters.
    Last edited by 7stud; 05-10-2005 at 09:10 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    great info 7stud and orborde...thanks a lot! Maybe this will be the beginning of a "being right" streak...and then hopefully that will casually ease into world domination and then that's all she wrote...for the world muwhahahaha. Thanks again-Chap

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Chaplin27
    who is it on the forums who's little quote is "eternally confuzzled"?
    That would be Prelude.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM