NeonBlack Posted January 10, 2006 Posted January 10, 2006 How do you pass a multidimensional array to a function in c++ and old-fashioned c? I assumed it would be the same as a 1-dimensional array doing the function prototype for example: int function (char []); and the function header as int function (char array []) but I get an 'need explicit cast' error from the compiler. I have done searches and looked on cprogramming.com, but have found nothing.
In My Memory Posted January 10, 2006 Posted January 10, 2006 On this C++ tutorial, it says: In a function declaration it is also possible to include multidimensional arrays. The format for a tridimensional array parameter is: base_type[][depth][depth] for example, a function with a multidimensional array as argument could be: void procedure (int myarray[][3][4]) Notice that the first brackets [] are left blank while the following ones are not. This is so because the compiler must be able to determine within the function which is the depth of each additional dimension.
bascule Posted January 10, 2006 Posted January 10, 2006 Just use a char pointer in the function declaration/prototype instead of an array. i.e. int function(char *string);
slur Posted January 17, 2006 Posted January 17, 2006 In C and C++ arrays and pointers are basically the same thing. A char* is what you're actually dealing with when you refer to an array of characters. The square brackets are an operator that you use to calculate an offset from the character pointer. In fact, you can legally use brackets with a variable you've defined as a char* and it has the same effect as using the + operator (in one-dimensional arrays). Here's a snippet to show the possibilities: char *my_pointer; char my_array[12]; char mychar; my_pointer = my_array; // point to the start of the array mychar = my_pointer[6]; // get the 7th character in the array my_pointer = &my_array[5]; // the address of the 6th character mychar = *my_pointer; // the 6th character mychar = my_pointer[2]; // the 8th character my_pointer = my_array; mychar = my_pointer[3][3]; // the 8th character
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now