![]() |
|
#1
|
|||
|
|||
Returning a 2 dimensional Array from a functionHi 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
|
|||
|
|||
Re: Returning a 2 dimensional Array from a functionQuote:
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:
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:
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:
So in the function that calls this function, you could have something like CPP / C++ / C Code:
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:
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:
Output: Code:
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 GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need Help with input files. | Efferus | C++ Forum | 2 | 24-Nov-2007 16:19 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 19:31 |
| Pointer Usage in C++: Beginner to Advanced | varunhome | C++ Forum | 0 | 19-Aug-2005 09:25 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 04:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The