TechnologY

Why can’t char** be the return type of the following function

I have the following function in C++ :

char** f()
{
    char (*v)[10] = new char[5][10];
    return v;
}

Visual studio 2008 says the following:

error C2440: 'return' : cannot convert from 'char (*)[10]' to 'char **'

What exactly should the return type to be, in order for this function to work?

typedef char carr[10];

carr* f()
{
    char (*v)[10] = new char[5][10];
    return v;
}

Basically, char (*v)[10] defines a pointer to a char array of size 10. It’s the same as the following:

 typedef char carr[10]; //carr is a char array of size 10

 carr *v; //v is a pointer to array of size 10

So your code becomes equivalent to this:

carr* f()
{
    carr *v = new carr[5];
    return v;
}
REFERENCE: http://stackoverflow.com/questions/7138720/why-cant-char-be-the-return-type-of-the-following-function-in-c?rq=1
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s