Dynamic Array of Pointers to String

This is a discussion on Dynamic Array of Pointers to String within the C++ Programming forums, part of the General Programming Boards category; sir, yesterday i tried to written a programme which takes some(any number) names as input and sort in ascending order. ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Dynamic Array of Pointers to String

    sir,

    yesterday i tried to written a programme which takes some(any number) names as input and sort in ascending order.
    Syntax is correct and programme works partially. really i don't know how to create dynamic array of pointers in C++. would you please help me.....
    i am attaching my code with this post. please take look
    Code:
    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    	clrscr();
                                                                                 //*a = new (int*)[SIZE];
                                                                                //for(int j=0; j<SIZE; j++)
                                                                                //a[SIZE] = new int[SIZE];
    	int size,p,m,n,i,j,k;
    	cout<<"enter the no of names: ";
    	cin>>size;
    	char **pt=new char*[size];
    	char *ptr;
    	cout<<"\nEnter names: ";
    	for(i=0;i<size;i++)
    		cin>>*(pt+i);
    	for(i=0;i<size-1;i++)
    	{
    		p=0;
    		for(j=0;j<size-1-i;j++)
    			for(k=0;;k++)
    			{
    				if(*(pt+p)+k=='\0')
    				{
    					p=j;
    					break;
    				}
    				else if(*(pt+j+1)+k=='\0')
    					break;
    				m=*(*(pt+p)+k);
    				n=*(*(pt+j+1)+k);
    				if(m==n)
    					continue;
    				else if(n>m)
    				{
    					p=j+1;
    					break;
    				}
    				else
    					break;
    			}
    		ptr=*(pt+p);
    		*(pt+p)=*(pt+size-i);
    		*(pt+size-i)=ptr;
    	}
    	for(i=0;i<size;i++)
    		cout<<*(pt+i);
    	delete []pt;
    	getch();
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    That code is horribly broken (not to mention an eyesore). Briefly, the main problem is that you are allocating an array of pointers, but neglecting to allocate memory to each element. Even so, you need to instruct 'cin' how many characters it is allowed to copy to on each read.

    Fortunately, C++ comes with a powerful library of code, eg. the STL (Standard Template Library). In place of char* arrays, you can use std::string, and instead of 'raw' arrays, you have std::vector.

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    int main( void )
    {
    	using namespace 
    		std;
    	string
    		input;
    	vector< string >
    		lines;
    	for( ;; )
    	{
    		cout << "Enter a line of text (or an empty line to exit) > ";
    		if( !getline( cin, input ) || input.empty( ) )
    			break;
    		lines.push_back( input );	
    	}
    	if( !lines.empty( ) )
    	{
    		cout << "You entered: " << endl;
    		// copy( lines.begin( ), lines.end( ), ostream_iterator< string >( cout, "\n" ) );
    		for( size_t index = 0, limit = lines.size( ); index < limit; ++index )
    		{
    			cout << "[ " << index << " ] : '" << lines[ index ] << "'" << endl;
    		}
    	}
    	return 0;
    }
    Much easier, no?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    That's the most obfuscated attempt at writing a sorting algorithm I have ever seen. I'm not even going to waste time trying to figure out if it is actually correct or not (which is most unusual for me).
    The best thing you can do to improve it (short of using std::sort etc, as shown) is to take advantage of the fact that "*(a + i)" is exactly equivalent to "a[i]".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Navigating a character string array from pointers
    By Boxknife in forum C Programming
    Replies: 3
    Last Post: 04-16-2009, 01:32 PM
  2. fprint an array of string pointers?
    By syaorankun in forum C Programming
    Replies: 4
    Last Post: 02-11-2008, 11:52 PM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. **Pointer to a Dynamic Array of Pointers
    By The Brain in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2005, 06:52 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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