YS_20190904_C语言_03

指针

  1. &p 打印内存地址
  2. 取地址符 & 获取变量的内存地址
符号
#include "pointer.h"
#include <stdio.h>
#include "../defines.h"//跳到上一级找头文件
#include <stdlib.h>
//指针中的符号
void test1(){
    /*
     * 取地址符  & 获取变量的内存地址
     * %p 打印内存地址
     */
    int a= 110;//000000000062FDBC 一个位置且不固定
    printf("%p\n",&a);
    /*
     *  *号  声明指针变量的符号 ->把某一个数据类型变成指针类型
     * 指针类型-->值是内存地址
     */
    int *ap;//int类型的指针变量
    //ap的值必须是一个内存地址
    ap = &a;// ap 取 a 的地址
    printf("add:%p\n",ap);
    /*
     * 可以通过内存地址  找到里面存储的值
     *   *号也可以读取指针变量存储的值
     *
     */
    printf("value:%d\n",*ap);// *号也可以取变量值
    *ap =12000;
    printf("%d\n",a);//*ap也可以取值
空指针
/*
     * 空指针
     * 没有指向任何位置的指针
     */
    int *b = NULL;
数组指针
/*
 * 数组指针
 */
void test2(){
    int arr[]={2,5,7,9};
    int *arrP = &arr[0];

指针运算
/*
     * 指针运算(挪动指针位置)
     * ++向右
     * --向左
     * +=向右挪动几个位置
     * -=向左挪动
    */
    printf("%d\n",*arrP);
    arrP++;
//通过指针遍历数组
void test3(){
    int arr1[]={2,5,7,9};
    int* arrP1 = &arr1[0];
    int l;
    for (l= 0;l<4;l++){
        printf("value:%d\n",arr1[l]);
        printf("value:%d\n",*arrP1);
        *arrP1++;

    }
指针数组
/*
 * 指针数组
 * 存放着指针对象的数组
 */
void test4(){
    //只能存放内存地址
    int* arr[3];
    int a= 10;
    int b= 10;
    int c= 100;
    arr[0] = &a;//000000000062FD9C
    arr[1] = &b;
    arr[2] = &c;
    printf("%p\n",arr[0]);
    printf("%p\n",&a);
    printf("%p\n",arr[1]);
    printf("%p\n",&b);
    a = 20;
    printf("%d\n",*arr[0]);
}

把指针对象当作参数传递
void test5(int *a){

    *a = 30;
    printf("%d\n",*a);
    int app = 10;
    printf("%d\n",app);
}

void run(){
    //test1();
    //test2();
    //test3();
    //test4();
    int app;
    app = 20;
    printf("%d\n",app);
    test5(&app);
    app = 40;
    printf("%d\n",app);// 为什么不是10?局部变量?
 }
指向指针的指针
/ 指向指针的指针
void test6(){
    int a = 10;
    int* ap = &a;
    //int* 类型的指针变量 -- > int **app
    //对应的值是  int* 类型的内存地址
    //
    int **app = &ap;
    printf("%d\n",**app);
    printf("%d\n",*ap);
    printf("%d\n",a);
    printf("%p\n",*app);
    printf("%p\n",ap);
    printf("%p\n",&a);
}
函数的指针对象
/*
 * 函数的指针对象
 */
void callback(){printf("....\n");}
void loadData(void (*ld)()){
    ld();
回调函数
void test7(){
    //int *a;
    //char *s;
    //int **aa;
    void (*c)() = &callback;
    //回调函数
    loadData(c);
}

void updateUI(const char *y){
    printf("update UI\n");
    printf("%s\n",y);
}
void readFile(void (*cb)(const char *)){
    const char *msg = "result";
    cb(msg);
    //printf(".........");
}


//typedef __int8 myChar;
//myChar a ='a';
//typedef __int8  myChar;
//typedef void(*Fun)();
//myChar C;
//Fun fun;
void run(){
    test7();
    readFile(&updateUI);
}
自定义函数指针类型

defines.h

/*
 * 自定义函数指针类型
 */
typedef void(*Function)();

typedef int* *(Fun)();
void test8(Function callback){
    const char *msg= "result";
    const char *p = "continue";
    const char *con = "dadwa";
    callback(msg);
    callback(p);
    callback(con);
}
void test9(const char *m){
    printf("%s\n",m);
}

void test10(const char *p,const char *con){
    printf("%s\n",p);
    //printf("%s\n",con);
}
void run(){

    Function fun= &test9;
    test8(fun);
    Function f = &test10;
     test8(f);
}     
通过指针遍历数组
void  test11(Fun *a){
    int *arr = a();
    int l = 0;
    for(l=0;l<5;l++){
        printf("%d\n",*arr);
        ++*arr;
    }

}
int *test12(){
    int arr[] ={4,5,6,7,8};
    int *ac=  &arr[0];
    return ac;
}
void run(){
	test11(&test12);
}
实现闭包函数

/*
 * 实现闭包函数
 */
Function test13(){
    return &test6;//把函数指针当做返回值
}
void run(){
	test12()();
}

内存管理
void test14(){
    //sizeof();查看内存空间大小
    //可以通过malloc创建指针对象
    //需要引入<stdlib.h>
    int *a= malloc(sizeof(int));
    //free();释放内存空间
    free(a);//释放之后就不要再继续使用释放之后的变量
}
/*
 * init()初始化函数
 * release()释放函数
 */
int *init(int a){
    int *temp  =malloc(sizeof(int));
    *temp  = a;
    return *temp;
}
void release(int *a ){
    free(a);
}
void run(){
	int *a = init(12);
    printf("%d\n",a);
    release(a);
}

结构体

define.h

/*
 * 结构体
 * 可以包含多种数据结构的综合体
 */
struct Person{
    const char *name;
    int age;
    //Function move;
};


typedef struct _Class{
    const char *name;
    __int8 lv;
    Function working;
    const char **stu;

}Class;

typedef struct _Animal{
    struct _Animal *(*run)(struct _Animal *animal);
    struct _Animal *(*eat)(struct _Animal *animal);
    struct _Animal *(*heal)(struct _Animal *animal);
    const char *name ;
}Animal;

结构体指针
void working(){
    printf("on class\n ");
}
void test15(){
    
/*
* 结构体
*/
//声明结构体
    struct Person xiaoming;
    xiaoming.name = "小明";
    xiaoming.age = 16;

//直接赋值花括号
    struct  Person xiaoR={"xiaoR",16};
    printf("%s\n %d\n",xiaoming.name,xiaoming.age);
    printf("%s\n %d\n",xiaoR.name,xiaoR.age);
//班级
    Class niuX;
    niuX.name = "perfect class";
    niuX.lv = 4;
    const char **stu = {
            "xiaoming",
            "xianghuang"
    };
    niuX.stu = stu;
    niuX.working =&working;
    niuX.working();
}
void speak1(struct Person *per){
    /*
     * 结构体指针对象的成员 是通过->来获取的
     */
    printf("%s 唠唠叨叨\n",per->name);
}
void run(){
	test15();
    struct Person xiaomao = {"xiaomao",25};
    struct Person *xiaomaoDE = &xiaomao;
    speak1(xiaomaoDE);
    }
仿链式函数
//仿链式函数
//xx.a().b().c()
Animal *run1(Animal *r){
    printf("%s running \n",r->name);
    return r;
}

Animal *eat1(Animal *e){
    printf("%s eating \n",e->name);
    return e;
}
Animal *heal1(Animal *h){
    printf("%s: I need healing\n",h->name);
    return h;
}
Animal *fight1(Animal *f){
    printf("%s fighting \n",f->name);
    return f;
}
void run(){
    Animal Cat;
    Cat.name = "Genji";
    Cat.run = &run1;
    Cat.eat = &eat1;
    Cat.heal = &heal1;
    Cat.fight = &fight1;
    Cat.run(&Cat)->eat(&Cat);
    printf(Cat.eat(&Cat)->heal(&Cat)->fight(&Cat)->run(&Cat)->name);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值