help with arrays and pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alias09
    New Member
    • Mar 2010
    • 3

    #1

    help with arrays and pointers

    I'm still a student, and I'm really having a hard time understanding how arrays of pointers and pointer arrays work. (dealing with C++)


    I'm trying to make a 2D Array with pointers, where the first array is a list of X pointers (decided by the user at runtime, and each pointer points to a different array of Y characters, entered by the user

    Then I need to manipulate the array of pointers so that when displayed, the character Arrays are displayed in alphabetic order. (AKA "Andrea", "Billy", "Bob", "Tera", "Treven", "Willy", etc.)


    I'm less worried about the putting in alphabetic order, and more having trouble figuring out how to let the 2D array to be manipulated, in that each character entered is only one element in the array, which can be displayed consecutively like a string while stopping at the end of string mark, individually checked (so as to allow sorting for each letter), and how to use the pointers in the first array to call the second array and alter it.
    ...
    Maybe I just need an easy-to-understand explaination of how pointers, pointer arrays, and arrays of pointers work, and how they are used, cus I'm so confused that I don't even know exactly what I need help with..

    here's some snippets of the code I'm trying to work out, if it helps describe my problem.

    the Array:
    Code:
    Array = new char * [NumEntries]; //array of row pointers
    	for (int i = 0; i < NumEntries; i++)
    		Array [i] = new char [Length+1];
    for taking input:
    Code:
    int TakeInput (char **Array, int NumEntries, int Length)
    	{
    	char Input;
    	int CurrCount (0);
    
    
    	for (int i = 0; i < NumEntries; i++)
    		{
    		cout << "Enter Name: " << i+1;
    		
    		while ((Input = cin.get ()) != '\n')
    			{
    			Array [i][CurrCount] = Input;
    			CurrCount++;
    			
    			if (CurrCount >= Length)
    				break;
    			else;
    			}
    			Array [i][CurrCount+1] = '\0';
    			cout << endl;
    		}
    	return **Array;
    	}
    for displaying them:
    Code:
    void Display (char **Array, int NumEntries)
    	{
    	for (int i = 0; i < NumEntries; i++)
    	{
    		for (char j = 0; j != '\n'; j++) 
    		cout << Array [i][j];
    	}
    	}
    for sorting them:
    Code:
    for (int i = 0; i <= EndCount; i++)
    			for (int j = 0; j <= Length; j++)
    				{
    				if ((Array [i][j]) > (Array [i + 1][j]))
    					{
    					Temp	= Array [i];
    					Array [i]	= Array [i+1];
    					Array [i+1] = Temp;
    					Swapped = true;
    					}
    				else;
    Thanks for your time, I'd appreciate any help you could give me.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Take a look at Arrays Revealed.

    Comment

    Working...