GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jan-2008, 04:04
vicky_brsh vicky_brsh is offline
New Member
 
Join Date: Dec 2007
Posts: 25
vicky_brsh has a little shameless behaviour in the past

Returning a 2 dimensional Array from a function


Hi all,

I know there is no need to return a dimensional array because it is nothing but a pointer and even though it is updated in the function the value will be reflected in the main function.

But still i wanted to know whether is this really possible or tnot.

I have the following code for that.


#include <iostream>
using namespace std;


string **func(string Arr[2][5]);
int main()
{
string Arr[2][5];
string Arr1[2][5];

Arr1= func(Arr);
cout << Arr1[0][0] << "Hai" << endl;;



}

string **func(string Arr[2][5])
{
Arr[0][0] = "Hai";
Arr[0][1] = "Hai1";
Arr[1][0] = "Hello";
Arr[1][1] = "Hello1";

return Arr;
}

it is giving error messages as follows

bash-2.04$ g++ String2dimensionalArr.cpp
String2dimensionalArr.cpp: In function 'int main()':
String2dimensionalArr.cpp:11: error: incompatible types in assignment of 'std::string**' to 'std::string [2][5]'
String2dimensionalArr.cpp: In function 'std::string** func(std::string (*)[5])':
String2dimensionalArr.cpp:25: error: cannot convert 'std::string (*)[5]' to 'std::string**' in return
bash-2.04$
bash-2.04$



Can any one teach me how can i achieve this ?


thanks in advance
Vikram
  #2  
Old 04-Jan-2008, 15:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Returning a 2 dimensional Array from a function


Quote:
Originally Posted by vicky_brsh
I know there is no need to return a dimensional array because it is nothing but a pointer
Not true! Not true!!!

In C and C++, an array is not the same as a pointer. Isn't now. Never was. Never will be. Period. Full stop.

You can never pass an array to a function. You can never return an array from a function. You can not copy contents of an array with an assignment statement. Not now. Not ever.

In C and C++, you can pass a pointer to a function and let the function modify what the pointer pointed to. In C++ you can pass a reference to any kind of variable (pointer or anything else) and let the function modify that variable.
Quote:
Originally Posted by vicky_brsh
Can any one teach me how can i achieve this ?
In order to learn such things, you have to buy into a process of studying pointers (big-time). I, frankly, just don't have the personal bandwidth (or stamina) to have high hopes of teaching such things on a general forum like this, but I can show you an example. (See footnote.)

Many C++ programmers (and, in fact, many C programmers) never "have" to do what you are asking. It's just too involved, and it hardly ever is really necessary.

However, since you asked whether such a thing is even possible...

For a 2-Dimensional array, the notation for accessing elements of the array looks the same (to us humans) as the notation for dereferencing a pointer-to-pointer to data item, the two are not the same. An array is not a pointer. (But I said that already.)


One other note: In C and C++, there really is no such thing as a 2-Dimensional array. We have arrays and we have arrays of arrays (and even deeper things)

So, if you declare an array, say
CPP / C++ / C Code:
  string Arr[2][5];

Really have an array of array of strings.

Arr[0] is an array of five strings.
Arr[1] is an array of five strings.

Arr[0][0] is a string
Arr[0][1] is a string
Arr[0][2] is a string
Arr[0][3] is a string
Arr[0][4] is a string

Arr[1][0] is a string
Arr[1][1] is a string
Arr[1][2] is a string
Arr[1][3] is a string
Arr[1][4] is a string

One more thing: When you use the name of an array (by itself; no [] brackets), the compiler takes it to mean a pointer whose value is the first element of the array.

Therefore, with the above declaration for Arr, if you write Arr[0] in a program, it is taken to be a pointer to an array of strings. Its value is the address of Arr[0][0]

Now if you want a function to take a pointer to your kind of array, let's see what you have to do.

First, something simple. Suppose the function doesn't have to return anything (It has a void return type)

The function could be declared and defined with the following header:

