0

I have been trying to pass an array to another function with the hopes of iterating through it based on size to no avail.. Here's what I have been attempting and what the output is:

void check_array(std::string str_arr[])
{
  // print statistics
  std::cout << "# of bytes in object: " << sizeof(str_arr) << std::endl;
  std::cout << "# of bytes in element: " << sizeof(str_arr[0]) <<std::endl;
  std::cout << "Object address: " << &str_arr << std::endl;
}

int main()
{
  // initalize stuff
  std::string str_arr[2];

  str_arr[0] = "First element";
  str_arr[1] = "Second element";

  // print statistics
  std::cout << "# of bytes in object: " << sizeof(str_arr) << std::endl;
  std::cout << "# of bytes in element: " << sizeof(str_arr[0]) <<std::endl;
  std::cout << "Object address: " << &str_arr << std::endl;

  // pass it on so the other method can see
  check_array(str_arr);
}

The output is:

"# of bytes in object: 16" "# of bytes in element: 8" "Object address "

"# of bytes in object: 8" "# of bytes in object: 8" "Object address "

How do I pass this array to a function and get the same numbers for 'bytes in object' and more importantly, the same address? I've tired passing by reference like I would with a normal pod type but I get compile errors..

Thanks!

Walklikeapenguin
  • 127
  • 2
  • 10
  • 1
    You can’t. The array turns into a pointer and no size information is included. You need to use a container that holds the size also if you need it, or give the size separately – Sami Kuhmonen Oct 11 '19 at 18:16
  • Possible duplicate of [Passing an array by reference to template function in c++](https://stackoverflow.com/questions/20163469/passing-an-array-by-reference-to-template-function-in-c) – hegel5000 Oct 11 '19 at 18:18
  • 2
    Use a std::vector instead of an std::string[] – Jorge Omar Medra Oct 11 '19 at 18:19
  • 1
    For a fixed size array, use `std::array`. For a dynamic array, use `std::vector`. – super Oct 11 '19 at 18:20

2 Answers2

0

There are a lot of problems with the code as-is. First, arrays without a size specifier (i.e. string[]) are not legal C++. What your compiler is likely doing is decaying this to a pointer, since arrays decay to pointers in many other contexts. You have a couple of options here.

  1. Instead of using an array, you can use std::array and pass it via reference (i.e. std::array<std::string, 2>& str_arr as the parameter).
  2. Instead of taking a string[], you want to take a string* and a length.
  3. If you absolutely want to pass an array with a compile-time fixed size, and want to do it via reference, there is a way but the syntax is a little strange:

    void check_array(std::string (&str_arr)[2]) { ... }

Chuu
  • 4,301
  • 2
  • 28
  • 54
0

Arrays in C++ are passed via pointer/references - so you can do something like this :

// pass it as a pointer
check_array(str_arr, 2);

void check_array(std::string *str_arr, int length){...}

or 
// pass it as a reference
check_array(str_arr[0], 2);

void check_array(std::string &str_arr, int length){...}