Hello,
How To pass 2 dimensional array of strings to a function??at a go?
means i dont want to pass address of each string 1 by 1..but want to pass whole 2D array string at a go?
Hello,
How To pass 2 dimensional array of strings to a function??at a go?
means i dont want to pass address of each string 1 by 1..but want to pass whole 2D array string at a go?
and ya those strings are local to main() not the global ones
Maybe it's too early for me, but I don't see why you don't just pass the base address of the array, to the function.
What's this "pass address of each string 1 by 1", stuff about?
Would you show the relevant code in a post and surround it with the forum's code tags, please?
This is what i mean passing address of each string 1 by 1
Look at the above code each string is passed 1 by 1 in loop but i dont want to do that instead of loop i just want to pass the address a single time. How is it possible?Code:#include <string.h> #include <stdio.h> void function(char *temp) { printf("%s\n",temp); } void main() { int i,n; char temp[10][30]; printf("\n\nHow Many Names Do You Want To Enter :"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n\nEnter Name ->%d :",i+1); flushall(); gets(temp[i]); } for(i=0;i<n;i++) { function(temp[i]); } getch(); }
You might write the function as:
You would then call it as:Code:void print_strings(char arr[][30], size_t size) { size_t i; for (i = 0; i < size; ++i) { printf("%s\n", arr[i]); } }
Incidentally, please read our wiki FAQ concerning void main and [url=http://cpwiki.sourceforge.net/Gets]gets[/man] to find out what is wrong with them. You should also indent your code better.Code:print_strings(temp, 10);
Last edited by laserlight; 01-23-2008 at 07:03 AM. Reason: Corrected the example code.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
do this:
Code:void func(char** names, n) { for (int i=0; i<n; i++) puts(names[i]); } // then call like this: func(temp, n);
duh! laserlight is, fast like laser! fast like light!
probably becasu temp is declared as char temp[10][30]
print_strings should be declared as
and not char**Code:void print_strings(char arr[][30], size_t size);
PS. And i should be declared as size_t not int
Last edited by vart; 01-23-2008 at 07:00 AM.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Ok laserlight thank you very much! excellent! and ya i will use int main from now onwards
Indeed, I overlooked both, so I shall make the fixes.print_strings should be declared as
And i should be declared as size_t not int
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ok now can anybody tell me how to do the same job with integer array??i mean please can anybody give me code for that? i have done it by 1 method but anybody knows a sweet and short method for it then please tell me?
Also explain me the funtion of size_t i haven't come across that functionCode:#include <stdio.h> void print(int *a,int m,int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d ",*(a+i*n+j)); } printf("\n"); } } int main(void) { int a[3][3],i,k=1,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { a[i][j]=k++; } } print(&a[0][0],3,3); return 0; }
Last edited by chottachatri; 01-23-2008 at 07:23 AM.
If you base your new code on my corrected example, you should have concluded that:
should be:Code:void print(int *a,int m,int n)
and that:Code:void print(int a[][3],int m,int n)
should be:Code:print(&a[0][0],3,3);
Also, you can use a normal array subscript with a[i][j] instead of trying to compute the array index yourself.Code:print(a, 3, 3);
By the way, I think that an indentation of just one space may be too little. Typically about four spaces would be optimal.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
one dimention parameter is enough - second is hardcoded
Code:void print(int a[][3],int m)
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
A double pointer won't work as a replacement for a 2D array - it only works the other way around, that you can make a 2D array by using a double pointer. If you are going to use a double pointer, you will have to allocate "rows" number of pointers, then assign each pointer to the first element of the rows.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
> but anybody knows a sweet and short method for it then please tell me?
Yeah, it's called copy and paste.
If this is your array,
int arr[2][3][4];
Then this is your function prototype
void func ( int arr[2][3][4] );
Yes, you can then fiddle with the prototype to change it into either of these forms, but you don't buy a lot. Some compilers will warn about the left-most dimension not being empty, but apart from that, it's good.
Alternatives:
void func ( int arr[][3][4] );
void func ( int (*arr)[3][4] );
Irrespective of the form you choose to write the function prototype / declaration, it would be called with
func ( arr );
And the usual array access subscripts you would use if the array were in scope will still do what you want inside the function.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.