473,395 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Multidimensional arrays as parameters

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
4 3391
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
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
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
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

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

Similar topics

5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
0
by: Wihlelm Bierbaum | last post by:
I'm trying to make use of a certain method of a COM object that takes a multidimensional array as its sole parameter. Here's an example of the construction of said array: $cA = array(); $cA =...
2
by: Terry | last post by:
Hi, can someone plz tell me how multidimensional arrays (like a 2-D array) are stored in memory? Are they like single dimensional arrays? Stored sequentially in one "row", so to say? Thanks ...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
3
by: Claire | last post by:
I have a multidimensional array defined as private double myArray = new double; The first column of the array contains X values, the other contains Y values I have a charting function defined as...
3
by: Ravi Singh (UCSD) | last post by:
Hello all I am trying to use jagged and multi-dimensional arrays in C++. In C# these work fine // for jagged arrays string jaggedArray = new string ; //for multidimensional arrays string...
3
by: matthewburton | last post by:
Hi everyone, I'm trying to write a program that will search a body of text and replace certain words (say, A, B, and C) with different words that I think are more appropriate (A1, B1 and C1). I...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.