Can anybody tell me methods about how to pass a 2d array to a function in c language?
-
please tell me about function prototype if i have to pass a pointer to array related with a 2d array.which is best method to pass a 2d array to a function?
-
Answer:
arrays in c are really just pre allocated pointers. You have a couple ways you can pass them to a function. you can declare your function to accept a 2d array void myFunct(int array[5][5]); to call it -- int myArray[5][5]; myFunct(myArray); think of an array of integers as a pointer that points to the first integer in the array. when you use an index [ ] you are just telling it the offset from the starting pointer. so you declare an int x[5] array x would be a pointer essentially. if you want to access the third int x[2] that is telling the compiler take the pointer x (a memory location and add the size of integer * 2 to it. Basically it is just pointer math but the compiler does it for you to prevent memory leaks and helps prevent access of non-allocated memory. you can actually allocate something dynamically and access with the array [] index feature. int *x; //pointer of type int //allocate a dynamic array of 5 ints x = (int*) malloc(sizeof(int) * 5); x[0] = 1; //is the same as *x=1; x[1] = 2; //is the same as *(x + (sizeof(int) *1)) = 2; //manual pointer math a 2d array is simply a pointer to a pointer. int my2d[5][5]; the first index is pulling a pointer to the actual array (and since and array is simply a pointer) it's a pointer to a pointer. if you wanted to pull out individual arrays you could int my2d[5][5]; int *ptr; //ptr is now an array of my2d[0][0-4] ptr = my2d[0] //ptr[4] is the same as my2d[0][4] so another way to pass a 2d array would be. void myFunct(int **array); called by same int myArray[5][5]; myFucnt(myArray);
sumit d at Yahoo! Answers Visit the source
Other answers
pass a pointer to the array
cmeinco
Related Q & A:
- How to convert a char array into an int in C++?Best solution by Stack Overflow
- How to pass a parameter to a function that is called on an event?Best solution by Stack Overflow
- How to pass a C structure in Python?Best solution by Stack Overflow
- How can i tell if my 2000 ford f250 has a limited slip rearend?Best solution by Yahoo! Answers
- How can I tell if I am a baritone or a tenor?Best solution by quora.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.