簡體   English   中英

如何在 C 中使用多維 arrays 進行指針運算?

[英]How do you do Pointer arithmetic with multidimensional arrays in C?

如何在多維數組中使用指針? 在每個方向上,我將如何用指針算術代替我所做的? 我已將我的 ptr 定義為 *location。 我認為我需要進行此更改,因為當 totalHops>400 時出現分段錯誤。 因此,每次顯式更改 x、y、z 都會導致此錯誤。 上下文:我在一個 L×L×L 3D 空間中移動一個粒子。 我有一個隨機數生成器來確定每次粒子隨機移動位置時粒子是否向左、向右、向上、向下、向后或向后移動。 (請注意,我已將系統設計為具有周期性邊界條件)。

const int L = 10;
int N = L*L*L;
const int totalHops = 200; 
int sites[L][L][L] = {};
int x = 0, y = 0, z = 0;
int tracker[N] = {};
int *location;
location = &sites[0][0][0];
for (int i = 1; i <= totalHops; i++) // the random walk //
    {
        int direction = randomInt(6); // six possible directions to move //
        // along x //
        if (direction == 0) { // move in negative direction //
            x -= 1;
            if (x == -1)
            {
                x = L-1;
            }
        }
        if (direction == 1) { // move in positive direction //
            x +=1;
            if (x == L) 
            {
                x = 0;
            }
        }
        // along y //
        if (direction == 2) { // move in negative direction //
            y -= 1;
            if (y == -1)
            {
                y = L-1;
            }
        }
        if (direction == 3) { // move in positive direction //
            y +=1;
            if (y == L) 
            {
                y = 0;
            }
        }
        // along z //
        if (direction == 4) { // move in negative direction //
            z -= 1;
            if (z == -1)
            {
                z = L-1;
            }
        }
        if (direction == 5) { // move in positive direction //
            z +=1;
            if (z == L) 
            {
                z = 0;
            }
        }
    tracker[i] = sites[x][y][z]; }

非常感謝您提前提供的幫助。

請記住,盡管C可以容納數組表示法,例如 2D、3D、...、nD arrays,以便從人類可讀性的角度更自然地使用它們。 但在 memory 中,arrays 實際上是作為一個連續的 memory 塊創建的。 例如你的數組:

const int L = 10;
...
int sites[L][L][L] = {0}; //(using {0} is an idiomatic way to init. arrays to all 0

在 memory 中排列為 memory 的 10*10*10 個連續 sizeof(int) 部分,從sites指向的 memory 位置開始。

| | | | | | | | | ...| | | | 
^                        ^
sites + 0                sites + (10*10*10 - 1)*sizeof(int)

由於這個事實,指針數學變得非常簡單:

*(sites + 0)   is equivalent to sites[0][0][0]
*(sites + 1)   is equivalent to sites[0][0][1]
*(sites + 2)   is equivalent to sites[0][0][2]
...
*(sites + 10)  is equivalent to sites[0][1][0]
...
*(sites + 100) is equivalent to sites[1][0][0]
...
*(sites + 998) is equivalent to sites[9][9][8]
*(sites + 999) is equivalent to sites[9][9][9]

指針表示法和數組表示法之間的模式變得非常明顯,因為添加到數組開頭的數字與數組表示法中索引的排列相關。

基於這種基本形式,您可以推導出一種使用指針數學表示多維 arrays 的方法,在您的情況下,使用int *location; 初始化到sites的開頭可用於跟蹤(或確定)正在查看或修改3D數組的哪些元素。

這可以很好地適用於跟蹤totalHops的特定問題,並且在x,y,z的任何方向上基於0 - 9范圍之外的值做出決策可能比基於*(sites + 400) (根據您在 OP 中的描述)。

所以總結我的分段錯誤,我只是在我的跟蹤器數組中使用了錯誤的變量。 但盡管如此,作為一個相當新的程序員,有這些對話是件好事,並感謝您的所有幫助。 我很高興探索了索引和指針的使用。

數組需要

int tracker[totalHops] = {};

暫無
暫無

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

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