指针值,指针地址,指针变量之间的区别 [英] Difference between pointer value, pointer address, pointer variable

查看:174
本文介绍了指针值,指针地址,指针变量之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个代码:

first code:

void Push(struct node** headRef, int data) {
struct node* newNode = malloc(sizeof(struct node));
newNode->data = data;
newNode->next = *headRef; // The '*' to dereferences back to the real head
*headRef = newNode; // ditto
}
void PushTest() {
struct node* head = BuildTwoThree();// suppose this returns the list {2, 3}
Push(&head, 1); // note the &
Push(&head, 13);
// head is now the list {13, 1, 2, 3}





第二代码:



second code:

void foo(char *ptr) {
    ptr++; // Move the pointer (but it's pass by value)
}
void bar() {
    char *str = "cat";
    char *ptr = str;
    foo(ptr);
    printf("%s\n", ptr); // Prints "cat"
}





我尝试过:





What I have tried:

i was confuse here in first code<pre>Push(&head, 1)

中混淆了它调用Push并给出头部地址,head是指向其中的struct节点的指针,它也指向另一个节点。

& head这里是指头部存储的头部/地址的地址?

它实际传递给推送功能



第二个代码,有foo(ptr),调用foo和传递ptr,ptr是一个指向字符串的指针,它在cat中指向c,但实际上传递给函数foo的是什么?

是cat中ptr /地址c的地址??

i与指针值,指针地址和指针变量地址本身混淆!!!!!

it calls Push and give head address, head is a pointer to a struct node that inside it, it also point to another node.
&head here means the address of head/address of what is store inside head?
what actually it pass to Push function

in second code, there is foo(ptr), calls foo and pass ptr, ptr is a pointer to a string and it point "c" in "cat", but actually what is being passed to function foo?
is it the address of ptr/address of "c" in "cat"??
i was confused with pointer value, pointer address, and pointer variable address itself!!!!!

推荐答案

OK, head 被声明为节点* ,这意味着 head 是一个包含指向节点对象的指针的变量。



想想汽车片刻。您可以在停车场停放车辆。如果你让我驾驶它,你不要告诉我这是红色福特嘉年华,注册号ABC 123,因为我必须搜索整个停车场寻找它 - 你告诉我它停在3级,排B,海湾17:3B17足以让我找到这辆车。

3B17是包含汽车的海湾地址 - 或指向汽车的指针。如果我移动车,我会返回一个不同的地址:4A12你可以找到这辆车然后回家。



同样,头部不包含列表中的第一个节点 - 它包含一个数字,告诉您该节点的位置:节点的地址。就像我一样,我通过去那个地址找到你的车。

要转到节点,你按照指针 - 这被称为解除引用指针 - 通过在前面加上'*'或使用 - >运营商。



要获取任何地址,请在前面添加'&':所以& head 为您提供 head 的地址,或指向节点的指针。

在C中,所有值都传递给函数值(不是通过引用)这意味着在函数内部使用传递内容的副本 - 您所做的任何更改都不会影响外部世界。这很方便:

OK, head is declared as a node* which means that head is a variable which contains a pointer to a node object.

Think about cars for a moment. You have a space in the car park where you have parked your car. If you let me drive it, you don't tell me "it's the red Ford Fiesta, registration number ABC 123" because I'd have to search the whole car park looking for it - you tell me it's parked on level 3, row B, bay 17: 3B17 is enough for me to find the car.
"3B17" is the address of the bay containing the car - or a pointer to the car. If I move the car, I return a different address: "4A12" and you can find the car and go home.

Similarly, head doesn't contain the first node in your list - it contains a number which tells you where that node is: the address of the node. And just like I did, I found your car by going to that address.
To go to the node, you follow the pointer - this is called dereferencing the pointer - by prefixing it with a '*' or by using the -> operator.

To get the address of anything, you prefix it with '&': so &head gives you the address of head, or a pointer to a pointer to a node.
And in C, all values are passed to functions by value (not by reference) which means that inside the function you use a copy of what is passed - any changes you make do not affect the outside world. This is handy:
void DoIt(int i)
   {
   i = 2 * i;
   }

这样做很好:

Is fine when you do this:

int i = 666;
DoIt(i);
printf(i);

然后你得到值666打印。如果它通过引用传递,你将打印1332 - 这也可以工作,直到你这样做:

And you get the value 666 printed. If it passed by reference, you would print 1332 - which would also work, until you did this:

DoIt(666);

按值传递意味着该函数不会影响外部世界,因此不会尝试更改基本常量!



所以当你写:

Passing by value means that the function can't affect the outside world, so there is no attempt to change fundamental constants!

So when you write:

Push(&head, 1);

你通过head的地址,或指向head的指针(或指向节点的指针),这意味着函数可以改变head本身。



当您使用 foo 函数时,cat中c的地址副本将传递给函数(按值,记住),该函数通过自动递增来更改副本,并在函数末尾丢弃副本,保持外部世界不变。

You pass through the address of head, or a pointer to head (or a pointer to a pointer to a node) which means that head itself can be changed by the function.

When you use your foo function, a copy of the address of the 'c' in "cat" is passed to the function (by value, remember), the function changes the copy by auto incrementing it, and the copy is discarded at the end of the function, leaving the outside world unchanged.


在示例1中,您有以下语句:

In example 1 you have the statement:
struct node* head = ...



声明 head 指向节点结构的指针。也就是说 head 是一个指针变量,它的值将是节点结构的地址; malloc BuildTwoThree 返回的值。在对推送的调用中,您将参数1作为& head 传递该变量的地址(& 是C / C ++中的addressof运算符。传递地址允许被调用的函数修改变量的内容。



在示例2中,您将指针而不是其地址传递给函数(实际上您传递了一个临时函数)复制)。因此,对函数中指针的任何更改都不会影响原始变量。


which declares head as a pointer to a node structure. That is to say that head is a pointer variable and its value will be the address of a node structure; either the value returned by malloc or BuildTwoThree. In the call to Push you have parameter 1 as &head which passes the address of that variable (& is the addressof operator in C/C++). Passing the address allows the called function to modify the variable's contents.

In example 2 you pass the pointer, not its address, to the function (actually you pass a temporary copy). So any changes to the pointer in the function do not affect the original variable.


Quote:

差异指针值,指针地址,指针变量之间

Difference between pointer value, pointer address, pointer variable



这是C的难点部分,我认为最好是阅读参考书并做练习。



以下是语言作者对C和C ++参考书的链接。注意,C是C ++的祖先,所以知道C对C ++总是有用。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

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 [ ^ ]



C ++编程语言 [ ^ ]


this is the difficult part of C, I think the best is to read the reference book and do exercises.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
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[^]

C++ Programing Language[^]


这篇关于指针值,指针地址,指针变量之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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