| | |
Passing 2D Array of Pointers into a function
![]() |
•
•
Join Date: Sep 2005
Posts: 5
Reputation:
Solved Threads: 0
Dear All,
I am facing a problem in passing 2D array of pointers to a function.
can any one give some suggestions on this.
1. How the Declaration fo the function should be ?
2. How to pass 2D array of pointers to that function?
3. How to access the array elements.
The aray declaration is *ptr[2][3] = { {"AA","XX","FF"},
{"DD","QQ","EE"},
};
It would be great helpful for me,since I am struggling with this one
I am facing a problem in passing 2D array of pointers to a function.
can any one give some suggestions on this.
1. How the Declaration fo the function should be ?
2. How to pass 2D array of pointers to that function?
3. How to access the array elements.
The aray declaration is *ptr[2][3] = { {"AA","XX","FF"},
{"DD","QQ","EE"},
};
It would be great helpful for me,since I am struggling with this one
If you're struggling, duplicate your declaration:
Within foo, you use arg just as you would use ptr unless you're trying to use sizeof, in which case you probably won't get the result you want.
C++ Syntax (Toggle Plain Text)
void foo ( int *arg[2][3] ); int main ( void ) { int *ptr[2][3]; foo ( ptr ); return 0; }
In case you were wondering, yes, I do hate you.
•
•
Join Date: Sep 2005
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
If you're struggling, duplicate your declaration:
Within foo, you use arg just as you would use ptr unless you're trying to use sizeof, in which case you probably won't get the result you want.C++ Syntax (Toggle Plain Text)
void foo ( int *arg[2][3] ); int main ( void ) { int *ptr[2][3]; foo ( ptr ); return 0; }
Hi,
Thanks for your reply.
But I have diffrent types of 2D array of pointers which varies rows and columns size, So in the function declaration/definition I can not specify the rows and columns size.
So I am trying for an alternative.
>So in the function declaration/definition I can not specify the rows and columns size.
Then you can't use an array. The size of the first dimension may be omitted, but the others are required, and the size of an array dimension must be a compile-time constant. If you want to pass a two dimensional array of any size to a function, you use dynamic memory:
You have more options in C++, but you neglected to mention what language you're using.
Then you can't use an array. The size of the first dimension may be omitted, but the others are required, and the size of an array dimension must be a compile-time constant. If you want to pass a two dimensional array of any size to a function, you use dynamic memory:
C++ Syntax (Toggle Plain Text)
#include <stdlib.h> void foo ( int ***arg ); int main ( void ) { int ***ptr = malloc ( 2 * sizeof *ptr ); int i; for ( i = 0; i < 2; i++ ) ptr[i] = malloc ( 3 * sizeof *ptr[i] ); foo ( ptr ); for ( i = 0; i < 2; i++ ) free ( ptr[i] ); free ( ptr ); return 0; }
In case you were wondering, yes, I do hate you.
here is how you initialize the function and pass the values
c++ Syntax (Toggle Plain Text)
char *function(char **array) { //your code here return 0; } int main() { //put values in array function(p); //passing to function }
Maybe you want to take a look at my code:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void func(char **& ref_to_ptr); /* Function declaration */ int main(void) { /* Declare the '2D Array' */ char ** ptr = new char * [5]; ptr[3] = new char[20]; /* Put some data in the array */ ptr[3] = "k"; /* Print the first value on the screen */ cout << "First value: " << ptr[3] << endl; /* Pass the array by reference to the function 'func()' */ func(ptr); /* Again we print on the screen what's in the '2D Array' */ cout << "Second value: " << ptr[3] << endl; /* Wait for the user to press ENTER */ cin.get(); /* Cleanup */ delete[] ptr; /* Tell the Operating System that everything went well */ return 0; } void func(char **& ref_to_ptr) { /* This function demonstrates how to change the value of it's argument(s) */ ref_to_ptr[3] = "tux4life"; }
Last edited by tux4life; Mar 11th, 2009 at 2:40 pm.
![]() |
Similar Threads
- select sort using array of pointers---getting wrong values. (C++)
- Deleting Characters Function (C)
- Passing a Value between an Array and Another Function (C++)
- Passing array of structures into a function (C++)
Other Threads in the C++ Forum
- Previous Thread: Usage of array of pointers
- Next Thread: Problem with AVL tree!
Views: 22075 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment basic beginner binary c++ c/c++ calculator char class classes code command compile compiler console constructor conversion convert count data delete desktop dll dynamic encryption error file files fstream function functions game givemetehcodez graph gui homework http i/o iamthwee image input int lazy library linker list loop looping math matrix member memory newbie number numbers object objects opengl output parameter pointer pointers problem program programming project random read recursion recursive reference search sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual visualstudio void win32 window windows winsock






