函数中的C指针赋值
为什么我不能在函数中指定一个点。正如您在下面的代码中注意到的。在函数返回后,我无法分配指向正确地址的指针p1。但是使用全局指针*p,我可以存储地址信息函数中的C指针赋值,c,function,pointers,variable-assignment,C,Function,Pointers,Variable Assignment,为什么我不能在函数中指定一个点。正如您在下面的代码中注意到的。在函数返回后,我无法分配指向正确地址的指针p1。但是使用全局指针*p,我可以存储地址信息 #include <stdio.h> #include <stdlib.h> int *p = NULL; void test(int * pt1, int**pt2){ p = (int*)malloc(sizeof(int)); pt1 = p; *pt2 = p; print
#include <stdio.h>
#include <stdlib.h>
int *p = NULL;
void test(int * pt1, int**pt2){
p = (int*)malloc(sizeof(int));
pt1 = p;
*pt2 = p;
printf("p points to %p\n", p);
printf("pt1 points to %p\n", pt1);
printf("pt2 points to %p\n", *pt2);
}
int main(void) {
int *p1 = NULL;
int *p2 = NULL;
printf("p points to %p\n", p);
printf("p1 points to %p\n", p1);
printf("p2 points to %p\n", p2);
test(p1, &p2);
printf("p points to %p\n", p);
printf("p1 points to %p\n", p1);
printf("p2 points to %p\n", p2);
return 0;
}
您需要按值传入p1,因此更改仅在该函数的范围内可见。传递一个指向该指针的指针,就像对p2所做的那样,这样就可以了
#include <stdio.h>
#include <stdlib.h>
int *p = NULL;
void test(int **pt1, int**pt2){
p = (int*)malloc(sizeof(int));
*pt1 = p;
*pt2 = p;
printf("p points to %p\n", p);
printf("pt1 points to %p\n", pt1);
printf("pt2 points to %p\n", *pt2);
}
int main(void) {
int *p1 = NULL;
int *p2 = NULL;
printf("p points to %p\n", p);
printf("p1 points to %p\n", p1);
printf("p2 points to %p\n", p2);
test(&p1, &p2);
printf("p points to %p\n", p);
printf("p1 points to %p\n", p1);
printf("p2 points to %p\n", p2);
return 0;
}
#包括
#包括
int*p=NULL;
无效测试(int**pt1、int**pt2){
p=(int*)malloc(sizeof(int));
*pt1=p;
*pt2=p;
printf(“p指向%p\n”,p);
printf(“pt1指向%p\n”,pt1);
printf(“pt2指向%p\n”,*pt2);
}
内部主(空){
int*p1=NULL;
int*p2=NULL;
printf(“p指向%p\n”,p);
printf(“p1指向%p\n”,p1);
printf(“p2指向%p\n”,p2);
测试(p1和p2);
printf(“p指向%p\n”,p);
printf(“p1指向%p\n”,p1);
printf(“p2指向%p\n”,p2);
返回0;
}
您正在通过值传递p1
,因此它不会在main
函数中更新。但是,您正在通过引用传递p2
(请注意,您编写了&p2
),因此可以对其进行更改。在test
内部,变量pt1
本身就是一个离散指针。也就是说,它不仅仅是p1
的别名,而是仅在调用生命周期内存在的副本
因此,您对它所做的任何分配都只在该调用期间存在,并且不会传播到调用之外。当您从test
返回时,指针pt1
不再存在,任何更改都不会复制回来
除了像使用pt2
那样使用额外的指针“层”,有时还可以使用返回值与更广泛的受众“共享”更改:
#include <stdio.h>
#include <stdlib.h>
int *p = NULL;
int *test(int * pt1, int**pt2){
p = (int*)malloc(sizeof(int));
pt1 = p;
*pt2 = p;
printf("p points to %p\n", p);
printf("pt1 points to %p\n", pt1);
printf("pt2 points to %p\n", *pt2);
return pt1;
}
int main(void) {
int *p1 = NULL;
int *p2 = NULL;
printf("p points to %p\n", p);
printf("p1 points to %p\n", p1);
printf("p2 points to %p\n", p2);
p1=test(p1, &p2);
printf("p points to %p\n", p);
printf("p1 points to %p\n", p1);
printf("p2 points to %p\n", p2);
return 0;
}
#包括
#包括
int*p=NULL;
int*测试(int*pt1,int**pt2){
p=(int*)malloc(sizeof(int));
pt1=p;
*pt2=p;
printf(“p指向%p\n”,p);
printf(“pt1指向%p\n”,pt1);
printf(“pt2指向%p\n”,*pt2);
返回pt1;
}
内部主(空){
int*p1=NULL;
int*p2=NULL;
printf(“p指向%p\n”,p);
printf(“p1指向%p\n”,p1);
printf(“p2指向%p\n”,p2);
p1=试验(p1和p2);
printf(“p指向%p\n”,p);
printf(“p1指向%p\n”,p1);
printf(“p2指向%p\n”,p2);
返回0;
}
在C语言中,一切都是按值传递的。指针也不例外。哦,看起来75英寸的钢琴家比我快!非常感谢你。我现在明白了。
#include <stdio.h>
#include <stdlib.h>
int *p = NULL;
int *test(int * pt1, int**pt2){
p = (int*)malloc(sizeof(int));
pt1 = p;
*pt2 = p;
printf("p points to %p\n", p);
printf("pt1 points to %p\n", pt1);
printf("pt2 points to %p\n", *pt2);
return pt1;
}
int main(void) {
int *p1 = NULL;
int *p2 = NULL;
printf("p points to %p\n", p);
printf("p1 points to %p\n", p1);
printf("p2 points to %p\n", p2);
p1=test(p1, &p2);
printf("p points to %p\n", p);
printf("p1 points to %p\n", p1);
printf("p2 points to %p\n", p2);
return 0;
}