Board index » cppbuilder » Creating an array in a function

Creating an array in a function


2004-07-25 08:56:29 AM
cppbuilder14
Hi,
I have an application that creates an array in main() using a const
arraySize. I want to pass the array and arraySize to a function that
will create a two-subscripted array using the arraySize passed to the
function. Every method I've tried results in a message saying a constant
must be used in the array declaration.
I was able to get the program running using a global const arraySize
so I know the program works, but not with the passed arraySize.
Please help
-Jim
 
 

Re:Creating an array in a function

XXXX@XXXXX.COM wrote:
Quote
I have an application that creates an array in main() using a const
arraySize. I want to pass the array and arraySize to a function that
will create a two-subscripted array using the arraySize passed to the
function. Every method I've tried results in a message saying a constant
must be used in the array declaration.
Sorry, can't do it. You have to do the math yourself.
If you want an array[y][x] then you have to allocate an array[y*x] as
in
char *array = (char*) malloc( y * x * sizeof(char) );
And access the items instead of array[J][K], as array[ (J*x) + K ]
 

Re:Creating an array in a function

XXXX@XXXXX.COM writes:
Quote
I have an application that creates an array in main() using a const
arraySize. I want to pass the array and arraySize to a function that
will create a two-subscripted array using the arraySize passed to the
function. Every method I've tried results in a message saying a constant
must be used in the array declaration.
Can you be a little more specific?
What does the array definition in main() look like (C++ code).
How do you want to use the array in the function that is passed the array?
 

{smallsort}