user4081530
user4081530

Reputation: 1

Pass multidimensional array to function accepting char*[]

How do I do this? I get a segmentation fault.

void pass_array(char* arr[])
{
    cout << arr[0] << " " << arr[1] << "\n";
}

int main()
{
    #define NUM_ELEMENTS 10
    #define CHAR_LEN 32
    char arr[NUM_ELEMENTS][CHAR_LEN];
    cin >> arr[0];
    cin >> arr[1];
    cout << arr[0] << " " << arr[1] << "\n";
    pass_array((char**) arr);
}

I want to do this without dynamic memory. Is it possible?

I know how many elements there are in arr, but the function signature must not change.

Upvotes: 0

Views: 77

Answers (4)

stefafafan
stefafafan

Reputation: 1

If you are willing to use C++11 or newer versions, this should work, std::arrays are more preferable than c arrays.

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

constexpr auto numElements = 10;

auto passArray(std::array<std::string, numElements> arr) -> void
{
    std::cout << arr[0] << " " << arr[1] << std::endl;
}

auto main() -> int
{
    std::array<std::string, numElements> arr;
    std::cin >> arr[0];
    std::cin >> arr[1];
    std::cout << arr[0] << " " << arr[1] << std::endl;
    passArray(arr);
    return 0;
}

Upvotes: 0

rcgldr
rcgldr

Reputation: 28826

It's not clear if you want to pass a true multidimensional array, such as a matrix, or if you want to pass an array of pointers instead.

The alternate syntax for the matrix

char arr[number_of_rows][number_of_colums]

is

char (*arr)[number_of_columns]

As a simple example of a pre-initialized array of pointers:

char *arr[5] = {"one", "two", "three", "four", "five"};

Upvotes: 0

Richard Chambers
Richard Chambers

Reputation: 17593

You will need to do something like the following source.

void pass_array(char* arr[])
{
    std::cout << arr[0] << " " << arr[1] << "\n";
}

int main(int argc, _TCHAR* argv[])
{
    #define NUM_ELEMENTS 10
    #define CHAR_LEN 32
    char arr[NUM_ELEMENTS][CHAR_LEN];
    char *arr2[NUM_ELEMENTS];

    std::cin >> arr[0]; std::cin.ignore();    // read the first string, discarding CR
    std::cin >> arr[1];                       // read the second string
    std::cout << arr[0] << " " << arr[1] << "\n";
    arr2[0] = arr[0];   // put address of first string into your argument array
    arr2[1] = arr[1];   // put address of second string into your argument array
    pass_array(arr2);
}

Upvotes: 0

M.M
M.M

Reputation: 141618

This is not possible. Arrays and pointers are different. The line char arr[NUM_ELEMENTS][CHAR_LEN]; defines a contiguous bloc of characters; however the function is expecting a list of pointers. There are no pointers in arr.

You should get compiler errors when compiling this code. Using a cast to suppress the errors is telling the compiler "Ssh, I know what I'm doing". However you don't know what you are doing.

To do this without changing the function signature you will have to build a table of pointers, e.g.:

char *ptrs[NUM_ELEMENTS];

for (size_t i = 0; i != NUM_ELEMENTS; ++i)
    ptrs[i] = arr[i];

pass_array(ptrs);

Upvotes: 2

Related Questions