Originally Posted by
CommonTater
The compiler accepts it, but from what I've seen the value in the [] brackets is ignored... I could be wrong (happens sometimes).
No, you're right, at least for 1-d arrays. Like any array in a function definition, the degradation to a pointer makes the the size in the leftmost set of [ ] unnecessary (the other sizes are needed to compute the space between, e.g. &arr[0][0] and &array[1][0]). For a 1-d, there's only 1 set of [ ] and it doesn't need a size. For multi-dimensional arrays however, the variable size thing works, AFAIK. Here's a sample 2-d array program to test:
Code:
#include <stdio.h>
void print(int size1, int size2, int x[size1][size2])
{
int i, j;
for (i = 0; i < size1; i++) {
for (j = 0; j < size2; j++) {
printf("%4d ", x[i][j]);
}
putchar('\n');
}
}
int sum(int size1, int size2, int x[size1][size2])
{
int i, j, sum;
sum = 0;
for (i = 0; i < size1; i++) {
for (j = 0; j < size2; j++) {
sum += x[i][j];
}
}
return sum;
}
int main(void)
{
int size1 = 3;
int size2 = 5;
int array[3][5] = {
{ 1, 2, 3, 4, 5},
{ 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
print(size1, size2, array);
printf("Sum: %d\n", sum(size1, size2, array));
return 0;
}