对于字符串类型的指针
*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)
请按任意键继续. . .
请问为什么,求大神解惑 |
|