i just wants to know to how to pass two dimensiobnal array of string to a function
say:
main()
{
char a[][]={"nitish","abhik",
"vikas"};
sort(%26lt;?%26gt;);
}
void sort(%26lt;?%26gt;)
{
}
i wants to pass array a[][] to the function sort
what are the arguments of sort
simply what is "?"
Thanks
How to pass two dimensional array of string in 'c' ?
No need to pass pointers, do this instead.
#include %26lt;stdio.h%26gt;
void function_that_gets_array(char* theArr[]) {
int i=0;
for (i=0; i %26lt; 3; i++) {
printf("Element %s\n",theArr[i]);
}
return;
}
int main(int argc, char* argv) {
char* a[] = {"one","two","three"};
function_that_gets_array(a);
return 1;
}
------
This example compiles, and works.
gubatrons-macbook-pro:/tmp gubatron$ gcc sample.c -o sample
gubatrons-macbook-pro:/tmp gubatron$ ./sample
Element one
Element two
Element three
-----
The trick is not to use a[][], but char* a[];
Reply:you need to define the number of columns in the array to be passed.
for example:
a[][] = {...};
sort(a); //sort(a) will get an error
sort(a[?]);
// these will be valid but you need to specify the ? so that the compiler knows how the array should be oriented. (simply put the compiler cannot know where the rows ends and the columns ends because it does not know the dimensions)
or you could just pass a pointer to a 2D array like this:
char *ptr_2D;
char a[][] = {...};
ptr_2D = a;
//im not sure about this, you may need to include the dimensions as well
sort(char*);
sort(ptr_2D);
Reply:using pointers...
-----------------------
function declaration.
sort(char * );
function definition
sort(char *arr)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment