簡體   English   中英

如何將這些數字存儲在C中?

[英]How can I store these numbers in C?

假設您有一個像這樣的數組

{ 1 2 5 7 2 3 7 4 2 1 }

並且您想存儲數組的前半部分和后半部分之間的差異在位置2和4。

訣竅是,我稍后需要在其他代碼中使用這些存儲的數字,所以我不知道是如何存儲這些數字的。

我有這種方法

int * getPositions(int *array, int size){
    int * c[(size/2)];
    int counter = 0;
    for(int i = 0; i < size /2; i++) {
        if (*(array + i) != *(array + (size - 1) - i)) {
            c[counter]= (int *) i;
            counter++;
        }
    }return (int *) c;
 }

但似乎將-1774298560存儲在每個位置。 當我嘗試打印時,我知道原因

int c = (int) getPositions(array, size_of_array);

for(int i = 0; i < ((size_of_array/2)); i++){
    printf("%d\t", c);
}

它打印出來的是

-1774298560 -1774298560 -1774298560 -1774298560 -1774298560

PS:我在其他地方初始化了arraysize_of_array

PS:我已經考慮了這些注釋,並將代碼更改為以下內容

int * getPositions(int *array, int size){
    int * c = (int *) malloc((size_t) (size/2));
    int counter = 0;
    for(int i = 0; i < size /2; i++) {
        if (*(array + i) != *(array + (size - 1) - i)) {
            c[counter]= i;
            counter++;
        }
}

如果函數應返回一個簡單的int數組,則需要聲明一個指向int的指針,然后調用malloc為該數組保留空間。 然后填寫數組,並返回指針。 調用函數將需要在某個時候free內存。

int *getPositions(int *array, int size)
{
    int *c = malloc( (size/2) * sizeof(int) );
    if ( c == NULL )
        return NULL;

    // put stuff in the array using array syntax, e.g. 
    c[0] = array[0];

    return c;
}

像這樣調用函數

int *c = getPositions( array, size );

if ( c != NULL ) 
    for( int i = 0; i < (size/2)); i++ )
        printf( "%d\t", c[i] );

free( c );

筆記:

  • 是的,在C中進行錯誤檢查很麻煩,但是您必須這樣做,否則您的程序將隨機崩潰。
  • 您可以將數組語法與指針一起使用,只需確保不要在指針所引用的內存末尾讀或寫。
  • NULL指針傳遞給free是合法的。

另外一個選項。

int * getPositions(int *array, int size);
int main() {
    int array[] = { 1, 2, 5, 7, 2, 3, 7, 4, 2, 1 };

    int size_of_array = sizeof(array) / sizeof(int);
    int *ptr = getPositions(array, size_of_array);

    for(int i = 0; ptr[i] != '\0' ; i++){
        printf("%d\t", *(ptr + i));
    }

    return 0;
}
int * getPositions(int *array, int size) {
    int temp[size/2];
    int counter = 0;

    for (int i = 0; i < size / 2 ; i++) {
        if (array[i] != array[(size - 1) - i]) {
            temp[counter++] = i;
        }
    }
    int *c = malloc(counter * sizeof(int));
    for (int i = 0; i < counter; i++) {
        c[i] = temp[i];
    }

    return  c;
}

我的編譯器實際上發瘋了...您正在使用哪個編譯器? 任何現代靜電分析儀都應該警告您

aftnix@dev:~⟫ gcc -std=c11 -Wall st.c 
st.c: In function ‘getPositions’:
st.c:8:25: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
             c[counter]= (int *) i;
                         ^
st.c:11:6: warning: function returns address of local variable [-Wreturn-local-addr]
     }return (int *) c;
      ^
st.c: In function ‘main’:
st.c:16:13: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
     int c = (int) getPositions(array, 10);

因此,編譯器將顯示代碼的所有問題。 而且您編輯的代碼甚至無法編譯:

aftnix@dev:~⟫ gcc -std=c11 -Wall st.c 
st.c:4:4: error: expected identifier or ‘(’ before ‘[’ token
 int[] getPositions(int *array, int size){
    ^
st.c: In function ‘main’:
st.c:17:5: warning: implicit declaration of function ‘getPositions’ [-Wimplicit-function-declaration]
     int c = (int) getPositions(array, 10);

您必須考慮幾件事。

  1. 如果需要返回存儲地址,請不要嘗試返回局部變量。 由於局部變量在堆棧中分配。 堆疊框架具有相同的功能壽命。 使用malloc和co在堆上分配內存。 對於C程序員來說,了解Runtime數據結構的分配和使用方式非常重要,因為C根本沒有花哨的神奇東西。 C幾乎是裸露的金屬。

堆疊與堆

  1. 在傳遞數組/指針時,應首先考慮設計。 您要“原位”更改原始數組嗎? 或者您想要“轉換”數組的其他副本。 通常,您應該嘗試避免“就地”更改內存,因為可能會有其他用戶這樣做。 但是,如果沒有這種擔心,則可以將函數設計為“輸入->輸出”的基礎。 這意味着您將不會返回void ,而是將返回轉換后的數組本身。

暫無
暫無

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

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