Passing an array of unknown size to a function

Hello everyone,

I am trying to pass an array to a function as a parameter. Usually, when I pass an array whose size is known to me, I do the folowing:

1
2
3
4
5
6
7
//functions.cpp

void testf(int myArray[5])
{
  //function body
}




1
2
3
4
//functions.h

void testf(int myArray[5]);




1
2
3
4
5
6
7
8
9
10
11
//main.cpp

int main()
{

myArray[5] = {30, 30, 30, 30, 30};

testf(myArray);

  return 0;
}



In this case, my array's size is not known beforehand. The array's contents are intended to be used for mathematical calculations, but the number of values stored inside the array depend upon user input. So I want to pass the array as a parameter to a function without specifying it's size explicitly in my code. Here's a brief example of the kind of approach I attempted to utilize:

1
2
3
4
5
6
7
//functions.cpp

void testf(int n, int myArray[n])
{
  //function body
}





1
2
3
4
//functions.h

void testf(int n, int myArray[n]);





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//main.cpp

int main()
{

int n; 

printf("Input size of array:"); //prompt user for input
scanf("%d", &n);

myArray[n]; //declare the array

testf(n, myArray); //call the function

  return 0;
}



As a result, I am getting compiler errors of "this variable was not declared in this scope" for almost every variable in my program. I conclude that this is not the correct way to pass an array of unknown size to a function, but am unsure of what I do wrong. Any feedback or comments on how to approach this would be most appreciated.

Thank you in advance for any replies. I appreciate any feedback.

Regards,

Okaya



Last edited on
I suggest that you use the std::vector or std::deque. This article suggests some examples on how to do this using std sequence containers instead of a c-array.
http://cplusplus.com/forum/articles/20881/
Thank you kempofighter, this is a very interesting article. I intend to study it in greater detail.

I had tried something similar to your "method 1" for passing an array to a function before starting this topic. What I did was:

1
2
3
4
5
6
7
//functions.cpp

void testf(int n, int myArray[])
{
  //function body
}



1
2
3
4
//functions.h

void testf(int n, int myArray[]);



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//main.cpp

int main()
{

int n; 

printf("Input size of array:"); //prompt user for input
scanf("%d", &n);

myArray[n]; //declare the array

testf(n, myArray); //call the function

  return 0;
}



Essentially, the difference with my initial post's version is that I removed the explicit array size in the function's declaration, passing it only indirectly as a second parameter. The result was that my code compiled fine, but the program crashes once the function is called.


I also tried the following variant after reading your article:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//main.cpp

int main()
{

int n2; 

printf("Input size of array:"); //prompt user for input
scanf("%d", &n2);

const int n(n2);

myArray[n]; //declare the array

testf(n, myArray); //call the function

  return 0;
}


So I get the input, then define a const int, and then I declare an array with that size. Again, the code compiles, but the program crashes upon the function call.

Have I misunderstood or misapplied "method 1" of your article? Any feedback is most appreciated.

As it is, my program uses several arrays and passes them into numerous functions for performing mathematical computations with them (it's an astronomy-related program), but the arrays themselves almost never have a size greater than 10, so I think that even your "method 1" might be applicable for the immediate requirements of this program. At the same time, I am looking forward to study your provided alternatives to C-Arrays in greater detail. I will probably redesign my whole program based on std::vector or std::deque in the immediate future.

Many thanks for any feedback. Regards,

Okaya
Last edited on
Hello all,

I would like to note here that I actually rewrote the part of this program mentioned here, reimplementing the "method 1" found in the article kempofighter mentioned earlier, and the compiled program no longer causes any problems. So the problem referred to in my previous post must have been due to a misimplementation that got cleared away by my rewriting this part of the code.

Thank you again for your help, kempofighter. I will probably be changing my program to using std::vector or std::deque in the near future.

Regards,

Okaya
Last edited on
Make a class wich contains the array and its size? :3
Okaya could you please look at my question in the disscusion board and tell me what is wrong with my programs

I really appreciate it :-)) thanks


It is called "need a hand please"
Topic archived. No new replies allowed.