By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,429 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,429 IT Pros & Developers. It's quick & easy.

Multidimensional arrays as parameters

P: 5
I have a multidimensional array called 'ticket', which has 25 rows and 6 columns. I want to parse this array to a function whose purpose is to initialize the array contents of ticket (for simplicity, let's assume all elements in the array are initialized to zero). Please note, I *must* use a separate function to achieve this initialization.

Below is the code I have, which is clearly wrong, but hopefully it makes it clear as to what I want to achieve. If someone could please correct my code or point me in the right direction it would be greatly appreciated.

Thank you.



Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     int ticket[25][6];
  4.  
  5.     /* ... */
  6.  
  7.     /* Fill array with zeros */
  8.     init_ticket(ticket);
  9.  
  10.     /* ... */
  11. }
  12.  
  13. void init_ticket(int ticket)
  14. {
  15.     int col;
  16.     int row;
  17.  
  18.     for (row = 0; row < 25; row++)
  19.     {
  20.         for (col = 0; col < 6; col++)
  21.         {
  22.             ticket[row][col] = 0;
  23.         }
  24.     }
  25. }
Dec 30 '06 #1
Share this Question
Share on Google+
4 Replies


vpawizard
P: 66
I have a multidimensional array called 'ticket', which has 25 rows and 6 columns. I want to parse this array to a function whose purpose is to initialize the array contents of ticket (for simplicity, let's assume all elements in the array are initialized to zero). Please note, I *must* use a separate function to achieve this initialization.

Below is the code I have, which is clearly wrong, but hopefully it makes it clear as to what I want to achieve. If someone could please correct my code or point me in the right direction it would be greatly appreciated.

Thank you.



Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     int ticket[25][6];
  4.  
  5.     /* ... */
  6.  
  7.     /* Fill array with zeros */
  8.     init_ticket(ticket);
  9.  
  10.     /* ... */
  11. }
  12.  
  13. void init_ticket(int ticket)
  14. {
  15.     int col;
  16.     int row;
  17.  
  18.     for (row = 0; row < 25; row++)
  19.     {
  20.         for (col = 0; col < 6; col++)
  21.         {
  22.             ticket[row][col] = 0;
  23.         }
  24.     }
  25. }


Hello,
The array 'ticket' is a two dimensional array. So, the data type of ticket is int[][] or int** and not int. So, the argument of init_ticket would be int [][] ticket or int** ticket. The function call is perfectly okay.

Regards,
Dec 30 '06 #2

P: 5
Thanks for your prompt reply.

Doing so results in the following compiler (gcc) warning:

gcc -g -Wall -ansi -pedantic -o rand rand.c
rand.c: In function 'main':
rand.c:43: warning: passing argument 1 of 'init_ticket' from incompatible pointer type


Code below:

Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     int ticket[25][6];
  4.  
  5.     /* ... */
  6.  
  7.     /* Fill array with zeros */
  8.     init_ticket(ticket);
  9.  
  10.     /* ... */
  11. }
  12.  
  13. void init_ticket(int** ticket)
  14. {
  15.     int col;
  16.     int row;
  17.  
  18.     for (row = 0; row < 25; row++)
  19.     {
  20.         for (col = 0; col < 6; col++)
  21.         {
  22.             ticket[row][col] = 0;
  23.         }
  24.     }
  25. }
Dec 30 '06 #3

vpawizard
P: 66
This is because the ticket is an array and the procedure is expecting a double-pointer.

http://gcc.gnu.org/ml/gcc-bugs/2005-03/msg01391.html

You can declare the function as
void init_ticket(int[25][6]);

and the declarator line of definition as
void init_ticket(int ticket[25][6]);

Another point to note here is that only first dimension of array can be empty.

void init_ticket(int[][]); //Error
void init_ticket(int[][6]); //OK
void init_ticket(int[25][6]); //OK

The same rule applies for arrays with more dimensions.i.e.arr[][6][10]

Regards,
Dec 30 '06 #4

P: 5
Thanks again for your help; it seems to be working :-)

I have a question though that you might be able to clear up for me. I am interested to know why the array can be modified when parsed by value. I thought that it could only be modified if it were parsed by reference. My code is below.

I am currently reading a book on C (Practical C Programming, Oualline) and have not yet reached pointers/references. It has been a while since I last covered this material, so perhaps my memory is a little foggy.



Expand|Select|Wrap|Line Numbers
  1. #define GAMES       6
  2. #define SELECTIONS 25
  3.  
  4. /* This is parsing by value, right? */
  5. void create_ticket(int ticket[][SELECTIONS])
  6. {
  7.     int  game;
  8.     int  selection;
  9.  
  10.     for (game = 0; game < GAMES; game++)
  11.     {
  12.         for (selection = 0; selection < SELECTIONS; selection++)
  13.         {
  14.             /* Initialize array elements to zero */
  15.             ticket[game][selection] = 0;
  16.  
  17.             /* When I print the array's elements **OUTSIDE** of this function
  18.              * it is evident that the values have been initialized correctly.
  19.              * How can this be since I parsed the array by value???
  20.              */
  21.         }
  22.     }
  23. }
Dec 30 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.