C指针复习

//
//  main.c
//  point
//
//  Created by Tom on 2017/6/16.
//  Copyright © 2017年 Tom. All rights reserved.
//

#include <stdio.h>


/**
 过代码
 */
//**************************************************
void test1(){
    int a = 0;
    int b = 10;
    char buf[10];
    printf("%p,%p,%p",&a,buf);
    
    int *p;
    p = &a;
    printf("%x,%p",*p, p);
    int *p1;
    p1 = &b;
}

void test2()
{
    int a = 3;
    int *p;
    p = &a;
    printf("%d", *p);
    
    *p = 10;
    printf("a= %d\n", a);
    
    printf("------------");
    
    char buf[10];
    printf("%p,%p,%p,%p\n", buf, &buf[0],&buf[1], &buf[2]);
    
    char c = 0;
    char *p2 = &c;
    
    void *p3;
    p3 = NULL;
    printf("%ld\n", sizeof(p3));
}

void test3() {
    int a = 10;
    int *p;
    p = &a;
    *p = 1;
    printf("a = %d\n", *p);
}

void test4(){
    int a = 2;
    int b = 4;
    int c = 7;
    int *p;
    p = &a;
    
    *p = 10;
    p = &b;
    *p = 20;
    p = &c;
    *p = 30;
    printf("%d,%p,%d", a,&b,*p);
}

void test5(){
    int a = 0x1310;
    char b = 2;
    int *p = &b;
    printf("%x\n", *p);
    
    char buf[10] = {0x12, 0x13, 0x56, 0x78, 5, 6, 7, 8};
    p = buf;
    printf("%x\n", *p);
    
}

void test6(){
    float a = 3.14;
    int i = a;
    int *p = &a;
    printf("%d\n", *p);
}

void test7(){
    int a = 10;
    int b = 30;
    const int *p = &a;
    a= 20;
    p = &b;
}

void test8(){
    int a = 10;
    int b = 30;
    int *const p = &a;
    *p = 20;
    //p = &b;
    printf("%d", *p);
}
//*********************point&array*******************************
void print(char s[]){
    int i;
    for (i = 0;i < 10; i ++){
        printf("s[%d] = %d\n", i, s[i]);
    }
}

void atest(){
    char buf[10] = {0, 1, 2, 3, 4};
    char *p = &buf;
    char *p1 = &buf[0];
    char *p2 = &buf[1];
    char *p3 = &buf[2];
    char *p4 = &buf[3];
    
    *p2 = 7;
    p3 += 1;
    *p3 = 100;
    
    p3 -= 2;
    *p3 = 70;
    printf("%d, %d, %d, %d\n", p, p1, p2, p3);
    
    int i;
    for(i = 0; i < 10; i++){
        *p = i;
        p++;
    }
    
    p -= 10;
    for (i = 0; i < 10; i++){
        *p = i * 10;
        p++;
    }
}

void ip2s(int n){
    unsigned char *p = &n;
    printf("%u,%u,%u,%u", *p, *(p+1), *(p+2), *(p+3));
}
//储存ip地址
int s2ip(char s[]){
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    sscanf(s, "%d,%d,%d,%d", &a, &b, &c, &d);
    int ip = 0;
    char *p = &ip;
    *p = a;
    p++;
    *p = b;
    p++;
    *p = c;
    p++;
    *p = d;
    return ip;
}

void tranIP(){
    char s[] = "192.168.1.21";
    ip2s(s2ip(s));
}
//*******************point & array2*********************************
void bubble(int *s){
    int i;
    int j;
    for(i=0; i < 10; i++){
        for (j=1; j < 10 - i; j++) {
            if (s[j] < s[j-1]) {
                int temp = *(s + j);
                *(s+j) = *(s+j-1);
                *(s+j-1) = temp;
            }
        }
    }
}

void print_buf(int *s){
    int i;
    for(i=0; i < 10; i++){
        printf("%d\n", *(s+i));
    }
}

