Jump to content

multidimensional arrays.

Featured Replies

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.

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.

:)

Just use a char pointer in the function declaration/prototype instead of an array.

 

i.e. int function(char *string);

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

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.