CPP / C++ / C Code:
void f1(string s[][5], int r, int c)
{
  // The function is useful only if r is no greater than the first dimension
  // of the array where you declared it and c is no greater than
  // the second dimension where you declared it
  // To access elements in the array, you can use notation like the following
  // s[i][j]
  // where, i is greater than or equal to zero and i is less than r
  // and j is greater than or equal to zero and j is less than c
}
Note that the first argument function must be a pointer to an array of five strings. Nothing else is acceptable. Not a pointer to an array of six strings, not a pointer to an array of four strings, not a pointer-to-pointer to string. Nothing else!!!!!!

So in the function that calls this function, you could have something like
CPP / C++ / C Code:
    string Arr[2][5];
.
.
.
    f1(Arr, 2, 5);
.
.
.

Note that there is no way (no way) for a function to know how many rows and columns the array has unless you tell it!

Now, the fun part: You want the function to return a pointer to an array of five strings. It doesn't have a void type any more, it has a type that is a pointer to an array of five strings.

Here is the function definition:

CPP / C++ / C Code:
string (*(f)(string s[][5], int r, int c))[5]

You have to read complicated declarations "inside out"

The fact that f is in parentheses says that f is a function.
The arguments are in the next set of parentheses:
The First argument is the name of an array of array[5] of strings.
The other two arguments are ints.

Now the return type of the function is a pointer. Pointer to what? A pointer to an array of five strings.

(Now, maybe you see why people try to solve their problems without doing this stuff.)

Anyhow...

Here is an example. The function appends a '!' to the array element specified by the two int arguments.

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

// f is a function that returns a pointer to an array of five strings
string (*(f)(string s[][5], int r, int c))[5]
{
    s[ r ][ c ] += '!';
    return s;
}


int main()
{
    string x[2][5] = {{"Hi00", "Hi01", "Hi02", "Hi03", "Hi04"},
                      {"Hi10", "Hi11", "Hi12", "Hi13", "Hi14"}};
    string (*y)[5]; // A pointer to an array of five strings

    y = x;
    cout << "Initially:" << endl;

    cout << "  x[0][0] = " << x[0][0]
         << ", y[0][0] = " << y[0][0] << endl;
    cout << "  x[1][2] = " << x[1][2]
         << ", y[1][2] = " << y[1][2] << endl << endl;

    y = f(x, 0, 0);
    cout << "After y = f(x, 0, 0) function call" << endl;
    cout << "  x[0][0] = " << x[0][0]
         << ", y[0][0] = " << y[0][0] << endl;
    cout << "  x[1][2] = " << x[1][2]
         << ", y[1][2] = " << y[1][2] << endl << endl;
    y = f(x, 1, 2);
    cout << "After y = f(x, 1, 2) function call" << endl;
    cout << "  x[0][0] = " << x[0][0]
         << ", y[0][0] = " << y[0][0] << endl;
    cout << "  x[1][2] = " << x[1][2]
         << ", y[1][2] = " << y[1][2] << endl << endl;
    return 0;
}

Output:
Code:
Initially: x[0][0] = Hi00, y[0][0] = Hi00 x[1][2] = Hi12, y[1][2] = Hi12 After y = f(x, 0, 0) function call x[0][0] = Hi00!, y[0][0] = Hi00! x[1][2] = Hi12, y[1][2] = Hi12 After y = f(x, 1, 2) function call x[0][0] = Hi00!, y[0][0] = Hi00! x[1][2] = Hi12!, y[1][2] = Hi12!


Regards,

Dave

Back in the 'olden' days, when C++ was new, people learning C++ had already learned C. The biggest single language element of C required for effective problem solving of all but the most trivial problems is, in my opinion, pointers and pointer notation. (So people should have been intimately familiar with pointers and their application.)

I used to recommend that people read (and understand as much as possible) the original C text: The C programming Language by Brian Kernighan and Dennis Ritchie. An awful lot of the examples in that book require that you actually learn and understand about pointers. Beginners don't like it because it's "too hard."

The modern trend is to jump into C++ without ever teaching C (and that means, not teaching much about pointers.) You can do lots of C++ programming using vectors instead of arrays, and there are very good reasons to learn about such things early in your C++ development.

Occasionally you may be faced with things that they don't ever show you in beginning C++, and then you get to go back to good old K&R to learn about pointers.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 10:25
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 05:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:17.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.