int max(int *s){
    for (int i = 1; i < 10; i++) {
        if (s[i] < s[i - 1]) {
            int temp = *(s + i);
            *(s + i) = *(s + i - 1);
            *(s + i - 1) = temp;
        }
    }
    return *(s + 9);
}

int smax (int *s){
    //不可以出现[]
//    for (int i = 0; i < 10; i++) {
//        if (*(s + i) < *(s + i - 1)) {
//            int tmp = *(s + i);
//            *(s + i) = *(s + i - 1);
//            *(s + i - 1) = tmp;
//        }
//    }
//    for (int i = 0; i < 9; i++) {
//        if (*(s+i) < *(s + i - 1)) {
//            int tmp = *(s+ i);
//            *(s + i) = *(s+i-1);
//            *(s+i-1) = tmp;
//        }
//    }
//    return *(s + 8);
    
    int max = *s >= *(s + 1) ? *s : *(s + 1);
    int s_max = *s < *(s + 1) ? *s : *(s + 1);;
    for (int i = 2; i < 10; i++) {
        if (*(s + i) > max) {
            s_max = max;
            max = *(s + i);
        }else if (*(s + i)< max && *(s + i) > s_max){
            s_max = *(s + i);
        }
    }
    return s_max;
}
//汉字字符串逆置
#include <string.h>
void tranChinese(){
    //不同编译器汉字字符占的字节数大小不一样;
    char str[] = "他好我也好";
    char *start = str;
    char *end = &str[(strlen(str) - 3)];
    while(start < end){
        for (int i = 0; i < 3; i++) {
            char tmp = *(start + i);
            *(start+i) = *(end+i);
            *(end+i) = tmp;
        }
        start += 3;
        end -= 3;
    }
    printf("%s", str);
    //e.g.
//    short *str_start = str;
//    short *str_end = &str[strlen(str) - 2];
//    while (str_start < str_end) {
//        short *tmp = *str_start;
//        *str_start = *str_end;
//        *str_end = *tmp;
//        str_start++;
//        str_end--;
//    }
//    printf("%s\n", str);
}
//get relative distance between two point;
long getRelativeDstance(int *p1, int *p2){
    return (p2 - p1) > 0 ? p2 - p1 : p1 - p2;
}


//*************************string practise***************************
char* tomstrstr(char s1[], char s2[]){
    char *p1 = s1;
    char *p2 = s2;
    int len = 0;
    while (*p1) {
        p1++;
        len++;
    }
    printf("%d", len);
    while (*p2) {
        *p1++ = *p2++;
    }
    printf("%s", p2);
    return p2;
}
//*******************main-a*********************************
int main01(int argc, char *argv[]){
    int i;
    for(i = 0; i < argc; i++){
        printf("%s", argv[i]);
    }
    return 0;
}
int main02(int argc, char *argv[]){
    char buff[100] = "dir";
    for (int i = 1; i < argc; i++) {
        strcat(buff, " ");
        strcat(buff, argv[i]);
    }
    system(buff);
    return 0;
}

//*****************func ping***********************************
int tommax(int x, int y){
    return x>y?x:y;
}
int tomadd(int x, int y){
    return x + y;
}
int main001(){
    int (*p) (int, int) = &tomadd;
    return p(1, 2);
}
int func1(int(*p)(int, int), int a, int b){
    return p(a, b);
}
int main002(){
    return func1(tomadd, 6, 8);
}
//*********************memory operate function*******************************
int memUsing(){
    int buf[1024] = {0};
    buf[0] = 8;
    buf[1] = 9;
    buf[2] = 8;
    memset(buf, 0, sizeof(buf));
    //第一个参数是要设置的内存地址,第二个参数是要设置的值,第三个参数是要设置的内存大小,单位:字节
    //将一个内存设置为0的最常用的方法
    for (int i = 0 ; i < 10 ; i++) {
        printf("buf[%i] = %d\n", i, buf[i]);
    }
    return 0;
}
int memcpyUsing(){
    int buf1[10] = {1,2,3,4,5,6,7};
    int buf2[10];
    memcpy(buf2, buf1, sizeof(buf1));
    //将buf1中的内容拷贝到buf2中
    return 0;
}
int memsetUsing(){
    int buf1[10] = {1,2,3,4,5,6,7,8};
    int buf2[10];
    memmove(buf2, buf1, sizeof(buf1));
    //尽量用这个做内存拷贝
    return 0;
}
void pointArrayTests1(){
    //指针数组
    int *p1[3];
    int a = 0;
    int b = 1;
    int c = 3;
    p1[0] = &a;
    p1[1] = &b;
    p1[2] = &c;
    //数组指针
    
    int buf[2][3] = {{1,2,3}, {4,5,6}};
    int (*p2)[3] = buf;
    printf("%p\n,%p\n,%p\n,%p\n,%p\n", buf, buf[0], &buf[0][0], p2, *p2);
    p2++;
    printf("%p\n,%p\n,%p\n,%p\n,%p\n", buf, buf[1], &buf[1][1], p2, (*p2+1));
    printf("%p\n,%p\n,%p\n,%p\n,%p\n", buf, buf[1], &buf[2][0], p2, *(p2+1));
}

