簡體   English   中英

如何在C中將數組的一維設置為零

[英]How to memset one dimension of an array to zero in C

假設我有一個3d數組(在數據段中)聲明為球形,我想將其1d設置為0。

int multi_dimension_array[x][y][z];

我可以用這行代碼來記下整個過程:

memset(multi_dimension_array, 0, sizeof(multi_dimension_array));

但是現在假設我只想將x維值設置為某個值(例如2),這意味着multi_dimension_array [2] [0] [0]到multi_dimension_array [2] [y-1] [z-1]的值應該全部為零。 我不認為有一種巧妙的方式將memset用於y或z,因為它們不連續。 以下行應起作用:

memset(&multi_dimension_array[2][0][0], 0, sizeof(multi_dimension_array[2][0][0]) * y * z);

我的“問題”是我不喜歡memset參數的* y * z部分。 數組中是否有東西說sizeof(multi_dimension_array [2])== byte_size_of_type * y * z?

我想使用數組的一個屬性,在這個示例中,sizeof將為“ x”維度求出正確的字節數。 如果有人更改了聲明中的大小,並且他們沒有更改此內存集,我不想使用* y * z,而且我不喜歡它的外觀。

memset(&multi_dimension_array[2], 0, sizeof multi_dimension_array[2]);

這要求multi_dimension_array[i]是數組的數組而不是指針的數組。 之所以起作用,是因為將sizeof應用於數組時,它將返回數組的大小。 數組不像大多數表達式那樣自動轉換為指針。

當然,它僅適用於第一個維度(如果您執行多個操作,則適用於前幾個維度)。 例如,您可以將一個內存集與array[i]array[i][j]一起使用,但不能與諸如array[???][j]類的中間維度一起使用。

對於以后這個問題的讀者來說,埃里克·波斯特皮斯希爾(Eric Postpischil)的答案是正確的,我已經接受了,因為它是正確的。 這是一個示例程序和輸出,用於演示應用於多維數組的sizeof運算符/函數。 通常,如果聲明為int數組

int mda[x][y][z]; 
then mda[a][b][c] == *(mda + ((a *( y* z)) + (b*z) + (c))

重要的是,與我最初提出的問題相比,最重要的是索引到數組中的“維數”少於您在程序中聲明的“維數”仍將編譯並維護下一個數組的“ sizeof”。

mda[a] == *(mda + (a * (y*z))) AND sizeof(mda[a]) == sizeof(array_type) * y * z
mda[a][b] == *(mda + (a * (y*z)) + (b * z)) AND sizeof(mda[a][b]) == sizeof(array_type) * z

好的,這是一個示例程序,您可以在在線IDE中運行並驗證:

#include <stdio.h>

#define X (4)
#define Y (10)
#define Z (5)
int multi_dimension_array[X][Y][Z];

void print_array(){
    for(int i=0; i<X; i++){
        printf("THIS IS THE %dth value of X\n", i);
        for(int j=0; j<Y; j++){
            for(int k=0; k<Z; k++){
                printf("%d ", multi_dimension_array[i][j][k]);
            }
            printf("\n");
        }
    }
}

int main(void) {
    printf("%d %d %d %d\n", sizeof(multi_dimension_array[0][0][0]), sizeof(multi_dimension_array[0][0]), sizeof(multi_dimension_array[0]), sizeof(multi_dimension_array));

    memset(multi_dimension_array,0x01,sizeof(multi_dimension_array));
    print_array();

    memset(&multi_dimension_array[2],0x0,sizeof(multi_dimension_array[2]));

    printf("\n\n\nNEXT MEMSET with the X=2 zeroed out\n\n\n");
    print_array();

    return 0;
}

這是程序輸出:

4 20 200 800
THIS IS THE 0th value of X
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
THIS IS THE 1th value of X
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
THIS IS THE 2th value of X
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
THIS IS THE 3th value of X
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 



NEXT MEMSET with the X=2 zeroed out


THIS IS THE 0th value of X
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
THIS IS THE 1th value of X
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
THIS IS THE 2th value of X
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
THIS IS THE 3th value of X
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 
16843009 16843009 16843009 16843009 16843009 

*請注意,memset將每個字節設置為第二個參數的值,在本例中為0x01,這使4個字節的整數0x01010101 == 16843009。

暫無
暫無

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

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