问题描述:

I'm sorry if I didn't pick a descriptive or concise name. A lot of questions sound similar, but I haven't been able to find what I'm looking for. What I want to do is store a 2D array of pointers somewhere and assign a variable in some object to that array to be able to access it.

Here's some example code that has the same compile error I'm getting with a bigger project.

#include <iostream>

using namespace std;

struct X{

float n, * b[8][8];

X(){

n = 1;

b[1][5] = &n;

cout << *(b[1][5]) << endl;

}

void Set(float * c[8][8]){

b = c;

cout << *(b[1][5]) << endl;

}

};

main(){

float m, * a[8][8];

m = 2;

a[1][5] = &m;

X obj;

obj.Set(a);

}

What I want to happen in this code is that an X object starts with its own 2D array, whose value pointed to by b[1][5] should be printed as "1". Then the main method's 2D array, a, is passed to the object's Set() method and assigned to its array variable. The value pointed to by b[1][5] should then be printed as "2".

However, I can't figure out what type the Set() parameter, c, should be. I get

error: incompatible types in assignment of ‘float* (*)[8]’ to ‘float* [8][8]’

when I try to compile. As for why I want to do this, I'm trying to use an array of pointers to objects, not floats, but it's the same error.

网友答案:

Try this:

#include <iostream>
using namespace std;
struct X{
    float n;
    float* (*b)[8];
    X(){
        n = 1;
        b = new float*[8][8];
        b[1][5] = &n;
        cout << *(b[1][5]) << endl;
    }
    void Set(float * c[8][8]){
        delete[] b;
        b = c;
        cout << *(b[1][5]) << endl;
    }
};
main(){
    float m, * a[8][8];
    m = 2;
    a[1][5] = &m;
    X obj;
    obj.Set(a);
}

Here, X stores a pointer to a 1D array, which we are treating as a pointer to the first element of a 2D array - i.e. as just a 2D array.

In X's constructor, X allocates its own array with new and sets its pointer to point to that. When calling Set(), X deletes its own array, and sets its pointer to point to the array provided by the caller.

The only thing to watch out for is, if you call Set() again, that array will in turn be deleted (which will blow up if that array is a stack array, like in this case). So, it might be advisable to separate the line that does delete[] b into its own member function, and call it only when necessary.

网友答案:

Your problem with set is that you need to copy the array contents - just doing b=c can't do what you want.

void Set(float * c[8][8]){
    for(unsigned int i=0; i<8; ++i) {
      for(unsigned int j=0; j<8; ++j) {
        b[i][j] = c[i][j];
      }
    }
    cout << *(b[1][5]) << endl;
}
网友答案:

There are several bugs I think.. Firstly, in Set() function you have assigned an array name. But array name should not be used as a variable. You can solve this by making 2D array of pointer b as a pointer to 1D array of pointer like float * (*b)[8]...

Secondly, when you send the 2D array of pointer as argument of Set() function it is decaying into a pointer to 1D array of pointer i.e something like this float *(*a)[8]. so you have to make the formal argument of Set() function a pointer to 1D array of pointers like void Set(float * (*c)[8])...

And finally there is a thing your float variable m inside main is a local to main , so when control pass to Set() function I think the compiler cant find the value(may be deallocated) in the m...so it outputs undefined or give run time error..you can solve this by making m a static version. i.e by declaring static float m...

In total make your code like following :

#include <iostream>
using namespace std;
struct X{
    float n, *(*b)[8];
    X(){
        n = 1;
        b[1][5] = &n;
        cout << *(b[1][5]) << endl;
    }
    void Set(float * (*c)[8]){
        b = c;
        cout << *(b[1][5]) << endl;
    }
};
main(){
    float * a[8][8];
    static float m;
    m = 2;
    a[1][5] = &m;
    X obj;
    obj.Set(a);
}

This will output correctly :)

相关阅读:
Top