C++博客 :: 首页 :: 新随笔 ::  ::  :: 管理

指针操作一维数组

Posted on 2011-05-27 21:30 Kevin_Zhang 阅读(276) 评论(0)  编辑 收藏 引用 所属分类: C/C++
用指针操作数组。要求先输出数组,然后将数组倒置,再输出倒置的数组。
源代码:
// test3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include 
"iostream"
using namespace std;


int main(int argc, char* argv[])
{   
    
int *ptr1,*ptr2,t;
    
int a[]={1,2,3,4,5,6,7,8,9,10};
    ptr1
=a;
    ptr2
=&a[9];
    
for(int i=0;i<10;i++)
        cout
<<*(ptr1+i)<<" ";
    cout
<<endl;
    
while(ptr2>ptr1)
    
{
        t
=*ptr1;
        
*ptr1=*ptr2;
        
*ptr2=t;
        ptr1
++;
        ptr2
--;
    }

    ptr1
=&a[0];
    
for(int j=0;j<10;j++)
        cout
<<ptr1[j]<<" ";
    cout
<<endl;
    
return 0;
}



只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理