invalid use of array with unspecified bounds.. why? | | |
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**
} | | | | re: invalid use of array with unspecified bounds.. why?
Andrew Joros (ezze16@gmail.com) wrote:[color=blue]
> 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);[/color]
you can't use [][] to specify function parameters. using double **
will accomplish the same thing and compile. | | | | re: invalid use of array with unspecified bounds.. why?
"tedu" <tu@zeitbombe.org> writes:[color=blue]
> Andrew Joros (ezze16@gmail.com) wrote:[color=green]
>> 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);[/color]
>
> you can't use [][] to specify function parameters.[/color]
Right.
[color=blue]
> using double ** will accomplish the same thing and compile.[/color]
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) kst-u@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. | | | | re: invalid use of array with unspecified bounds.. why?
Andrew Joros (ezze16@gmail.com) wrote:[color=blue]
> I have no idea what the problem is...
> It is giving me this error: invalid use of array with unspecified
> bounds[/color]
[...]
[color=blue]
> 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**
>
> }[/color]
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. | | | | re: invalid use of array with unspecified bounds.. why?
"Simon Biber" <news@ralmin.cc> wrote in message
news:43603199$0$28782$afc38c87@news.optusnet.com.a u...[color=blue]
> Andrew Joros (ezze16@gmail.com) wrote:[color=green]
>> I have no idea what the problem is...[/color][/color]
<SNIP>>[color=blue]
> 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.[/color]
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 | | | | re: invalid use of array with unspecified bounds.. why?
In article <1130370608.652046.219160@g49g2000cwa.googlegroups .com>,
"Andrew Joros (ezze16@gmail.com)" <ezze16@gmail.com> wrote:
[color=blue]
> // Prototype
> void copy_arr(double a[][], double b[][], int size, int size2);
>}[/color]
You may have other problems, but size and size2 are never assigned.
leo
--
<http://web0.greatbasin.net/~leo/> | | | | re: invalid use of array with unspecified bounds.. why?
Dick de Boer wrote:[color=blue]
> "Simon Biber" <news@ralmin.cc> wrote in message
> news:43603199$0$28782$afc38c87@news.optusnet.com.a u...
>[color=green]
>>Andrew Joros (ezze16@gmail.com) wrote:
>>[color=darkred]
>>>I have no idea what the problem is...[/color][/color]
>
> <SNIP>>
>[color=green]
>>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.[/color]
>
>
> 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:[/color]
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.
[color=blue]
>
> void copy_arr(int size, int size2, double a[][5], double b[][5])
> {....[/color]
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. | | | | re: invalid use of array with unspecified bounds.. why?
Leonard Blaisdell <leo@greatbasin.com> writes:[color=blue]
> In article <1130370608.652046.219160@g49g2000cwa.googlegroups .com>,
> "Andrew Joros (ezze16@gmail.com)" <ezze16@gmail.com> wrote:
>[color=green]
>> // Prototype
>> void copy_arr(double a[][], double b[][], int size, int size2);
>>}[/color]
>
> You may have other problems, but size and size2 are never assigned.[/color]
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) kst-u@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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|