Help with Array of Pointers!!

Hello everyone. I am new to C++ and have a HW assignment that involves pointers and arrays. I am very confused by pointers and I have no idea how to proceed. I have not tried to write any code because I am confused as to what I am even suppose to be doing. Any help will be appreciated and I am not trying to get someone to write my code for me, I just want some clarification and someone to point me in the right direction (no pun intended). Thanks.

The Assignment:

Write a function that receives an array of pointers to characters. Each pointer in the array points to an array of characters (string). The function should sort the strings by manipulating their pointers.
Last edited on
A lot of people struggle with the concept of pointers at first, so don't worry if you're not "getting it" just yet.


There's far too much to write about pointers that will fit in a box here, so I would very strongly suggest you read the tutorials in these links
http://www.eternallyconfuzzled.com/tuts/languages/jsw_tut_pointers.aspx
http://www.augustcouncil.com/~tgibson/tutorial/ptr.html
http://www.augustcouncil.com/~tgibson/tutorial/arr.html
http://www.daweidesigns.com/cgi-bin/pointers.php
http://cslibrary.stanford.edu/104/ (video)


Arrays/Pointers FAQs:
http://www.c-faq.com/charstring/index.html
http://www.c-faq.com/ptrs/index.html
http://www.c-faq.com/null/index.html
http://www.c-faq.com/aryptr/


When reading/learning about pointers; always keep in mind that a pointer value is just a number (an integer), and a pointer variable is really no different to an int variable, except for the operations that C or C++ will let you do to it.
Thanks Bench82. I have been checking out some tutorials online and I am getting a better understanding of how pointers work but I am still not sure what my professor is asking me to do, especially the part about "sorting the strings to manipulate their pointers," how do I write a function that sorts strings? I will check out the links you posted, thanks again for the help.
I think you should use standard function strcmp to compare strings. For example, let assume that you have an array of pointers to character strings (character arrays)

char *a[] = { "first", "second", "third", "fourth" };

and if I have correctly undestood you should sort this array.
Thanks for the reply vlad, and you have understood me correctly. I am researching the strcmp function now. I was unaware of it before you mentioned here. This is what I have so far. Now I need to write a function to sort them, I assume this is where I will use strcmp. Thanks again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
	char *seasonsPtr[ 4 ] = { "Fall", "Spring", "Winter", "Summer" };

	cout << "The unsorted strings are: " << endl;

	for( int i=0; i < 4; i++)
		cout << seasonsPtr[i] << endl;

        cout << "The sorted strings are: " << endl;
	
	.......
	
	
	return 0;
}
I have looked up several examples and read about how to solve this and I am still at a loss. Can anyone please help? I am not sure if I am allowed to use something like strcmp or not. Is there a way to do it without? Any help is really appreciated.
How exactly do you want them to be sorted? I know that sounds like an obviously dumb question, but it needs to be stated. Like, you could assign a number to each season (Spring 1, Summer 2, Autumn 3, Winter 4), then sort them from lowest to highest kinda thing. Or are you sorting alphabetically? I don't know if you're taking user input either, it seems kinda pointless to make a sorting program without it.
Last edited on
Without strcmp you're looking at some pretty complicated code to alphabetize the pointers.

Thanks for your replies WhiteWind and Need4Sleep. It was not stated how to sort them or if I needed to take user input, that's why I didn't put it. I am pretty sure they need to be sorted alphabetically. Not sure about the user input but I see what you mean. Thanks Need4Sleep, at least now I know i need to use strcmp. Any other advice is more than welcomed.
they can be sorted pretty much any way you dictate. question is basically asking you to create an algorithm for strings. the function doesn't care how it is fed the data so user input has nothing to do with it.

this function definition will sort a char pointer array alphabetically.
"Fall", "Spring", "Winter", "Summer" -- should turn into -- "Fall", "Spring", "Summer", "Winter"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Sort_It(char *ar[], int n)
{
    char * temp;
    for (int i = 0; i < n; i++)
    {
        for (int k = 0; k < n; k++ )
        {
            if (*ar[i] < *ar[k])
            {
                temp = ar[i];
                ar[i] = ar[k];
                ar[k] = temp;
            }
        }
    }
}
Thanks for all the replies. I fooled around with ti quite a bit and got it to work. Appreciate the help!
Topic archived. No new replies allowed.