# Omitting rows numbers of a matrix

Discussion in 'C Programming' started by /* frank */, Jun 25, 2004.

1. ### /* frank */Guest

Why I can omit numbers of rows, when I define
a two-dimensions array i.e. :

double table [] [100];

thanks

/* frank */, Jun 25, 2004

2. ### Eric SosmanGuest

/* frank */ wrote:
> Why I can omit numbers of rows, when I define
> a two-dimensions array i.e. :
>
> double table [] [100];

First, let's simplify to a one-dimensional array. There
are three principal contexts where you can omit the element
count:

- When the array is defined and an initializer is
provided, the compiler deduces the array size from
the number of initializer values:

double vector[] = { 1, 2, 3, 27 };

has the same effect as

double vector[4] = { 1, 2, 3, 27 };

- When you write a declaration of an array that "lives
elsewhere," you can omit the array size:

extern double vector[];

gives the compiler all the information it needs to
generate code to access the elements of this array;
it does not need to know how many elements exist.

- When you write an array declaration in a function
parameter list, it is automatically transformed into
a declaration of a pointer to the array's first element.
Such a pointer carries no information about the number
of elements in the array, and since the count would be
"thrown away" anyhow, you needn't provide it. These
three function definitions have the same meaning:

int f(double p[4]) { ... }
int f(double p[]) { ... }
int f(double *p) { ... }

If any of this is confusing, I suggest you review Section 6
in the comp.lang.c Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/s6.html

All the above concerns one-dimensional arrays, but your
original question was about two-dimensional arrays. Here's
the connection: C doesn't actually support multi-dimensional
arrays at all! When we speak of a "two-dimensional array" in
C, what we actually mean is a one-dimensional array whose
individual elements are themselves one-dimensional arrays.
We can show this more explicitly with a typedef:

typedef double Row[100];
Row table[];

The second line says "`table' is an array containing an unknown
number of `Row' elements." The first line says "A `Row' is an
array of 100 `double' elements." Together, the two lines say
exactly the same thing as `double table[][100];'.

You may wonder why we can't write `double table[][];' for
an array of unknown size whose elements are arrays of unknown
size, or why we can't write `typedef double Row[];' in the
explicit reformulation. The reason is that the compiler needs
complete information about the type of an array's elements, even
though it doesn't (usually) need to know exactly how many elements
there are. And the problem in the "two-dimensional" case is that
"complete information" about an array's type includes the element
count. Think about it: The compiler needs to know how many bytes
to skip over when going from `table[0]' to `table[1]', which means
it needs to know the `sizeof' the element -- and if the element is
itself an array, that means the compiler needs to know how many
elements the sub-array contains.

The upshot is that only the "leftmost" dimension of a "multi-
dimensional" array can be left unspecified (in the cases described
above), but the other dimensions must be specified fully.

--

Eric Sosman, Jun 25, 2004