0

How can you find the length of an argument list inside a function?

If you have a list passed into a function, how do you find the length of it? I've found that inside the function the argument turns into just the pointer for the 0th element in the list so only has a "size" of the zeroth element. Thanks! Update, thanks for the helpful answers

21st Aug 2017, 8:26 PM
Richard Finlay Tweed
Richard Finlay Tweed - avatar
3 Respuestas
+ 1
You use a second variable and pass the size along together with the list/array whatever. Sounds annoying, right? C++ standard library to the rescue!!! Instead of using silly C style arrays you can use std::vectors, std::arrays and a whole bunch more which keep track of their own size and alot more stuff!! Click here to find out more! http://en.cppreference.com/w/cpp/container
21st Aug 2017, 9:47 PM
Dennis
Dennis - avatar
+ 1
Also you can use templates like so: #include <iostream> template<typename T, std::size_t size> void someFunction(T(&arr)[size]) { for(std::size_t i = 0; i < size; ++i) { std::cout << arr[i] << " "; } std::cout << std::endl; } int main() { int arr[] = {4,5,6}; unsigned arr2[] = {6,2,3,1,5}; std::string arr3[] = {"abc", "def", "ghi"}; someFunction(arr); someFunction(arr2); someFunction(arr3); }
21st Aug 2017, 9:50 PM
Dennis
Dennis - avatar
0
Are you asking about the size of STL <list> container or "..." function argument? In 1st case the iterator might have been passing.
21st Aug 2017, 9:50 PM
Super User
Super User - avatar