C數組逆序

來源:互聯網
上載者:User

標籤:數組   array   移動   模式   元素   nbsp   原理   聲明   實現   

一、標準交換模式

/****

*標準交換模式

*實現數組的逆序,原理就是數組的首尾元素進行交換

***/

#define N 5;

int main(){

int array[N] = {15,20,25,30,35}

int temp; //聲明臨時變數

int i;

for(i = 0;i<N/2;i++){

//第i個值和第N-i-1個值相交換

temp = array[i];

array[i] = array[N - i - 1];

array[N - i - 1] = temp;

}

printf("逆序:\n");

for(i = 0;i < N;i++){

printf("%d\t",*(array + i));

}

}

 

二、指標交換模式

/****

*指標交換模式

*實現數組的逆序,原理就是數組的首尾元素進行交換

***/

#define N 5;

int main(){

int array[N] = {15,20,25,30,35}

int temp; //聲明臨時變數

int i;

int *ptr_array_start = array;

int *ptr_array_end = array + N - 1;

while(ptr_array_start>=ptr_array_end){

//首尾交換,指標分別進行更新

temp = *ptr_array_start;

*ptr_array_start = *ptr_array_end;

*ptr_array_end = temp;

//首元素指標要向後移動

ptr_array_start++;

//末元素指標要向前移動

ptr_array_end--;

}

printf("逆序:\n");

for(i = 0;i < N;i++){

printf("%d\t",*(array + i));

}

}

 

C數組逆序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.