C指针笔记

#include <stdio.h>

int main1() {
    /*
   * (1)指针数组
   */
    int *a[10];//定义了一个指针数组,一共10个成员,每个成员都是int*类型
    printf("%d, %d\n", sizeof(a), sizeof(a[0]));
    short *b[10];//定义了一个指针数组,一共10个成员,每个成员都是short*类型
    printf("%d, %d\n", sizeof(b), sizeof(b[0]));
    //    80, 8
    //    80, 8

    /*
     * (2)二级指针
     */
    int c = 10;
    int *p = &c;
    int **pp = &p;  //定义了一个二级指针,指向了一个以一级指针的地址
    **pp = 100;     //通过二级指针修改内存的值
    printf("c = %d\n", c);

    /*
     * (3)指向多维数组的指针
     */
    int buf[2][3] = {{1, 2, 3},
                     {4, 5, 6}};
    int *p1[3];     //指针数组,每个成员都是int*类型
    int (*p2)[3];   //定义一个指针,指向int[3]这种数据类型,用来操作二维数组,所以也称为指向二维数组的指针
                    //这样理解:int[3] *p2   当然这种形式语法上是错误的
    p2 = buf;
    int i,j = 0;
    for (i = 0; i < 2; ++i) {
        for (j = 0; j < 3; ++j) {
            printf("%d\n", p2[i][j]);
            printf("%d\n", *(*(p2 + i) + j));//有点二级指针的意思(和一维数组有区别)
        }
    }
        //1
        //1
        //2
        //2
        //3
        //3
        //4
        //4
        //5
        //5
        //6
        //6
}
#include <stdio.h>

void set_array(int[]);

void print_array(int[]);

void print_array1(const int *, int);//利用const来标识不需改变的量(在函数内部不能对其值进行改变)

void print_2D_array(int (*p)[3], int m, int n);

char *mystrchr(char *, char);

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main2() {
    /**
     * (1) 指针变量做为函数的参数
     */
    int input;
    scanf("%d", &input);//必须用指针作为函数参数,否则值传递是无法改变input变量值的
    printf("input: %d\n", input);
    int a = 10, b = 20;
    printf("before swap: a = %d, b = %d\n", a, b);
    swap(&a, &b);
    printf("after swap: a = %d, b = %d\n", a, b);
//    before swap: a = 10, b = 20
//    after swap: a = 20, b = 10
    /**
     * (2) 一维数组名作为函数参数
     * 当数组名作为函数参数时,C语言将数组名解释为指针
     */
    int buf[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    printf("buf = %d,int = %d\n", sizeof(buf),sizeof(int));
    print_array(buf);
    set_array(buf);
    print_array1(buf, sizeof(buf) / sizeof(int));
    /**
     * (3) 二维数组名作为函数参数(不常用)
     */
    int buf1[2][3] = {{1, 2, 3},
                      {4, 5, 6}};
    print_2D_array(buf1, sizeof(buf1) / sizeof(buf1[0]), sizeof(buf1[0]) / sizeof(int));
    /**
     * (4) 指针做为函数的返回值
     */
    char s[100] = "hello world";
    char * s1 = mystrchr(s,'o');
    printf("%s\n",s1);

}

void set_array(int buf[]) {
    buf[0] = 100;
    buf[1] = 200;
}

void print_array(int buf[]) {
    int len = sizeof(buf) / sizeof(int);//这样计算不出数组长度
    printf("len = %d\n",len);
    int i;
    for (i = 0; i < 10; ++i) {
        printf("buf[%d] = %d\n", i, buf[i]);

    }
}

//改进 print_array
void print_array1(const int *buf, int n) {
    int i;
    for (i = 0; i < n; ++i) {
        printf("buf[%d] = %d\n", i, buf[i]);
    }
}

void print_2D_array(int (*p)[3], int m, int n) {
    int i, j;
    for (i = 0; i < m; ++i) {
        for (j = 0; j < n; ++j) {
            printf("p[%d][%d] = %d\n", i, j, p[i][j]);
        }
    }
}

char *mystrchr(char *s, char c) {
    while (*s) {
        if (*s == c)
            return s;
        s++;
    }
    return NULL;
}
#include <stdio.h>

int max(int, int);
int min(int, int);
int func1(int (*p)(int, int), int a, int b) ;

int main3() {
    /**
     * 指针高级话题:函数指针(有点函数式编程的感觉)
     */
    /**
     * (1)指向函数的指针(C语言中非常高级的话题,有点函数式编程的意思)
     */
    int *p1(int, int);      //声明了一个函数p1,返回值为int*,参数为int,int
    int *(*p2)(int, int);   //声明了一个参数为int,int,返回值为int*的函数指针
    int (*p)(int, int);
    p = max;
    printf("max=%d\n", p(4, 19));

    /**
     * (2)把指向函数的指针做为函数的参数,回调函数
     */
    int res = func1(max,10,229);//max在这里就叫回调函数
    printf("%d\n",res);
    int res1 = func1(min,10,229);
    printf("%d\n",res1);

}

int max(int a, int b) {
    return a > b ? a : b;
}

int min(int a, int b) {
    return a < b ? a : b;
}

int func1(int (*p)(int, int), int a, int b) {
    return p(a, b);
}
#include <stdio.h>
#include <string.h>

void print_arrayh(const int *, int);

int main() {
    /**
     * 内存操作函数
     */

    /**
     * (1)内存值设置(初始化)
     */
    int buf[10] = {0};      //只能用于定义数组时同时初始化内容
    buf[0] = 8;
    buf[1] = 9;
    print_arrayh(buf, 10);
    //想将这个buf再一次初始化为0,buf[10] = {0},错误的用法
    //按下述方法初始化,但是当数组较大时,效率低
    int i;
    for (i = 0; i < 10; ++i) {
        buf[i] = 0;
    }
    print_arrayh(buf, 10);
    //将一块内存初始化为0最常见方法
    //void *memset (void *__s, int __c, size_t __n)
    //第一个参数是要设置的内存地址
    //第二个参数是要设置的值
    //第三个参数是内存大小(单位字节)
    memset(buf, 0, sizeof(buf));
    print_arrayh(buf, 10);
    /**
     * (2)内存拷贝
     */
    int buf1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int buf2[10];
    memcpy(buf2,buf1, sizeof(buf1));
    print_arrayh(buf2,10);

    /**
     * (3)内存移动
     *  memmove()
     */
}

void print_arrayh(const int *buf, int n) {
    printf("数组buf:\n");
    int i;
    for (i = 0; i < n; ++i) {
        printf("buf[%d] = %d\n", i, buf[i]);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值