指向指针的指针在函数中参数传递并返回

1.demo1

#include <stdio.h>

typedef struct my{
  int tan;
  int kai;
  //struct my * next;
}mystruct;

void test(mystruct *tk);
void test1(mystruct **tk1);

int main(){
  mystruct list[128];
  //list = (mystruct *)malloc(sizeof(mystruct));
  //list->tan = 1;
  //list->kai = 1;
  //list->next = NULL;
  list[0].tan = 1;
  list[0].kai = 1;
  printf("list is 0x%lx\n",list);
  printf("list[0].tan is %d\n",list[0].tan);
  printf("list[0].kai is %d\n",list[0].kai);
  printf("&list is 0x%lx\n",&list);
  test(list);
  printf("list is 0x%lx\n",list);
  printf("list[1].tan is %d\n",list[1].tan);
  printf("list[1].kai is %d\n",list[1].kai);
  //char *b = NULL;
  //b = (char)malloc
  //int b = 1;
  //printf("&b is 0x %lx\n",&b);
  //int *c = &b;
  //printf("c is 0x %lx\n",c);
  //test(c);
  //printf("*c is %d\n",*c);
  return 0;
}

void test(mystruct *tk){
  //printf("tk is ox%lx\n",tk);
  //tk ++;
  //printf("tk is 0x%lx\n",tk);
  //printf("*tk is 0x %lx\n",*tk);
  //tk->tan = 2;
  //tk->kai = 2;
  //printf("result \n");
  test1(&tk);
  //tk[2].tan = 2;
  //tk[2].kai = 2;
  //printf("tk is 0x%lx\n",tk);
  //printf("&tk is 0x%lx\n",&tk);
  //*a = 2;
}


void test1(mystruct **tk1){
  (*tk1) += 1;
  (*tk1)->tan = 2;
  (*tk1)->kai =2 ;
}
gcc -o 1 1.c
list is 0x7fffc736bed0
list[0].tan is 1
list[0].kai is 1
&list is 0x7fffc736bed0
list is 0x7fffc736bed0
list[1].tan is 2
list[1].kai is 2

2.demo2

#include <stdio.h>

void test(char **b);
int main(){
  char * a = NULL;
  a = (char *)malloc(sizeof(char));
  printf("a is 0x%lx\n",a);
  test(&a);
    printf("a is 0x%lx\n",a);

}

void test(char **b){
  printf("*b is 0x%lx\n",*b);
  *b = (char*)malloc(sizeof(char));
  printf("*b is 0x%lx\n",*b);
}
gcc -o 2 2.c
a is 0x74f010
*b is 0x74f010
*b is 0x74f030
a is 0x74f030

3.demo3

vi 3.c

#include <stdio.h>

void mymalloc(char **a);
void myfree(char **a);

int main()
{
  char *a = NULL;
  mymalloc(&a);
  printf("a is 0x%x,strlen(a) is %d\n",a,strlen(a));
  myfree(&a);
  return 0;
}

void mymalloc(char **a)
{
  *a = (char *)malloc(10);
  strcpy(*a,"adbcde");
}

void myfree(char **a)
{
  free(*a);
}
gcc -g -o 3 3.c
a is 0x16e8010,strlen(a) is 6

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值