02 二级指针

二级指针

void getMem(char **p2){
    *p2 = 400;
}


void main(){
    char *p1 = nullptr;
    char **p2 = nullptr;
    
    p1 = 0x11;
    p2 = 0x22;
    
    // 直接修改p1的值
    p1 = 0x111;
    
    // 间接修改p1的值
    p2 = &p1;
    *p2 = 100; // p1 = 100
    
    getMem(&p1) // p1 = 400
}
int getMem(char **myp1, int *mylen1, char **myp2, int *mylen2){
    int ret = 0;
    char *tmp1, *tmp2;
    
    tmp1 = (char *)malloc(100);
    strcpy(tmp1, "123124");
    *mylen1 = strlen(tmp); // 1级指针 
    *myp1 = tmp1; // 二级指针
    
    tmp2 = (char *)malloc(200);
    strcpy(tmp2, "1adf24");
    *mylen2 = strlen(tmp2);
    *myp2 = tmp2;
    
    return ret;
}

int main(){
    int ret = 0;
    
    char *p1 = nullptr;
    int len1 = 0;
    char *p2 = nullptr;
    int len2 = 0;
    
    ret = getMem(&p1, &len1, &p2, &len2);
   	
    if(p1 != nullptr){
        free(p1);
        p1 = nullptr;
    }
    if(p2 != nullptr){
        free(p2);
        p2 = nullptr;
    }
    
    return ret;
}

间接赋值应用场景

在这里插入图片描述

1 C语言的字符串以零结尾的字符串

2 C语言中没有字符串类型 通过字符数组模拟字符串

3 字符串内存分配 堆 栈 全局区

[] 和 *p

buf[i] == *(buf + i); 

buf[5] 是一个常量指针

void copy_str(char *from, char *to){
    for(;*from!="\0";from++; to++){
        *to = *from;
    }
    *to = "\0";
}

void copy_str(char *from, char *to){
    for(;*from!='\0';){
        *to++ = *from++; // 先执行*, 再执行++
    }
    *to = '\0';
}

void copy_str(char *from, char *to){
    while((*to = *from)!='\0'){
        from++;
        to++;
    }
}

void copy_str(char *from, char *to){
    while((*to++ = *from++)!='\0'){
        ;
    }
}

void copy_str(char *from, char *to){
    
    // 防止错误
    if(to == nullptr || from == nullptr)
        return -1;
    while(*to++ = *from++){
        ;
    }
}

void main(){
    char *from = "abcdefg";
    char buf2[100];
    copy_str21(from, buf2);
    
}

字符串模型

// char *p = "abcd1213124123abcd213214ksdaaa";
// 求字符串中 abcd出现的次数

int getCount(char *mystr, char *sub, int *ncount){
    
    int ret = 0;
    int tmpCount = 0;
    char *p = mystr;
    
    if(mystr == nullptr || sub == nullptr || ncount == nullptr){
        ret = -1;
        // 输出error
        return ret;
    }
    
    do{
    	p = strstr(p, sub);
        if(p!=nullptr){
            tmpCount++; //
            p = p + strlen(sub);  // 指针达到下次查找的条件
        }
        else{
            break;
        }
    }while(*p!='\0');
    
    *ncount = tmpCount; // 间接修改
    return 0; 
}

void main(){
    int ret = 0;
    
    char *p = "abcd1213124123abcd213214ksdaaa";
    int count = 0;
    char sub[] = "abcd";
    
    ret = get(p, sub, &count);
    if(ret!=0){
        // 输出错误
    }
    
    int ncount = 0;
    char *p = "abcd1213124123abcd213214ksdaaa";
    
    do{
    	p = strstr(p, "abcd");
        if(p!=nullptr){
            ncount++; //
            p = p + strlen("abcd");  // 指针达到下次查找的条件
        }
        else{
            break;
        }
    }while(*p!='\0')
        
    while(p = strstr(p, "abcd")){
        ncount++;
        p = p + strlen("abcd");  // 指针达到下次查找的条件
        if(*p='\0')
            break;
    }
}

