I want to pass this 2-D array by reference (i mean in called function i would modify the value of this array and nothing will be return but if i again print the array in calling function in should print the changed value)?
-
[code] void fun(char **a) { //some modification ... each character X will be replaced by 'A' //return nothing } main() { char **a; // a 3x3 char array // initiallize all values with character 'X' print a //print 3 rows each containing 3 'X' . fun(a); // here i want to pass 2D array by reference something like //fun(&a) or anyother method print a //print 3 rows each containing 3 'A' . } [/code ]
-
Answer:
You can do this by three approaches. They are as follows:- Passing the array as an argument:- In this approach, you pass the array you want to modify as an argument. However, you need to understand that unlike 1D array, this array is not automatically degenerated into a pointer because a pointer to a pointer is being used here. So, the body of the function will be as follows:- void foo(int array[][3]) { // body of the code } 2. Passing the pointer to an array as the argument:- In this approach, you can create a pointer to an array which actually helps you create a two dimensional array and this 1D array can be passed to the function as an argument. int *arr[3]; for(int i = 0;i<3;i++) int *arr[i] = new int(3); void foo(int *array[3]) { // code to be written } foo(arr); 3. Passing a double pointer as an argument:- In this approach create a pointer to a pointer and allocate memory to it. Then pass it as an argument to the required function. This can be done as follows:- int** arr; arr = new int *[3]; for(int i=0;i<3;i++) arr[i] = new int(3); // now you have the required 2D array // enter the values you want in arr[3][3] void foo(int** a) { // write the code for the array } foo(arr); //print the array here The memory allocation is in C++ but that shouldn't be a big problem.
Kunal Suri at Quora Visit the source
Related Q & A:
- How can I change a value in an array?Best solution by Stack Overflow
- How to pass javascript jQuery variable value in php array?Best solution by Stack Overflow
- Why does yahoo keep asking me if I want to leave my ID signed in for 2 weeks?Best solution by Yahoo! Answers
- I want to return to old yahoo mail format.Best solution by Yahoo! Answers
- Why the iPod is called as iPod what does the "i" mean?Best solution by ipod.about.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.