Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning a pointer which could be accessed with [][]

Status
Not open for further replies.

Sen7inel

IS-IT--Management
Feb 14, 2001
90
FI
I have a class with a member char foo[][]. How can I return a pointer to this in such a way that I could address it as a two-dimensional array, as it is inside the class?
 
You shouldn't really be returning pointers to your private data inside your class.

But if you want to, look at this
Code:
#include <stdio.h>

char (*func(void))[20] {
    static char foo[10][20];
    return foo;
}

/* The syntax can be a bit tricky, a typedef simplifies this */
typedef char (*twod)[20];
twod func2(void) {
    static char foo[10][20];
    return foo;
}


int main(void) {
    char (*arr)[20] = func();
    twod ar2 = func2();
    int r, c;
    for ( r = 0 ; r < 10 ; r++ ) {
        for ( c = 0 ; c < 20 ; c++ ) {
            arr[r][c] = 0;
            ar2[r][c] = 0;
        }
    }
    return 0;
}

--
 
You can also use one of these methods:
Code:
#include <cstring>
#include <iostream>
using std::cout;
using std::endl;

class CCharArray
{
public:
	CCharArray()
	{
		strcpy( m_String[0], "One" );
		strcpy( m_String[1], "Two" );
		strcpy( m_String[2], "Three" );
		strcpy( m_String[3], "Four" );
		strcpy( m_String[4], "Five" );
	}

	virtual ~CCharArray() {};

	const char* Get( unsigned int index )
	{
		return m_String[index];
	}

	void Get2( const char*  pszArray[5] )
	{
		for ( int nCount = 0; nCount < 5; ++nCount )
		{
			pszArray[nCount] = m_String[nCount];
		}
	}

private:
	char	m_String[5][20];
};

int main( void )
{
	CCharArray cArray;
	const char* pszArray[5];
	cArray.Get2( pszArray );

	cout << endl << pszArray[0] << endl << pszArray[1] << endl << pszArray[2]
		 << endl << pszArray[3] << endl << pszArray[4] << endl;

	cout << endl << cArray.Get(0) << endl << cArray.Get(1) << endl << cArray.Get(2)
		 << endl << cArray.Get(3) << endl << cArray.Get(4) << endl;

	return 0;
}
 
That was my first thought too, but when I tried it out it didn't work right. Do you have an example?
 
This seems to work correctly?

Code:
#include<iostream>


char **foo()
{
	static char *bas[] ={
							"Hello 1",
							"Hello 2",
							"Hello 3"
						};
	std::cout<<"bas = "<<&bas<<std::endl;
	return bas;
}

int main()
{
	char **bar = foo();
	std::cout<<"bar here = "<<bar<<std::endl;
	for(int i=0;i<3;i++)
		std::cout<<bar[i]<<std::endl;
	return 0;
}
 
> Return a pointer-to-pointer, char **
But [tt]char** != char (*arr)[20] [/tt]

Since the question stated that a 2D array was required (well that's what I assumed from the [tt][][][/tt] in the title), my example specified a pointer to an array, not a pointer to a pointer.

Either way is valid, so long as that is what you start with. Random casting from one to another simply isn't going to cut it.

--
 
OK, it works for that function, but when I put it into a class (which is what the original question was for), it compiles but when you run it crashes...
Code:
#include <iostream>
#include <cstring>

class CTest
{
public:
	CTest()
	{
		strcpy( m_String[0], "one" );
		strcpy( m_String[1], "two" );
		strcpy( m_String[2], "three" );
	}

	char **foo()
	{
		std::cout << "bas = " << &m_String << std::endl;
		return m_String;
	}

private:
	char*	m_String[3];
};

int main()
{
	CTest test;
    char **bar = test.foo();
    std::cout << "bar here = " << bar << std::endl;
    for( int i=0; i<3; i++ )
	{
        std::cout << bar[i] << std::endl;
	}
    return 0;
}
 
Well my method isn't proper-like anyhow, so I'd stick to one that works (Salem's method?). Sorry about that. :)

I agree with earlier posters, you shouldn't try and allow direct-access to your class's innards, that's what friend s are for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top