//*********************point array*******************************
void pointArrayTests2(){
    int *a[10];
    short *b[20];
    int a1;
    short b1;
    a[0] = &a1;
    b[0] = &b1;
}
void multi_levelpoint(){
    int a = 10;
    int *p = &a;
    int **pp = &p;
    **pp = 2;
    
    int ***ppp = &pp;
    ***ppp = 20;
    
}
//*********************point review again*******************************
void wildpointtest(){
    int a = 10;
//    int *p = &a;
//    int *p; p = &a; *p = 10;
//    int *p; *p = 10; 直接给没有设置地址的指针赋值,会出现野指针
    

}

void constintandintconstTest(){
    int a = 10;
    int b = 20;
    int *const p1 = &a;
    //p1不能修改地址了,但是可以修改值
//    p1 = &b;
    
    const int *p2 = &a;
    //不能修改值了
//    *p2 = 10;
    p2 = &b;
}

void pointArrayTests3(){
    int a = 0x12345678;
    char *p = &a;
    printf("%x,%x,%x,%x,%x\n", *p, *(p+1), *p+1,*(p+2),*p+2);
    printf("%x,%x,%x,%x,%x\n", p[0], p[1], p[1], p[2], p[2]);
    printf("%p,%p,%p,%p,%p\n", p, p+1, p+2, p+3,p+4);
    printf("%p,%p,%p,%p,%p\n", &p[0], &p[1], &p[1], &p[2], &p[2]);
    
    printf("%p, %p, %p\n", p+1, &p[1], (int *)p + 1);
    //*p 在这里代表 指向内存的值
    //
    printf("%d", a);
}

void pointAssignment(){
    int buf[] = {1, 2, 3, 4, 5, 6, 7};
    int *p = buf;
    p[5] = 2;
    p += 5;
    *p = 2;
    *(p+3) = 12;
}
//*****************pointArray***********************************
void pointArrayTests4(){
    int buf[2][3] = {{1,2,3}, {4,5,6}};
    int (*p) [3];
    p = &buf[0];
    p = buf;
    
}
void pointArrayTests5(){
    int buf[3][5] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
    int (*p) [5] = buf;
    int sum = 0;
    for (int i = 0 ;i < 3; i++) {
        for (int j = 0; j < 5; j++) {
            printf("buf[%d][%d] = %d\n", i, j, *(*(p + i) + j));
            printf("buf[%d][%d] = %d\n", i, j, *(*(&p[0] + i) + j));
            printf("buf[%d][%d] = %d\n", i, j, *((&p[0][0] + i * 5) + j));
            printf("buf[%d][%d] = %d\n", i, j, p[i][j]);
            sum += *(*(p + i) + j);
        }
    }
    printf("%d", sum);
}

//*********************point as func param*******************************
void test(int *n){
    (*n)++;
}
void swap(int *a, int *b){
//    int *t = a;
//    a = b;
//    b = t;
    
    int tmp = *a;
    *a = *b;
    *b = tmp;
    printf("a = %d, b = %d", *a, *b);
}
void set_array(int buf[]){
    printf("buf = %ld\n", sizeof(buf));
    *buf = 100;
    buf++;
    *buf = 200;
}
void print_array(const int * buf, int n){
    int i;
    for (i = 0; i < n; i++) {
        printf("%buf[%d] = %d\n", i, buf[i]);
    }
}