传递指针进行函数操作

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int delSpace(char *str, char *newstr){
        char *p = str;
        int i = 0;
        int j = strlen(p)-1;
        int ncount = 0;

        while(isspace(p[i]) && p[i]!='\0'){
                i++;
        }
        while(isspace(p[j]) && p[j]!='\0'){
                j--;
        }
        ncount = j-i+1;
        strncpy(newstr, p+i, ncount);
        newstr[j] = '\0';
}

void main(){
        char abc[]="  ad  ";  // 分配在临时区可以修改
        char ss[100]={0};
        int count = 0;
        getCount(abc, &count);
        delSpace(abc, ss);
        printf("count:%d\n", count);
        printf("ss:%s\n",ss);
}
                                                                                                                                     40,12         Bot

字符串反转

#include<stdlib.h>
#include<string.h>
#include<stdio.h>


int reverse(char *str){
        int length;
        char *p1;
        char *p2;

        length = strlen(str);
        p1 = str;
        p2 = str + length - 1;

        while(p1 < p2){
                char c = *p1;
                *p1 = *p2;
                *p2 = c;
                ++p1;
                --p2;
        }
}

int main(){
        char buf[] = "abcdefg";

        /******************
        int length = strlen(buf);

        char *p1 = buf;
        char *p2 = buf + length-1;

        while(p1 < p2){
                char c = *p1;
                *p1 = *p2;
                *p2 = c;
        while(p1 < p2){
                char c = *p1;
                *p1 = *p2;
                *p2 = c;
                ++p1;
                --p2;
        }

         ***********/
        reverse(buf);
        printf("buf:%s\n",buf);
}

栈字符反转(递归)

void reverse02(char *p){

        if(p==NULL){
                return;
        }
        if(*p == '\0'){
                return;
        }

        reverse02(p+1); // 让字符串每一个地址入栈
        printf("%c", *p);

}

void reverse03(char *p){

        if(p==NULL){
                return;
        }
        if(*p == '\0'){
                return;
        }
        reverse03(p+1);
        strncat(g_buf,p,1);
}

void reverse04(char *p, char *to){

        if(p==NULL || to == NULL){
                return;
        }
        if(*p == '\0'){
                return;
        }
        reverse04(p+1,to);
        strncat(to,p,1);
}

int main(){
        char buf[] = "abcdefg";
        memset(g_buf, 0, sizeof(g_buf));
        reverse03(buf);
        printf("g_buf:%s \n", g_buf);
    
    	{
                char big_buf[1024] ={0};
                reverse04(buf, big_buf);
                printf("big_buf:%s\n",big_buf);
        }
}

综合

#include<stdlib.h>
#include<stdio.h>
#include<string.h>


int delSpace(char *from, char *to){
    	if(from == NULL || to ==NULL) return -1;
        int i = 0;
        int j = strlen(from)-1;
        int n_count = 0;
        while(isspace(from[i])&&from[i]!='\0'){
                ++i;
        }
        while(isspace(from[j])&&from[j]!='\0'){
                --j;
        }
        n_count = j - i + 1;
        strncpy(to, from+i, n_count);
        to[j]='\0';
        return 0;

}

int getKeyByValue(char *keyvalbuf, char *keybuf, char *valbuf){
        // 1查找key是否在母串中
        // 2有无=
        // 3等号后去掉=
        int ret = 0;
        char *p = NULL;
        p = strstr(keyvalbuf, keybuf);
        if(p==NULL){
                return -1;
        }
        //移动辅助指针变量
        p = p + strlen(keybuf);
  
        p = strstr(keyvalbuf, "=");
        if(p==NULL){
                return -1;
        }
        p = p + strlen("=");

        ret = delSpace(p,valbuf);
        return ret;

}

int main(){
        int ret = 0;
        char buf[1024]={0};
        int buflen = 0;

        char *keyandvalue = "key2=   value2  ";
        char *key = "key2";

        ret = getKeyByValue(keyandvalue, key, buf);
        if(ret!=0){
                return ret;
        }

        printf("buf:%s \n",buf);
        return 0;

}

const 练习

const char *p; // 可以修改p, 不能修改p的内容 p = 1
char const *p; // 可以修改p的内容,不能修改p  p[1] = 'a'
const char * const p; // 只读

//c中可以用访问地址改值
const int a = 10; // a = 10
int *p = &a;
*p = 100; // a=100
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值