Med*_*eda 9 c pointers c99 c89
我刚刚开始学习C所以请善待.从我到目前为止读到的关于指针的内容:
int * test1; //this is a pointer which is basically an address to the process
//memory and usually has the size of 2 bytes (not necessarily, I know)
float test2; //this is an actual value and usually has the size of 4 bytes,
//being of float type
test2 = 3.0; //this assigns 3 to `test2`
Run Code Online (Sandbox Code Playgroud)
现在,我不完全理解:
*test1 = 3; //does this assign 3 at the address
//specified by `pointerValue`?
test1 = 3; //this says that the pointer is basically pointing
//at the 3rd byte in process memory,
//which is somehow useless, since anything could be there
&test1; //this I really don't get,
//is it the pointer to the pointer?
//Meaning, the address at which the pointer address is kept?
//Is it of any use?
Run Code Online (Sandbox Code Playgroud)
同理:
*test2; //does this has any sense?
&test2; //is this the address at which the 'test2' value is found?
//If so, it's a pointer, which means that you can have pointers pointing
//both to the heap address space and stack address space.
//I ask because I've always been confused by people who speak about
//pointers only in the heap context.
Run Code Online (Sandbox Code Playgroud)
很好的问题。
你的第一个块是正确的。指针是保存某些数据地址的变量。该指针的类型告诉代码如何解释该指针所保存的地址的内容。
构造:
*test1 = 3
Run Code Online (Sandbox Code Playgroud)
称为指针的引用。这意味着,您可以像普通变量一样访问指针指向的地址并对其进行读写。笔记:
int *test;
/*
* test is a pointer to an int - (int *)
* *test behaves like an int - (int)
*
* So you can thing of (*test) as a pesudo-variable which has the type 'int'
*/
Run Code Online (Sandbox Code Playgroud)
以上只是我使用的一个助记装置。
您很少为指针分配数值...也许如果您正在为具有一些“众所周知”内存地址的特定环境进行开发,但在您的级别,我不会太担心那。
使用
*test2
Run Code Online (Sandbox Code Playgroud)
最终会导致错误。您会尝试遵循不是指针的东西,因此您可能会遇到某种系统错误,因为谁知道它指向哪里。
&test1and&test2实际上是指向test1和 的指针test2。
指向指针的指针非常有用,搜索指向指针的指针将引导您找到一些比我更好的资源。