关于C指针问题浅谈(二)

函数指针:

一般自定义函数类型  typedef 函数返回值 (* 函数指针)(形参类型1, 形参类型2)

void  fun() {

	cout << "fun地址" << endl;
}

//对于函数而言可以加入& 操作符 也可以不用加 
printf("%p\n", &fun);
printf("%p\n", fun);

函数原型 void fun();
指针类型 typedef   void  (*Pfun) ()

int main(){

   typedef   void  (*Pfun) ();

   Pfun  pfun=&fun;

   cout<<pfun<<endl;

}

野指针  内存已经销毁  指针无法访问里面的内存

int* p = (int*)malloc(sizeof(int));
    free(p);//产生野指针 
    p = NULL;//指针初始化NULL

指针加法:

 

 

    int a = 10;
    int* p = &a;
    cout << p << endl;//a的地址
    cout << p + 1 << endl;//a地址的后一位地址 加入int 
    cout << *(p + 1) << endl;//a地址的后一位地址d的值

一维数组指针  函数名为首地址

int main() {
    int a[5] = { 1,2,3,4,5 };
    //int len = sizeof(a) / sizeof(int);
    int* p = a;
    int* q = &a[0];
    for (int i = 0; i <5; i++) {
        cout << "地址:" << &a[i] << endl;
    }
    cout << "首地址:" << p<< endl;
    cout << "首地址:" << q << endl;
    //全部遍历
    for (int i = 0; i < 5; i++) {
        cout << "地址:" << *(p+i) << endl;
    }

}

二维指针:

 

 字符串指针 和指针的指针的待续>>>>>>>>>>>>>>

 

posted @ 2021-06-10 20:26  AI_Mi  阅读(64)  评论(0)    收藏  举报