Thread: array of pointers problem

  1. #1
    Registered User StevenGarcia's Avatar
    Join Date
    Nov 2006
    Posts
    22

    array of pointers problem

    I am trying to display the data from my list using an array that i pass to my class function display(). I can't seem to find any examples for what I am doing in the tutorials. When I attempt to display the list I get the following error.

    Code:
    Unhandled exception at 0x102aec80 (msvcr80d.dll) in list.exe: 0xC0000005: Access violation reading location 0x00000000.
    I was hoping for a simpler way to accomplish this. But I am very rusty and have to use the trial and error approach. Thanks for any help.


    Code:
    //=========================================================
    // DisplayInput
    //=========================================================
    
    void displayinput(list & mylist)
    {
    	int arraysize = 0;
    	char ** array;
    
    	mylist.display(array, arraysize);
    
    	for(int i = 0; i < arraysize; i++)
    	{
    		cout << array[i] << endl;
    
    	}
    
    	for(int i = 0; i < arraysize; i++)
    	{
    		delete [] array[i];
    	}
    
    	delete [] array;
    
    	return;
    
    }
    
    //==========================================================
    // Display
    //==========================================================
    
    int list::display(char **& array, int & arraysize)
    {
         int i = 0;  //for traversing array	
      
         arraysize = num_in_list;
    
         array = new char*[arraysize];
    
         node * curr;         
         curr = head;
    
         while(curr)
         {
    
    	array[i] = new char[60];
    	array[i] = '\0';            
            strcat(array[i], curr->fname);    
            strcat(array[i], " " );      
            strcat(array[i], curr->lname); 
        
    	++i;
    
            curr = curr->next;
         }
    
         arraysize = num_in_list;
    
          return 0;
    }
    Last edited by StevenGarcia; 11-17-2006 at 09:17 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    arraysize is not initialized.

    Also, I don't remember if this was asked before, but why aren't you using C++ strings?

  3. #3
    Registered User StevenGarcia's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Ah. Thanks for the catch. I made the necesary changes (updated posted code), but I still revceive the same error. I probably should begin to use C++ strings. I am relearning some programming so that I can take a class during the summer and my old teachers wouldn't allow us to use C++ strings. Also, I've never used them before.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> array[i] = '\0';
    I think you meant array[i][0] = '\0';

    If you really want to learn C++, I'd highly recommend learning the C++ string class and vectors. Modern C++ rarely ever uses C style strings and arrays.

  5. #5
    Registered User StevenGarcia's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Thank you for the help. That fixed the problem for me. Much appreciated. = )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  3. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM