Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing const values to functions for array subscripts. 1

Status
Not open for further replies.

0x4B47

Programmer
Jun 22, 2003
60
0
0
US
OK fellow coders, I can't work this one out. I'm passing a const value to a function and using that const value to declare the array size inside a function.
Code:
void foo( int a[], const int columns );

int main()
{
	const int SIZE = 10;
	int array[SIZE] = {0};
	foo( array, SIZE );
	return 0;
}

void foo( int a[], const int columns )
{
	const int rows = 10;
	int b[rows][columns] = { 0 }; //Line 17
}

here are the errors i get:
.cpp(17) : error C2057: expected constant expression
.cpp(17) : error C2466: cannot allocate an array of constant size 0
.cpp(17) : error C2087: '<Unknown>' : missing subscript

I will be very grateful if someone can help.
Thanks
Kunal.
 
columns is not a compile time constant (unlike rows)

> void foo( int a[], const int columns )
The const in this context just means that the function will not modify the parameter (say columns++) without generating an error message from the compiler.

I think the latest C++ standard permits variable length arrays such as this, but if you're using an older compiler / older standard, then you have to use dynamic allocation to achieve the same result.

--
 
Salem,
Thanks for the prompt reply. I'm using VC++ 6.0, is that too old? Anyway, i need the columns part of the array to be the same size as SIZE declared in main. How can I achieve this, as the compiler expects a constant for the array size?

Kunal.
 
Salem is right. The best thing is to do dynamic memory allocation. Try looking up 'malloc()' in your help files.
:)

Don't hate the player - hate the game
 
I dont know how annoying I'm beginning to get (?!?!?!) :) but as I'm writing a C++ application I'd rather stay away from the C malloc() function. How about if you guys showed me how to do it with the 'new' operator? Am I ok in suggesting that I can do this?
Code:
void fu( int a[], const int columns )
{
    const int rows = 10;
    int b = new int[rows][columns];
}

Sorry I would give it a try myself but I'm at my uncles and I dont have a compiler installed here! Actually the other day I was thinking of installing a compiler at all my relatives places, that way I can code wherever I want! :)

Thanks guys.
 
Actually, I don't think you can do that. I'd suggest using a vector. In my opinion it is easier to manage than a statically or dynamically allocated array.

To do it without a vector but with a dynamic array, you would do something like this:
Code:
void foo( int a[], const int columns );

int main()
{
    const int SIZE = 10;
    int array[SIZE] = {0};
    foo( array, SIZE );
    return 0;
}

void foo( int a[], const int columns )
{
    const int rows = 10;
    int* b[rows] = { 0 };
    for (int i = 0; i < rows; i++)
        b[i] = new int[columns];

    // ...

    for (int j = 0; j < rows; j++)
        delete [] b[j];
}
Note that I left the first dimension as a static array because rows is a constant. You could make that dynamic also, just remember to use delete[] after you delete each row.
 
Well you could always make the SIZE in main a global const.
Code:
void foo( int a[] );
const int SIZE = 10;

int main()
{
    int array[SIZE] = {0};
    foo( array );
    return 0;
}

void foo( int a[] )
{
    int b[SIZE][SIZE] = { 0 }; //Line 17
}
At least now you only have to change the const in one place instead of two (which is a good thing).

--
 
Swap columns and rows:


#define SIZE 10
#define ROWS 10

void foo( int a[], int columns )
{
int (*b)[ROWS];
b = new int [columns][ROWS];

for(int row=0; row<ROWS; row++)
for(int column=0; column<columns; column++)
b[column][row]=a[column];
}

int main()
{
int array[SIZE] = {0};
foo( array, SIZE );
return 0;
}
 
Thanks guys, you really are majic!

Kunal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top