Error When Passing String array to function in C++

Nico A.L

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?

Chris

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Linux

Error when passing variables to C function

From Dev

Error when passing struct to function in c

From Dev

C++ incomplete type error when passing class as function parameter

From Dev

Passing a string to function but expecting an array

From Dev

Passing a String Array to a Function

From Dev

Forcing an array size in a function parameter in C when passing an array

From Dev

why does passing C string into function as char* cause runtime error (but passing C string defined as char[] is ok)?

From Dev

Error when passing a 2-dim array as function's parameter in C

From Dev

Error in passing and modifying a array to a function

From Dev

Type mismatch error passing a string to a function from an array (in VBA)

From Dev

Error when passing string through

From Dev

PHP error when passing an array

From Dev

Passing a pointer (string) to a C function

From Dev

Error when passing function as an argument in C++

From Dev

A weird error when passing a struct pointer to a function in C

From Dev

Passing an array to function error?

From Dev

error C2664 when passing derived object into constructor function

From Dev

Error when passing multidimensional char array to a void function in C

From Dev

Error 13 when passing an array to a function that should fill it

From Dev

ArgumentError when passing ctypes array to C function

From Dev

Problem when passing multidimensional array to a function in C

From Dev

Error in passing the address of a pointer holding array elements to a function in c++

From Dev

Conflicting Types Error when Passing 2D Array to Function in C

From Dev

Passing a string array to another function

From Dev

Passing input string to a function in C

From Dev

Error when passing 2D array to function in c

From Dev

error: expected expression before ']' token when passing an array as an arguement in C

From Dev

Typescript error when passing const array to generic function

From Dev

How to avoid warning array to string converstion in php when you passing param as string or array to function?

Related Related

  1. 1

    Error when passing variables to C function

  2. 2

    Error when passing struct to function in c

  3. 3

    C++ incomplete type error when passing class as function parameter

  4. 4

    Passing a string to function but expecting an array

  5. 5

    Passing a String Array to a Function

  6. 6

    Forcing an array size in a function parameter in C when passing an array

  7. 7

    why does passing C string into function as char* cause runtime error (but passing C string defined as char[] is ok)?

  8. 8

    Error when passing a 2-dim array as function's parameter in C

  9. 9

    Error in passing and modifying a array to a function

  10. 10

    Type mismatch error passing a string to a function from an array (in VBA)

  11. 11

    Error when passing string through

  12. 12

    PHP error when passing an array

  13. 13

    Passing a pointer (string) to a C function

  14. 14

    Error when passing function as an argument in C++

  15. 15

    A weird error when passing a struct pointer to a function in C

  16. 16

    Passing an array to function error?

  17. 17

    error C2664 when passing derived object into constructor function

  18. 18

    Error when passing multidimensional char array to a void function in C

  19. 19

    Error 13 when passing an array to a function that should fill it

  20. 20

    ArgumentError when passing ctypes array to C function

  21. 21

    Problem when passing multidimensional array to a function in C

  22. 22

    Error in passing the address of a pointer holding array elements to a function in c++

  23. 23

    Conflicting Types Error when Passing 2D Array to Function in C

  24. 24

    Passing a string array to another function

  25. 25

    Passing input string to a function in C

  26. 26

    Error when passing 2D array to function in c

  27. 27

    error: expected expression before ']' token when passing an array as an arguement in C

  28. 28

    Typescript error when passing const array to generic function

  29. 29

    How to avoid warning array to string converstion in php when you passing param as string or array to function?

HotTag

Archive