0

the result of this program (process 9164) exited with code -1073741819. the program finished and nothing happens.....what should I do to make it run using only pointer without *x[] or x[][2]

#include <iostream>
#include<string>
#include <fstream>
using namespace std;
void sum(int **x) {
    int s = 0;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << *(*(x + i) + j) << " ";
            s += *(*(x + i) + j);
        }
        cout << "\n";
    }
    cout << s;
}
int main() {
    int x[3][2] = { {11,15},{19,28},{5,8} };
    sum((int**)x);
    return 0;
}
1
  • A C-style cast like (int**)x can be very dangerous. The compiler won't tell you that x is certainly not an int**. Your code never produces any pointer to pointer to int. Commented Oct 31, 2021 at 17:50

1 Answer 1

1

Whenever you feel the need to do a C-style cast (like you do with (int**)x) you should take that as a sign you're doing something wrong.

An array of arrays is not the same as a pointer to a pointer.

What happens is that you pass an invalid pointer, and the program will experience undefined behavior and crash.

What should happen is that the array x decay to a pointer to its first element, i.e. plain x is the same as &x[0]. That will be a pointer to an array, with the type int (*)[2] (in the case of x in your question). That is the type you need to use for the function argument:

void sum(int (*x)[2]) { ... }

And call like

sum(x);

You can also generalize to accept any kind of "2D" array, by using template arguments for the dimensions and passing the array by reference:

// For std::size_t
#include <cstdlib>

template<std::size_t A, std::size_t B>
void sum(int (&x)[A][B])
{
    // Use A and B as the sizes for the arrays...
    ...
}

While the above template version is better, the "proper" solution in C++ should be using std::array, as in:

std::array<std::array<int, 2>, 3> = { ... };

and then pass it as a constant reference to the function.

If the sizes aren't known at compile-time, or if the arrays should be dynamic, then use std::vector instead.

A good book and and good teacher would teach these classes before going into pointers or C-style arrays.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.