无法理解指针的行为 [英] Unable to understand the behavior of pointer

查看:78
本文介绍了无法理解指针的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int  * z = NULL; 
int checkaddress( int * a)
{
int * b =( int *)malloc( sizeof int ));
fprintf(stderr, b:%p \ n,b);
fprintf(stderr, a:%p \ n,a);
a = b;
fprintf(stderr, a:%p \ n,a);
fprintf(stderr, z:%p \ n,z);
}

int main()
{
checkaddress(z);
fprintf(stderr, z:%p \ n,z);
return 1 ;
}





我的尝试:



我是c语言的新手。我遇到了这种我很难理解的行为。我传递z的地址作为函数checkAddress的参数。理想情况下,a应该包含z的地址。现在我的理解是否指针b被分配给a。那么z也应该包含b的地址。虽然情况并非如此。我的理解一定有一些差距。任何人都可以解释一下这种行为。

解决方案

在本文中,请参阅指针指针:指针指针和指针引用 [ ^ ]

您正在将指针z的实际值传递给您的函数(这是它包含的地址值)。因此,具有该值的函数可以改变地址指向的地址而不是地址本身。要改变地址值,我们需要一个指向IT的指针,这是一个指向指针的指针(在这种情况下指向z的指针)。这个例子说明:



  #include   <   stdio.h  >  / * printf,scanf,NULL * / 
#include < stdlib.h > / * malloc, free,rand * /

int * z = NULL;

int checkaddress( int * a)
{
int * b =( int *)malloc( sizeof int ));
fprintf(stderr, b:%p \ n,b);
fprintf(stderr, a:%p \ n,a);
a = b;
fprintf(stderr, a:%p \ n,a);
fprintf(stderr, z:%p \ n,z); // z保持不受影响。正确。
free(b);
return 0 ;
}

int checkaddress2( int ** A)
{
int * B =( int *)malloc( sizeof int ));
fprintf(stderr, B:%p \ n,B);
fprintf(stderr, * A:%p \ n,* A) ; // 获取值A指向
* A = B; // 设置A指向B
fprintf(stderr, * A:%p \ n,* A);
fprintf(stderr, z:%p \ n,z); // 现在z是B的值
// 免费(B); //重要提示:分配的内存必须在某个时候免费。
return 0 < /跨度>;
}

int main()
{

fprintf(stderr, z:%p在\ n之前,z);
checkaddress(z);
checkaddress2(& z);
fprintf(stderr, z:%p After\\\
,z);
return 1 ;

}

< /stdlib.h>< /stdio.h>


阅读参考书。

https://hassanolity.files.wordpress.com/2013 /11/the_c_programming_language_2.pdf [ ^ ]

http:// www .ime.usp.br / ~pf / Kernighan-Ritchie / C-Programming-Ebook.pdf [ ^ ]



您应该学会使用调试器尽快。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

int * z=NULL;
int checkaddress(int * a)
{
 int * b = (int *)malloc(sizeof(int));
 fprintf(stderr,"b:%p\n",b);
 fprintf(stderr,"a:%p\n",a);
 a = b;
 fprintf(stderr,"a:%p\n",a);
 fprintf(stderr,"z:%p\n",z);
}

int main()
{
 checkaddress(z);
 fprintf(stderr,"z:%p\n",z);
 return 1;
}



What I have tried:

I am new to c language. I came across this behavior which I found difficult to understand. I am passing the address of z as an argument of function checkAddress. So ideally the a should contain the address of z. Now as far my understanding if the pointer b is assigned to a. Then z should also contain the address of b. Though that is not the case. There must be some gap in my understanding. Can anybody explain me this behavior.

解决方案

In this article see pointers to pointers: Pointer to Pointer and Reference to Pointer[^]
You are passing the actual value of the pointer z to your function (that is the address value it contains). Thus having this value the function may alter whatever that address points to but not the address itself. To alter the address value we need a pointer to IT, that is a pointer to a pointer ( a pointer to z in this case). This example illustrates that:

#include <stdio.h>      /* printf, scanf, NULL */
#include <stdlib.h>     /* malloc, free, rand */

int * z = NULL;

int checkaddress(int * a)
{
	int * b = (int *)malloc(sizeof(int));
	fprintf(stderr, "b:%p\n", b);
	fprintf(stderr, "a:%p\n", a);
	a = b;
	fprintf(stderr, "a:%p\n", a);
	fprintf(stderr, "z:%p\n", z); // z remains unaffected. Correct.
	free(b);
	return 0;
}

int checkaddress2(int ** A)
{
	int * B = (int *)malloc(sizeof(int));
	fprintf(stderr, "B:%p\n", B);
	fprintf(stderr, "*A:%p\n", *A); // get the value A points to
	*A = B; // Set what A points to to B
	fprintf(stderr, "*A:%p\n", *A);
	fprintf(stderr, "z:%p\n", z); // Now z is the value of B
	// free(B); // Important: allocated memory must be free'd at some point.
	return 0;
}

int main()
{

	fprintf(stderr, "z:%p Before\n", z);
	checkaddress(z);
	checkaddress2(&z);
	fprintf(stderr, "z:%p After\n", z);
	return 1;

}

</stdlib.h></stdio.h>


Read reference book.
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


这篇关于无法理解指针的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