簡體   English   中英

無法從int [] []轉換為int *

[英]Cannot Convert from int[][] to int*

我有一個3x3數組,我正在嘗試創建一個指向它的指針,但我一直在獲取該數組,這有什么用?

我該如何定義指針? 我已經嘗試過[]和*的每種組合。

是否有可能做到這一點?

int tempSec[3][3];

int* pTemp = tempSec;

你可以做int *pTemp = &tempSec[0][0];

如果要將3x3數組視為int *,則可能應將其聲明為int[9] ,並使用tempSec[3*x+y]而不是tempSec[x][y]

另外,也許您想要的是int (*pTemp)[3] = tempSec 那將是一個指向tempSec的第一個元素的指針,該第一個元素本身是一個數組。

實際上,您可以將指針指向2D數組:

int (*pTemp)[3][3] = &tempSex;

然后,您可以像這樣使用它:

(*pTemp)[1][2] = 12;

幾乎可以肯定這不是您想要的,但是您在評論中確實要求過……

使用typedef更容易

typedef int  ThreeArray[3];
typedef int  ThreeByThree[3][3];

int main(int argc, char* argv[])
{
    int         data[3][3];

    ThreeArray* dPoint    = data;
    dPoint[0][2]          = 5;
    dPoint[2][1]          = 6;   

    // Doing it without the typedef makes the syntax very hard to read.
    //
    int(*xxPointer)[3]    = data;
    xxPointer[0][1]       = 7;

    // Building a pointer to a three by Three array directly.
    //
    ThreeByThree*  p1     = &data;
    (*p1)[1][2]           = 10;

    // Building a pointer to a three by Three array directly (without typedef)
    //
    int(*p2)[3][3]        = &data;
    (*p2)[1][2]           = 11;

    // Building a reference to a 3 by 3 array.
    //
    ThreeByThree&  ref1   = data;
    ref1[0][0]            = 8;

    // Building a reference to a 3 by 3 array (Without the typedef)
    //
    int(&ref2)[3][3]      = data;
    ref2[1][1]            = 9;


    return 0;
}

哦。 這很容易!

int aai[3][3];
int* pi = reinterpret_cast<int*>(aai);

您實際上可以使用這種很棒的技術將其轉換為其他出色的類型。 例如:

int aai[3][3];
int (__stdcall *pfi_lds)(long, double, char*) = reinterpret_cast<int (__stdcall *)(long, double, char*)>(aai);

那不只是膨脹嗎? 問題是它是否有意義。

您在問如何欺騙您的編譯器。 所以首先要知道的是:你為什么要說謊?

int a[20][30];
int* b=&a[0][0];

原始帖子如下-請忽略,它是錯誤的消息。 為了后代的緣故;)

但是, 這是我發現的有關c ++中二維數組的內存分配的鏈接 也許它可能更有價值。


不確定這是您想要的東西,而且自從我編寫c ++以來已經有一段時間了,但是強制轉換失敗的原因是因為您要從數組數組轉到int指針。 另一方面,如果您嘗試從一個數組到另一個數組再到一個指針的指針,則可能會起作用

int tempSec[3][3]; 
int** pTemp = tempSec; 

記住,您的數組數組實際上是一個連續的內存塊,其中包含指向其他連續內存塊的指針-這就是為什么將數組的數組強制轉換為int數組將為您提供看起來像垃圾的數組的原因[該垃圾實際上是內存地址!]。

再次,取決於您想要什么。 如果要使用指針格式,則必須使用指針指針。 如果要將所有9個元素都作為一個連續數組,則必須對雙精度數組進行線性化。

正如Steve所指出的那樣,適當的形式是int *pTemp = &tempSec[0][0]; int** pTemp2 = tempSec; 不起作用。 給出的錯誤是:

cannot convert 'int (*)[3]' to 'int**' in initialization

它不存儲為指向數組的指針數組。 它被存儲為一個大向量,並且編譯器向您隱藏[a][b] = [a*rowLength+b]

#include <iostream>
using namespace std;
int main()
{
    // Allocate on stack and initialize.
    int tempSec[3][3];
    int n = 0;
    for(int x = 0; x < 3; ++x)
        for(int y = 0; y < 3; ++y)
            tempSec[x][y] = n++;

    // Print some addresses.
    cout << "Array base: " << size_t(tempSec) << endl;
    for(int x = 0; x < 3; ++x)
        cout << "Row " << x << " base: " << size_t(tempSec[x]) << endl;

    // Print contents.
    cout << "As a 1-D vector:" << endl;
    int *pTemp = &tempSec[0][0];
    for(int k = 0; k < 9; ++k)
        cout << "pTemp[" << k << "] = " << pTemp[k] << endl;
    return 0;
}

輸出:

Array base: 140734799802384
Row 0 base: 140734799802384
Row 1 base: 140734799802396
Row 2 base: 140734799802408
As a 1-D vector:
pTemp[0] = 0
pTemp[1] = 1
pTemp[2] = 2
pTemp[3] = 3
pTemp[4] = 4
pTemp[5] = 5
pTemp[6] = 6
pTemp[7] = 7
pTemp[8] = 8

請注意,第0行的地址與整個數組的地址相同,並且連續的行偏移sizeof(int) * 3 = 12

進行此操作的另一種方法是,首先創建一個指針數組:

int* pa[3] = { temp[0], temp[1], temp[2] };

然后創建一個指向該指針的指針:

int** pp = pa;

然后,您可以在該指針指針上使用常規數組語法來獲取所需的元素:

int x = pp[1][0]; // gets the first element of the second array

另外,如果您嘗試將其轉換為指針的唯一原因是可以將其傳遞給函數,則可以執行以下操作:

void f(int v[3][3]);

只要數組的大小是固定的,就可以將二維數組傳遞給這樣的函數。 比傳遞指針具體得多。

讓我們要求cdecl.org為我們翻譯您的聲明:

int tempSec[3][3]

退貨

declare tempSec as array 3 of array 3 of int 


好的,我們如何創建一個指向它的指針? 讓我們再次詢問cdecl:

declare pTemp as pointer to array 3 of array 3 of int

退貨

int (*pTemp)[3][3]


由於我們已經有了int數組3的數組3,所以我們可以這樣做:

int (*pTemp)[3][3] = &tempSec;
int tempSec[3][3];

int* pTemp = tempSec[0];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM