Say I have an array: array[2][4], and inside the main method, I have a call to a function blackandwhite. How can I pass this method the length and width of the array as arguments?
Say I have an array: array[2][4] , and inside the main method, I have
Share
This is a possible solution :
One can find the size of an array i.e. the number of elements in it by the following construct :
Unfortunately, C arrays are native arrays and do NOT contain any metadata embedded in them. Rows and Columns are just a way of representing/accessing what is essentially linear storage space in memory. AFAIK, there is no way to automatically determine the number of rows/columns of a 2D-array, given a pointer to it (in C).
Hence one needs to pass the number of columns/rows as separate arguments along-with the pointer to the 2D-array as shown in the example above.
More info on a related question here.
UPDATE:
Common pitfall no.1 : Using
int** arrayin param-listNote that a pointer to a 2 dimensional array of integers is still a pointer to a int.
int**implies that the param refers to a pointer to a pointer to an int, which is NOT the case here.Common pitfall no.2 : Using
int[][]in param-listFailing to pass the dimension(s) of the array. One need NOT pass the size of the array’s 1st dimension (but you may but the compiler will ignore it). The trailing dimensions are compulsory though. So,