指针的详解

1 NULL指针
2 指针的算数运算
3 指针 VS 数组
4 指针数组
5 指向指针的指针
6 传递指针给函数
7 从函数返回指针


1 NULL 指针
#include <iostream> using namespace std; int main() { //如果没有确切的地址可以赋值,为指针变量赋一个 NULL 值是一个良好的编程习惯。赋为 NULL 值的指针被称为空指针。 int *ptr = NULL; cout << "ptr is " << ptr << endl; if (ptr) { cout << "不打印" << endl; } else { cout << "运行" << endl; } return 0; } /* vim: set ts=4 sw=4 sts=4 tw=100 */
2 指针的算数运算  《 ++  --  + -》

#include <iostream>
using namespace std;

const int MAX = 3;

int main()
{
    int var[MAX] = {10, 100, 200};
    int *ptr;
    //数组名为数据的首地址
    ptr = var;
    for(int i=0; i < MAX; i++)
    {
        cout << "Address of var [" << i << "] =";
        cout << ptr << endl;

        cout << "Value of var[" << i << "] =";
        cout << *ptr << endl;

        ptr++;//地址(指针)移到下一个位置
    }
    return 0;
}

/* vim: set ts=4 sw=4 sts=4 tw=100 */
3 指针的比较

#include <iostream>
using namespace std;

const int MAX = 3;


int main()
{
    int var[MAX] = {10, 100, 200};
    int *ptr;

    *(var + 2) = 500;

    ptr = var;
    while ( ptr <= &var[MAX -1])
    {
        cout << ptr << "& " << *ptr << endl;
        ptr ++;
    }
    return 0;
}

 

4 指针数组:数组存储指向int或char或其他数据类型的指针

存储指向int类型指针的数组
存储指向char类型指针的数组
#include <iostream> using namespace std; const int MAX = 3; int main() { int var[MAX] = {100, 200, 300}; cout << "作为数组使用" << endl; for(int i=0; i<MAX; i++) { cout << var[i] << endl; } int *ptr[MAX]; for(int i=0; i<MAX; i++) { ptr[i] = &var[i];//赋值为整数的地址 } //int *prt[MAX] = {100, 200, 300}; ERR cout << "指向整形的指针数组" << endl; for(int i=0; i<MAX; i++) { cout << *ptr[i] << endl; } cout << "指向字符的指针数组来存储一个字符串列表" << endl; char *names[MAX] = {"azplkt", "ask", "fbljs"}; for(int i=0; i<MAX; i++) { cout << names[i] << endl; } return 0; } /* vim: set ts=4 sw=4 sts=4 tw=100 */

 

5 传递指针给一个函数
#include <iostream>
using namespace std;

void getSends(unsigned long *ptr);
double getAverage(int *arr, int size);
const int N = 5;

int main()
{
    unsigned long sec = 1;
    getSends( &sec);
    cout << sec << endl;

    //int size = 5;
    //int balance[size] = {1000, 2, 3, 17, 50};

    int balance[N] = {1000, 2, 3, 17, 50};

    double aver = getAverage(balance, N);
    cout << "average :" << aver << endl;
}


void getSends(unsigned long *ptr)
{
    *ptr = time(NULL);
}


double getAverage(int *arr, int size)
{
    double sum = 0;
    for (int i=0; i<size; i++)
    {
        sum += arr[i];
    }
    double aver = sum / size;
    return aver;
}

/* vim: set ts=4 sw=4 sts=4 tw=100 */

 

6 从函数返回指针
#include <iostream> #include <ctime> using namespace std; int * getRandom() {
   //C++ 不支持在函数外返回局部变量的地址,除非定义局部变量为 static 变量。
static int r[10]; //设置种子 srand( (unsigned)time(NULL) ); for( int i = 0; i < 10; ++i) { r[i] = rand(); cout << r[i] << endl; } return r; } int main() { int *p = getRandom(); cout << "==================" << endl; for(int i=0; i<10; i++) { cout << "*(p + " << i << ") : "; cout << *(p+i) << endl;//解引用方式 cout << p[i] << endl;//数组下标方式 } }

 

posted on 2015-03-20 18:35  雨渐渐  阅读(259)  评论(0)    收藏  举报

导航