指针传参和返回问题
在主函数把一个非空指针传到子函数里,让他指向新的地址,然后返回,在主函数里这个指针指向什么地方?
#include <stdio.h>
int *test(int *a)
{
a=NULL;
return a;
}
void main()
{
int b=3;
int *a=&b;
printf("%d\n",test(a));
printf("*a=%d a=%d\n",*a,a);
getchar();
}
#include <stdio.h>
int *test(int *a)
{
printf("test:&a=%d\n",&a);
a=NULL;
return a;
}
void main()
{
int b=3;
int *a=&b;
printf("main:&a=%d\n",&a);
printf("%d\n",test(a));
printf("*a=%d a=%d\n",*a,a);
getchar();
}
#include <stdio.h>
int *test(int *a)
{
a=NULL;
return a;
}
void main()
{
int b=3;
int *a=&b;
printf("%d\n",test(a)); // 函数返回的a
printf("*a=%d a=%d\n",*a,a); // main 中原来的 a, 与函数无关
getchar();
}
#include <stdio.h>
int glo = 9;
int *test(int *a)
{
//a=(void*)6;
//a=0;
a=&glo;
//a=NULL;
return a;
}
void main()
{
int b=3;
int *a=&b;
printf("%d\n",test(a));
printf("%d\n",*test(a));
printf("*a=%d a=%x\n",*a,a);
printf("a = %p\n",a);
getchar();
}
//int*看做一个整体
typedef int* PINT;
PINT test(PINT a)
{
a=NULL;
return a;
}
int b = 0x12345678;
PINT p = &b;
test(p); //参数a是p的拷贝、副本, 修改该副本不会影响原本, 这就是所谓的值传递