A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

小牛爱学习

初级黑马

  • 黑马币:33

  • 帖子:17

  • 精华:0

© 小牛爱学习 初级黑马   /  2017-3-28 09:09  /  678 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

对于字符串类型的指针
*myp1=p1;*myp2=p2;和myp1=&p1;myp2=&p2;有什么区别

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
int Testbin( char** myp1,int *mylen1, char **myp2, int *mylen2 ){
        char *p1=(char *)malloc(100);
        char *p2 =(char *)malloc(200);
        *myp1 = p1;
        *myp2 = p2;
        //myp1 = &p1;
        //myp2 = &p2;
        strcpy(p1, "hello my best friend");
        strcpy(p2, "hi how is it going");
       
        *mylen1 = strlen(p1);
        *mylen2 = strlen(p2);
        return 0;
}

int main(){
        char        *p1 = NULL;
        char        *p2 = NULL;
        int                len1;
        int                len2;
        Testbin(&p1, &len1, &p2, &len2);
        printf("len1:%d\n", len1);
        printf("len2:%d\n", len2);
        printf("p1:%s\n", p1);
        printf("p2:%s\n", p2);
       
        return 0;
}

运行结果
当是:*myp1 = p1;*myp2 = p2;时
len1:20
len2:18
p1:hello my best friend
p2:hi how is it going
请按任意键继续. . .
当是:myp1 = &p1;myp2 = &p2;时
len1:20
len2:18
p1:(null)
p2:(null)
请按任意键继续. . .
请问为什么,求大神解惑

2 个回复

倒序浏览
回复 使用道具 举报
只是想求解,有不当的地方请见谅
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马