实践中二级指针的用法1笔试题获得内存2操作string赋值

直接上TOUCH代码,读取配置列表获得数据的函数。

#include <stdio.h>
typedef struct
{
    char                 pair_pwd[9];
    char                 user_pwd[9];
}SystemInfoType;

static SystemInfoType     info={"hello","word"};
////////////////////////////此时全部变量也就确定了不能再main里面单独赋值,因为数组只有定义的时候可以赋值/////////////////

int cfg_read ( char mode , void **parma )
{
    int data = 0;

    switch(mode)
    {
        case 1:
        {
            *parma = info.pair_pwd;
             data = 1;
        }break;
        case 2:
        {
           *parma = info.user_pwd;
            data = 2;
        }break;
       case 3:
        {
            data = 3;
        }break;
    }
    return data;
}
//////////////////////////这就是一个读配置函数////////////////////////////
int main()
{
    char *userPwd;
   // info.pair_pwd={"hello"};
    //info.user_pwd={"word"};
    cfg_read(1 , (void **)&userPwd );
    printf("%s\n", userPwd);//---------------hello           
   printf("%d\n",cfg_read(3,NULL));//----------------3
  return 0;}


核心代码:

 1---一个待赋值的指针   char *userPwd;

 2---调用函数                cfg_read(1 , (void **)&userPwd );

3--函数中的写法           *parma = info.pair_pwd;

(数组赋值的办法 这样可以不要strcpy)

++++++++++++++++++++++++++20180712补充++++++++++++++++

我自己写了一个代码,看看问题和面试的几乎一样。

char *ReportName=NULL;

printf("1---%s\n",ReportName);
SDConfig_Logic(ReportName);

printf("2---%s\n",ReportName);

这样是不行的,打印1 2都是NULL,也就是你传一个str进去赋值,是不行的。


下面全部的写法 都是不行的 输出都是1NULL 2NULL
#include<stdio.h>


char *p1=NULL;
 void GetPoint(char *p)
 {

    //char *tem="hello";
    //static char *tem="hello";
    //char tem[10]="hello";
    static char tem[10]={"hello"};
    p=tem;
 }
int main()
{
printf("1%s\n",p1);
GetPoint(p1);
printf("2%s\n",p1 );
}

修改办法:

#include<stdio.h>

char *p1=NULL;
 void GetPoint(char **p)
 {

    //char *tem="hello";
  //  static char *tem="hello";
    
    //char tem[10]="hello";
    //static char tem[10]={"hello"};
    *p=tem;
 }
int main()
{
printf("1%s\n",p1);
GetPoint((char**)&p1);
printf("2%s\n",p1 );
}

核心代码:

GetPoint((char**)&p1);------(char**)可以不要

 void GetPoint(char **p)

*p=tem;

注意到上面的4个句子,有一个是不行的!!char tem[10]="hello";此时是乱码!!!出栈的时候销货啦!!


补充以前学习的笔记

下面的程序 编译OK  一RUN就段错误
#include<stdio.h>
#include<string.h>

void load_name(char *A,char *B)
{
    int tem=strlen(A);
    if( tem>16 )
    {
        B=A+tem-16;
    }
    else
    {
        B=A;   
    }
}

int main()
{
    char *ID="012345678912345";
    char *name=NULL;
    load_name(ID,name);
    printf("%s\n",name);     
}
因为都是指针 都没有内存 直接等于 是可以的 但是要数组 要memcpy
++++++++++++++++++++++++++++
void load_name(char *A,char *B)
{
    int tem=strlen(A);
    if( tem>16 )
    {
        B=A+tem-16;
    }
    else
    {
        B=A;    
    }
}

int main()
{
    char *ID="012345678912345";
    char name[16]={0};
    load_name(ID,name);
    printf("%s\n",name);     
}
为什么打印出来的是一行空空的
因为直接等 是不行的!!
深入思考1:
在主函数中修改
int main()
{
    char *ID="012345678912345";
    char name[16]={'A','B','C'};
    load_name( name,ID);
    printf("%s\n",name);     
}
哈哈 指针是可以直接等于的 现在打印出ABC

深入思考2:
需要用二级指针了
#include<stdio.h>
#include<string.h>

void load_name(char **A,char **B)
{
    int tem=strlen(*A);
    if( tem>16 )
    {
        *B=*A+tem-16;
    }
    else
    {
        *B=*A;   
    }
}

int main()
{
    char *ID="012345";
    //char name[16]={0};
    char *name=NULL;
    load_name(&ID,&name);
    printf("%s\n",name);     
}
+++++++++++++++++++++++++++



OK 算法
  #include<stdio.h>
#include<string.h>



void load_name(char *A,char *B)
{
    int tem=0;
    if( strlen(A)>16 )
    {
       tem= strlen(A)-16;
       memcpy(B,A+tem,16);
    }
    else
    {
       memcpy(B,A, strlen(A));
    }

}

int main()
{
    char *ID="012345";
    char name[16]={0};
    load_name(ID,name);
    printf("%s\n",name);     
}









猜你喜欢

转载自blog.csdn.net/weixin_42381351/article/details/80982528