Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

two dimensional array 2

Status
Not open for further replies.

rvy2k

Programmer
Jun 29, 2001
39
0
0
US
hi guys!

i was wondering if somebody could paste the code of a function that prints out all the values of a 2dimensional array. i keep getting a missing subscript compilation error.

the equivalent for a one dimensional array would be:

/* code starts here */
void print_array(int array [], int len) {
int n;
for(n=0;n<len;n++) {
printf(&quot;%i &quot;,array[n]);
}
printf(&quot;\n&quot;);
}
/* code ends here */

have a good thanksgiving!
 
You might want to check over my syntax. I'm a little rusty with C, it's been a while. I think you have to specify one of the dimensions of the array but like I said you should check on the syntax of passing a 2D array to a function. I do know for sure that you'll need 2 loops, one nested in the other - one will count your rows and the other your columns.

void print_array(int array[][], int len)
{
int n;
for(x=0;x<len;x++)
{
for(y=0;y<len;y++)
{
printf(&quot;%i&quot;,array[x][y]);
}
}
printf(&quot;\n&quot;);
}
 
thanks Mithrilhall, but it didnot work ...
I still get the missing subscript error

--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.c
E:\Documents and Settings\hroussel\Desktop\test.c(4) : error C2087: '<Unknown>' : missing subscript
Error executing cl.exe.

test.obj - 1 error(s), 0 warning(s)

#include <stdio.h>
#include <stdlib.h>

void print_array(int array[][], int len)
{
int x,y;
int n;
for(x=0;x<len;x++)
{
for(y=0;y<len;y++)
{
printf(&quot;%i&quot;,array[x][y]);
}
}
printf(&quot;\n&quot;);
}
 
Hello! Your friendly neighborhood ASM programmer here, without anything useful to do!

Try treating your TWO dimensional array as an array of POINTERS. Remember that in C, arrays are passed as pointers. Also, a two-dimensional array might be treated as an array of an array - it might work that way...

Alternatively, just pass it as if it were a one-dimensional array, and do the following:
void print_array(int *the_array, int lenx, int leny)
{
int x,y;
for(x=0;x<lenx;x++)
for(y=0;y<leny;y++)
printf(&quot;%i&quot;,the_array[y+x*leny]);
}
/*note: I can't remember exactly how C organizes two-dim arrays, if it's column-first or row-first... you'll have to massage the code section a bit, maybe. Also, chances are you'll need to put a (int*) the_array to type-cast the array.*/


&quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
#include <stdio.h>
#include <stdlib.h>
/*
-------------------------------------
|
V */
void print_array(int array[][10], int len)
{
int x,y;
int n;

for(x=0;x<len;x++)
{
for(y=0;y<len;y++)
{
printf(&quot;%i&quot;,array[x][y]);
}
}
printf(&quot;\n&quot;);
}

With some C compilers, the second array subscribe must be supplied or errors will occur. This must be declared when you define the function protoype and the actual function. If you don't know the column parameter (second subscript) then you should pass the array as a pointer.
 
hi, I think this should work find;

void main ()
{
int arr[4][5], row = 4, col = 5;
....
print_array (arr, row, col);
}

void print_array(int arr[][5], int row, int col)
{
for (int x = 0; x < row; x++){
for (int y = 0; y < col; y++)
printf (&quot;%d &quot;, arr[x][y]);
puts (&quot;&quot;);
}
} Andrew Lim

Trying and exploring is adventuring
 
Hello! Your friendly neighborhood Asm programmer here...

That's assuming you KNOW the value of the second dimension at COMPILE TIME. If you need to make it very generic (possibly to output multiple tables of different dimensions) you'll still need to use my code... &quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top