void print_buf1(int (*p)[5], int a, int b){
    int i;
    int j;
    for(i = 0; i < a ;i++){
        for (j= 0; j<b ; j++) {
            printf("p[%d][%d] = %d", i, j, *(*(p + i)+j));
        }
    }
}

void mystrcat(char *s1,const char *s2){
    int len = 0;
    char *s =s1;
    while (*s) {
        len++;
        s++;
//        printf("%s", s);
    }
    while (*s2) {
        *s = *s2;
        s++;
        s2++;
    }
    printf("%s", s1);
//    int len = 0;
//    while (s2[len]) {
//        len++;
//    }
//    while (*s1) {
//        s1++;
//    }
//    int i;
//    for (i = 0; i < len; i++) {
//        *s1 = *s2;
//        s1++;
//        s2++;
//    }
//    printf("%s", s1);
}


char *mystrchr(char *s, char c){
    while (*s) {
        if (*s == c) {
            return s;
        }
        else{
            s++;
        }
    }
    return NULL;
}

char *mystrstr(char *s, char *str){
    while (*s) {
        if (s == str)
            return s;
        s++;
    }
    return NULL;
}
//*****************字符指针与字符串***********************************
void myprint_array(int *p, int n){
    int i;
    for (i = 0; i < n; i++) {
        printf("%d", p[i]);
    }
}

void print_str(char *s){
    printf("%s", s);
    while (s) {
        printf("%c", *s);
        s++;
    }
}

void print_str_a(char *s, int n){
    int i;
    for (i = 0; i < n; i++) {
        printf("%c", s[i]);
    }
}

int main0001(){
    char s[] = "hello world";
    s[3] = 0;
    print_str_a(s, sizeof(s));
//    return 0;
    int array[10] = {1,2,3,4,5,6,7,8,9,10};
    print_array(array, sizeof(array));
    
    char buf[100] = "hello world";
    char *p = buf;
    p += 5;
    *p = 'x';
    p[3] = 'c';
    printf("%s", p);
    return 0;
}

