简体   繁体   English

当我想改变变量的值时使用指针 *

[英]using pointer * when I want to change the value of a variable

#include <stdio.h>

int main(void) {
  int x = 5;
  int y = 10;
  
  int* px = &x;
  int* py = &y;
  printf("the adress of %d is %p.\n",x,px);
  printf("the adress of %d is %p.\n",y,py);

  *px = 8;//change the value at the adress of px
  *py = 18;

  //what happens if instead of *px = 8, just x = 8?
  x = 99;
   
  printf("the adress of %d is %p.\n",x,px);
  printf("the adress of %d is %p.\n",y,py);
  //so It did change, what's the point of using *px when changing the value?
  return 0;
}

Hello, I started learning C in school, and I bump into a difficulty which could be extremely simple, but at this point I do not know about it.你好,我在学校开始学习C,遇到了一个可以非常简单的难点,但目前我还不知道。 I am trying to change the value of x, and I used the method *px = 8 first to change it.我试图改变x的值,我首先使用*px = 8的方法来改变它。 but right after that I used x = 99, and it changed too, so I do not know what is the difference between them.但紧接着我用了x = 99,它也变了,所以我不知道它们之间有什么区别。 I appreciate any feedback, thank you!我感谢任何反馈,谢谢!

Q: I used the method *px = 8 first to change it Q:我先用了*px = 8的方法改了

A: The value of both "x" and "*px" changed to "8", correct? A:“x”和“*px”的值都变成了“8”,对吗?

Q: but right after that I used x = 99, and it changed too问:但在那之后我使用 x = 99,它也改变了

A: Cool.答:很酷。 So now both "x" and "*px" are 99. This is what you expected, correct?所以现在“x”和“*px”都是 99。这是你所期望的,对吗?

Q: so I do not know what is the difference between them.问:所以我不知道它们之间有什么区别。

In this example, they're both equivalent.在这个例子中,它们是等价的。 They both accomplish exactly the same thing.他们都完成了完全相同的事情。

Why use pointers at all?为什么要使用指针? Because the variable "x" is tied to a specific memory location: you can't change it.因为变量“x”绑定到特定的 memory 位置:您无法更改它。

The pointer "px", on the other hand, can be changed at runtime to point to DIFFERENT memory locations, if you needed to.另一方面,如果需要,指针“px”可以在运行时更改为指向不同的memory位置。

Why is that useful?为什么这有用? There are many different reasons.有很多不同的原因。 For example:例如:

  • When you malloc() dynamic memory, the system gives you a pointer.当你malloc() dynamic memory时,系统给你一个指针。 You don't know or care where in memory it's located: you just use it.您不知道也不关心它在 memory 中的位置:您只是使用它。

  • Pointers are frequently needed to traverse the elements in a linked list经常需要指针来遍历链表中的元素

  • The ability to increment (++) and decrement (--) pointers simplifies many algorithms递增 (++) 和递减 (--) 指针的能力简化了许多算法

  • Etc. etc.等等等等

You might find these articles helpful:您可能会发现这些文章有帮助:

Other answers explained what is happening, but in your question I also read you ask 'why' someone would want to use references instead of direct accessing of a memory address.其他答案解释了正在发生的事情,但在你的问题中,我还读到你问“为什么”有人想使用引用而不是直接访问 memory 地址。

There are many reasons why anything is the way it is in computer science, but a few I think most people wouldn't argue that are helpful for a beginning theoretical understanding:计算机科学中的任何事情都是这样的原因有很多,但我认为大多数人不会争辩说这对开始理论理解有帮助:

A lot of Computer Science as a discipline is efficient/optimal management of resources, in many cases computer memory, but I/O and CPU make up two other resources we manage to make 'good' software.许多计算机科学作为一门学科是资源的有效/最佳管理,在许多情况下是计算机 memory,但 I/O 和 CPU 构成了我们设法制作“好”软件的另外两个资源。 You may recall that a simple computer can be thought of as a CPU, RAM, and Storage, connected by a "bus".您可能还记得,一台简单的计算机可以被认为是 CPU、RAM 和存储,它们通过“总线”连接。

Up until relatively recently, most software wasn't being run on a machine with lots and lots of RAM.直到最近,大多数软件还没有在具有大量 RAM 的机器上运行。 It was necessary that memory be managed for performance purposes, and though we have access to higher memory machines today - memory must still be managed!出于性能目的,有必要对 memory 进行管理,尽管我们今天可以访问更高级别的 memory 机器 - memory 仍然必须进行管理!

We manage that in more recently created languages with abstraction - or in this context, doing things for people when we know with a high degree of certainty what they are trying to do.我们使用最近创建的抽象语言来管理这一点——或者在这种情况下,当我们高度确定地知道他们正在尝试做什么时,我们会为他们做事。 We call languages where much of resource management is abstracted and simplified as "high-level" languages.我们将大部分资源管理被抽象和简化的语言称为“高级”语言。 You are "talking" to the machine with your code at a high level of control relative to machine code (you can think of that as the ones and zeros of binary).您正在使用您的代码与机器“对话”,该代码处于相对于机器代码的高控制级别(您可以将其视为二进制的 1 和 0)。

TLDR; TLDR; Memory is finite, and pointers generally take up less space than the data they point to, allowing for an efficiency in address space utilization. Memory 是有限的,指针通常比它们指向的数据占用更少的空间,从而提高地址空间的利用效率。 It also allows for many other neat things, like virtualized address spaces.它还允许许多其他整洁的东西,如虚拟化地址空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM