English 中文(简体)
如何为临时变量分配多维阵列?
原标题:How to assign a multi-dimensional array to a temporary variable?
  • 时间:2011-11-21 14:26:15
  •  标签:
  • c++

我想把一个固定分配的多维阵列分配给一个临时变量。 考虑以下例子:

void foo(int b[3][2])
{
    b[1][1] = 1; // no segmentation fault
}

int main()
{
    int a[3][2] = {{1, 2}, {11, 12}, {21, 22}};

    foo(a);

    int** c;
    c = (int**)&a;
    c[1][1] = 1; // segmentation fault on execution

    int* d[3];
    d[0] = (int*)&(a[0]);
    d[1] = (int*)&(a[1]);
    d[2] = (int*)&(a[2]);
    d[1][1] = 1; // no segmentation fault

    return 0;
}

基本上,我想由汇编者做以下工作:<代码>b>。 但是,我可以提出的唯一解决办法是d<>/code>。 是否有不那么复杂的方法?

最佳回答
问题回答

如您现在可能所知,从其他答复来看,<代码>a的类型实际上并不等于int**--它把 至(如按价值回归/绕过)。

int (*b)[2] = a; // would solve that

采用更多的C++方法:

typedef std::array<std::array<int, 2>, 3> M23;

void foo(M23& b)
{
    b[1][1] = 1; 
}

int main()
{
    M23 a = {{1, 2}, {11, 12}, {21, 22}};

    foo(a);

    M23 d = a;
    d[1][1] = 1;
}

如果你使用支持C++11标准足够部分的粗略现代汇编器,你可使用<代码>auto<>/code>:

int a[3][2] = ...;
auto &b = a;
b[1][1] = 1;  // a[1][1] will be set

当然,<条码>a和<条码>b都必须在其工作范围内加以界定。 您的职称中可有<代码>auto参数(例如,模板是什么)。

明确表达,这样尝试撰写

 c = &a;

Then the GCC compiler (using gcc -Wall -g bidim.c -o bidim to compile) gives you the correct warning:

bidim.c:13:7: warning: assignment from incompatible pointer type [enabled by default]

因此,你应当认识到,2D矩阵没有作为1D阵列的一系列要点加以实施。

The first thing that comes into my mind is using a typedef and reference like

typedef int thing_t[3][2];
thing_t& e = a;
e[1][1] = 1;

With pointers

int (*f)[2] = a;
f[1][1] = 1;

另一种可能性是将其载于struct





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签