//****************************************************
int main(int argc, const char * argv[]) {
    /**
     int array[100] = {1,2,3,4,5};
     int *p = array;//数组名取地址
     printf("%p,%lu\n", p, p);
     p++;//移动了一个int的内存大小
     printf("%p,%lu\n", p, p);
     unsigned long i = 0x7fff5fbff5d0;
     printf("%lu", i);
     */
    
    //int *p;
    //p = array;
    /**
     *p = 100;将100赋值给指针p指向的内存;
     p = 100;将整数100赋值给变量p,指针本应指向一个有效地址,这种写法错误
     */
    
    /**
     int i = 300;
     p = &i;
     */
    
    /**
     void *p
     */
    
    /**
     NULL
     当一个指针不指向任何一个有效地址的时候,我们应该把指针设置位NULL
     */
    
    /**
     野指针:没有指向任何一个变量地址的指针叫野指针,避免用到野指针
     int *p;
     *p = 100;//给野指针内存赋值,程序运行结果是未知的
     没有赋值,地址随机,无法操作
     */
    
    /**
     指针兼容性
     指针之间赋值笔普通数据类型赋值检测更为严格
     int i = 0x12345678;
     unsigned char *p = (unsigned char *)&i;
     printf("%x,%x,%x,%x\n", p[0], p[1],p[2], p[3]);
     */
    
    /**
     常量指针
     int i = 10;
     const int *p = &i;
     *p = 20;//提示readonly
     printf("%d\n", *p);
     */
    
    /**
     指针常量与
     int i = 10;
     int j = 20;
     int *const p = &i;
     *p = 100;
     p = &j;//提示不能修改地址,只能指向初始化时候指向的变量地址,不可以修改
     printf("%d", *p);
     */
    
    /**
     int array[100] = {0};
     int *p = array;
     p[0] = 20;
     //区别
     sizeof(array) = ?   ......400
     sizeof(p) = ?   ......4
     指针可以代表数组加下标,或者加加减减访问
     
     */
    
    /**
     int array[100] = {0};
     int *p = array;
     p[0] = 20;
     *p = 30;
     printf("%d,%p\n", p[0], p);
     p++;//移动了int类型的字节数
     *p = 40;
     printf("%d,%p\n", p[0], p);
     p++;
     *p = 50;
     printf("%d,%p\n", p[0], p);
     int len = 0;
     while (array[len]) {
     len++;
     }
     len--
     for (int i = 0; i <= len; i++) {
     printf("array[%d] = %d,\n", i, array[i]);
     }
     */
    
    /**
     指针也是可以比较的
     */
    
    /**
     int *s[10] = {0};  //定义了一个数组
     int (*p)[10] = 0; //定义了一个指针变量,指向int【10】这么大的一种数据类型
     //sizeof(s) = 40;
     //sizeof(p) = 4
     
     int array[3][4] = {0};
     int *p1 = array; //int *这种类型的指针适合指向一维数组
     int (*p2)[4] = array;
     //对于array[0]来讲
     //sizeof(array[1]) == 16;
     p2++;//在内存中会移动16个字节
     //p2[0] == array; 指向首地址
     //p2[1] == array[1];
     //p2[2] == array[2];
     //p2[1][2] == array[1][2];
     */
    
    /**
     指针作为函数参数
     1>
     int i = 0;
     test(&i);
     
     void test(int *p){
     *p = 100;
     }
     2>
     int array[10] = {1,2,3,4,5,6,7,8,9,10};
     test1(array);
     void test1(int p[]){//参数可添加const,设为只读
     printf(p[i]);
     }
     3>
     int array[10] = {1,2,3,4,5,6,7,8,9,10};
     int *p = array;
     sizeof(p); //只会打印单个元素的字节大小
     sizeof(array); //打印整个数组占用的字节大小
     */
    
    /**
     二维数组作为函数参数
     1>
     void test(int (*p)[3]){
     
     }
     //如果要调用以上函数需要定义一个怎样的数据类型呢
     int array[x][3] = {0};
     
     //传入的指针必须加括号
     
     2>
     */
    
    /**
     指向函数的指针
     void test(int *p){
     ...
     }
     
     void (*p1)(int *p);//定义了一个指针指向函数的指针变量
     p1 = test;
     int a = 10;
     p1(&a);//通过指针简介调用这个方法
     
     */
    
    /**
     #include <string.h>
     memcpy memset memmov
     memcpt(array2, array1, sizeof(array1));//把数组1的内容拷贝到数组2中
     memset(array1, 0, sizeof(array1));//清0
     */
    
    /**
     指针小结
     int (*p)[10] //定义一个指向int[10]类型的指针变量
     int func()
     int *func() //定义一个函数 返回值是int*
     int (*p)() //定义一个返回值为int的函数指针
     int **p //定义一个指向指针的指针,二级指针
     */
    
    /**
     test
     void test(const char *s){
     printf("%s\n",s);
     }
     void test1(char *s){
     s[0] = 'a';
     s[1] = 'b';
     }
     int main(
     1>test["hello world"];
     2>char s[] = "hello world";
     test1(s);
     //处理字符串和处理数组是一样的
     }
     */
    
    /**
     //调用main函数的指针
     argc , 系统参数数量
     argv[],
     for(i = 0; i < argc, i++){
     printf("argv[%d] = %s\n", i, argv[i]);
     //打印参数的值
     }
     
     */
    //test5();
    //ip2s(0x12121212);
    //int s[] = {50, 22, 13, 74, 35, 16, 7,81,29,10};
    //printf("max = %d", smax(s));
//    tranChinese();
//    printf("%d", main002());
//    pointArrayTests5();
//    swap(&a, &b);
    char s1[] = "test";
    char s2[] = "mesdasd";
    mystrcat(s1, s2);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值