0

Hi guys I've some errors when passing some strings of array in C++, do you know what's wrong with this code guys?, Thankyou

#include <iostream>
#include <string>

void showData(string data[]);

int main()
{

    string namaMahasiswa[] = {"Nico", "Yonathan", "Andre", "Ratu"};

    enum option{ SHOW = 5 };

    switch (5)
    {
    case SHOW:
        showData(namaMahasiswa);
        break;
    }
}

void showData(string data[])
{
    for (int z = 0; z < sizeof(data) / sizeof(*data); z++)
    {
        cout << data[z] << endl;
    }
}

This is the error :

'int main()':
index.cpp:61:18: error: could not convert '(std::string*)(& namaMahasiswa)' from 'std::string*' {aka 'std::__cxx11::basic_string<char>*'} to 'std::string' {aka 'std::__cxx11::basic_string<char>'}
   61 |         showData(namaMahasiswa);
      |                  ^~~~~~~~~~~~~
      |                  |
      |                  std::string* {aka std::__cxx11::basic_string<char>*}

In function 'void showData(std::string*)':
index.cpp:83:36: warning: 'sizeof' on array function parameter 'data' will return size of 'std::string*' {aka 'std::__cxx11::basic_string<char>*'} [-Wsizeof-array-argument]
   83 |     for (int z = 0; z < sizeof(data) / sizeof(*data); z++)

index.cpp:80:22: note: declared here
   80 | void showData(string data[])

So it means we can't pass string array to a function like that or maybe I've to use some char?

Nico A.L
  • 11
  • 3
  • 3
    What is `sizeof(data) / sizeof(*data)` supposed to accomplish? – Scott Hunter Feb 05 '22 at 16:28
  • It means like data.length, or to count the length from the array – Nico A.L Feb 05 '22 at 16:31
  • 1
    Can't reproduce your error once I add the appropriate namespace. And see https://stackoverflow.com/questions/5493281/c-sizeof-a-passed-array, since your size trick won't work. – Stephen Newell Feb 05 '22 at 16:40
  • The code you have posted is clearly not the code generating the error message. Form a [mre] and post it together with the error message it produces, both _verbatim_. – user17732522 Feb 05 '22 at 17:12
  • @NicoA.L. A `string[]` parameter is actually a decayed `string*` pointer. [You can't use the `sizeof` trick on such a parameter to get its length](https://stackoverflow.com/questions/9443957/). You will have to pass the array's length as a separate parameter. Or pass the array by reference instead of pointer. Or use `std::vector` instead. – Remy Lebeau Feb 05 '22 at 18:15
  • Does this answer your question? [When passing an array to a function in C++, why won't sizeof() work the same as in the main function?](https://stackoverflow.com/questions/36525798/when-passing-an-array-to-a-function-in-c-why-wont-sizeof-work-the-same-as) – Etienne de Martel Feb 05 '22 at 19:11

2 Answers2

0

You have to pass it in as

void showData(string* data, size_t len);

but since you are using c++ its better to use something like vector instead of C arrays. A different approach would be to use a template to deduce the size like this:

template<size_t S>
void showData(string (&data)[S]);

this gives you the size of the array as S

ACB
  • 1,607
  • 11
  • 31
  • Okee Thankyou @ACB I'll try it, but how about when I call the function like this `showData(namaMahasiswa);` is that correct? – Nico A.L Feb 05 '22 at 16:39
  • for that you can either use the template version or use a vector instead. The pointer version you have to call as `showData(namaMahasiswa, sizeof(namaMahasiswa));` – ACB Feb 05 '22 at 16:52
  • Okay thanks @ACB – Nico A.L Feb 05 '22 at 17:02
0

As an alternative to the answer provided by @ACB, you can use a std::array.

#include <array>
#include <string>
#include <iostream>

template <std::size_t S>
void foo(std::array<std::string, S> &bar) {
    for (auto &i : bar) {
        std::cout << i << std::endl;
    }
}

int main() {
    std::array<std::string, 3> baz = {"hello", "world", "wooble"};

    foo(baz);

    return 0;
}

We could even use the template to allow for std::arrays of other types. As long as there is an applicable << overload with std::ostream and T.

#include <array>
#include <string>
#include <iostream>

template <typename T, std::size_t S>
void foo(std::array<T, S> &bar) {
    for (auto &i : bar) {
        std::cout << i << std::endl;
    }
}

int main() {
    std::array<std::string, 3> baz = {"hello", "world", "wooble"};

    foo(baz);

    return 0;
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • `std::array baz = {"hello", "world", "wooble"};` that code means the array only has 3 items right?, how about if we don't know the array length? should I write `std::array baz = {}` , because the array length depending from user input – Nico A.L Feb 05 '22 at 17:13
  • With `std::array` you _must_ know the size at compiletime. If you will not know at compile time, use a `std::vector`. – Chris Feb 05 '22 at 17:15
  • Okay Thanks @Chris – Nico A.L Feb 06 '22 at 04:04