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

invalid use of array with unspecified bounds.. why?

P: n/a
I have no idea what the problem is...
It is giving me this error: invalid use of array with unspecified
bounds

How can I fix it. Thanks.
#include <stdio.h>

// Prototype
void copy_arr(double a[][], double b[][], int size, int size2);

int main()
{
double source[5][5] = {
{1.1, 1.2, 1.3, 1.4, 1.5},
{2.1, 2.2, 2.3, 2.4, 2.6},
{3.1, 3.2, 3.3, 3.4, 3.5},
{4.1, 4.2, 4.3, 4.4, 4.5},
{5.1, 5.2, 5.3, 5.4, 5.5}
};

double target3[5][5];
int i;
int j;
copy_arr(source, target3, 5, 5);

printf("Array Copy\n");

for (i=0; i < 5; i++)
{
printf("\n");
for (j=0; j < 5; j++)
{
printf("%2.1f ", target3[i][j]);

}

}

printf("\n\n");
system("pause");
return 0;
}

void copy_arr(double a[][], double b[][], int size, int size2)
{
int i, j;
for (i = 0; i < size; i++)
for (j = 0; j < size2; j++)
**error is here ==> b[i][j] = a[i][j]; <== error is here**

}

Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Andrew Joros (ez****@gmail.com) wrote:
I have no idea what the problem is...
It is giving me this error: invalid use of array with unspecified
bounds
void copy_arr(double a[][], double b[][], int size, int size2);


you can't use [][] to specify function parameters. using double **
will accomplish the same thing and compile.

Nov 15 '05 #2

P: n/a
"tedu" <tu@zeitbombe.org> writes:
Andrew Joros (ez****@gmail.com) wrote:
I have no idea what the problem is...
It is giving me this error: invalid use of array with unspecified
bounds
void copy_arr(double a[][], double b[][], int size, int size2);
you can't use [][] to specify function parameters.


Right.
using double ** will accomplish the same thing and compile.


No, it won't, at least not without changing the call as well as
the function.

Here's a snippet of the OP's code:

void copy_arr(double a[][], double b[][], int size, int size2);

int main()
{
double source[5][5] = {
[...]
};

double target3[5][5];
[...]
copy_arr(source, target3, 5, 5);
[...]

If you change the parameters to double**, the arguments have to be
pointers to pointers to double (possibly something that decays to that
type). source is a 2-dimensional array; the name decays to a pointer
to array of 5 doubles, not to a pointer to pointer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #3

P: n/a
Andrew Joros (ez****@gmail.com) wrote:
I have no idea what the problem is...
It is giving me this error: invalid use of array with unspecified
bounds
[...]
void copy_arr(double a[][], double b[][], int size, int size2)
{
int i, j;
for (i = 0; i < size; i++)
for (j = 0; j < size2; j++)
**error is here ==> b[i][j] = a[i][j]; <== error is here**

}


The compiler needs to know how big each element of the array is. Try:

void copy_arr(int size, int size2, double a[][size2], double b[][size2])
{
/* ... */
}

This will only work if your compiler supports variable-sized arrays,
which were added to the C standard in 1999.

Without variable-sized arrays, there is simply no way to pass an array
of arrays of arbitrary size to a function. You would need to change to a
different data structure, such as an array of pointers.

--
Simon.
Nov 15 '05 #4

P: n/a

"Simon Biber" <ne**@ralmin.cc> wrote in message
news:43***********************@news.optusnet.com.a u...
Andrew Joros (ez****@gmail.com) wrote:
I have no idea what the problem is...
<SNIP>> The compiler needs to know how big each element of the array is. Try:

void copy_arr(int size, int size2, double a[][size2], double b[][size2])
{
/* ... */
}

This will only work if your compiler supports variable-sized arrays, which
were added to the C standard in 1999.

Without variable-sized arrays, there is simply no way to pass an array of
arrays of arbitrary size to a function. You would need to change to a
different data structure, such as an array of pointers.


The standard says than an array is converted to a pointer, so it is possible
to pass an array to a function. You don't need variable sized arrays, but
you have to tell the compier how big the second dimension is:

void copy_arr(int size, int size2, double a[][5], double b[][5])
{....

wil work fine, even on older compilers. The arguments ar converted to type
double (*)[5]
as part of the ususal unary conversions, the type means 'a pointer to an
array of 5 doubles'

Now, the code will not produce a 'unspecified bounds error, becuase the size
of the object pointed to by the argument is known. (In the case of double
(*)[] it is not, accessing the second element (array) is not possible in
that case)
Ofcource, it is better to make the dimensions of the array a define, or make
a new type with a typedef.

DickB

Nov 15 '05 #5

P: n/a
In article <11**********************@g49g2000cwa.googlegroups .com>,
"Andrew Joros (ez****@gmail.com)" <ez****@gmail.com> wrote:
// Prototype
void copy_arr(double a[][], double b[][], int size, int size2);
}


You may have other problems, but size and size2 are never assigned.

leo

--
<http://web0.greatbasin.net/~leo/>
Nov 15 '05 #6

P: n/a
Dick de Boer wrote:
"Simon Biber" <ne**@ralmin.cc> wrote in message
news:43***********************@news.optusnet.com.a u...
Andrew Joros (ez****@gmail.com) wrote:
I have no idea what the problem is...

<SNIP>>
The compiler needs to know how big each element of the array is. Try:

void copy_arr(int size, int size2, double a[][size2], double b[][size2])
{
/* ... */
}

This will only work if your compiler supports variable-sized arrays, which
were added to the C standard in 1999.

Without variable-sized arrays, there is simply no way to pass an array of
arrays of arbitrary size to a function. You would need to change to a
different data structure, such as an array of pointers.

The standard says than an array is converted to a pointer, so it is possible
to pass an array to a function. You don't need variable sized arrays, but
you have to tell the compier how big the second dimension is:


If you read the OP's code, you'll see that he is trying to write a
general function that will work with any two-dimensional array, where
the size is given as a parameter of the function.

void copy_arr(int size, int size2, double a[][5], double b[][5])
{....


Of course this code will work fine on older compilers, but it will only
work with arrays where the second dimension is exactly five! There's no
point even having a size2 argument in your function, since it will
*only* compile when size2 is 5.

It's better not to produce a function that is not limited to work with
arrays of a particular dimension.

--
Simon.
Nov 15 '05 #7

P: n/a
Leonard Blaisdell <le*@greatbasin.com> writes:
In article <11**********************@g49g2000cwa.googlegroups .com>,
"Andrew Joros (ez****@gmail.com)" <ez****@gmail.com> wrote:
// Prototype
void copy_arr(double a[][], double b[][], int size, int size2);
}


You may have other problems, but size and size2 are never assigned.


Yes they are. size and size2 are arguments to the copy_arr function,
and the one call to it in the original code (which you snipped) passes
the values 5 and 5 as the corresponding arguments.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.