invalid use of array with unspecified bounds.. why?

  • Thread starter Andrew Joros (ezze16
  • Start date
A

Andrew Joros (ezze16

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[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[j] = a[j]; <== error is here**

}
 
T

tedu

Andrew said:
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.
 
K

Keith Thompson

tedu said:
Andrew said:
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.
 
S

Simon Biber

Andrew said:
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[j] = a[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.
 
D

Dick de Boer

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
 
S

Simon Biber

Dick said:
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.
 
K

Keith Thompson

Leonard Blaisdell said:
Andrew Joros ([email protected]) said:
// 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top