对指针及其内存地址的困惑

时间:2010-06-11 16:59:41

标签: c++ pointers

好吧,我在这里查看代码,这个想法很难理解。

#include <iostream>
using namespace std;
class Point
{
public :
    int X,Y;
    Point() : X(0), Y(0) {}
};

void MoveUp (Point * p)
{
    p -> Y += 5;
}

int main()
{
    Point point;
    MoveUp(&point);
    cout << point.X << point.Y;
    return 0;
}

好吧,所以我相信创建了一个类,声明了X和Y,并将它们放在构造函数中

创建了一个方法,参数是Point * p,这意味着我们将把构造函数的指针放在函数中;

现在我们创建一个名为point的对象然后调用我们的方法并将指针地址放在其中?

不是指针只是像0x255255这样的内存号吗?

为什么没有宣布?

(int * p = Y)

什么是内存地址?它可以用作参数吗?

6 个答案:

答案 0 :(得分:9)

p 已宣布

void MoveUp (Point * p)
{
    p -> Y += 5;
}

是一个函数,它将指向Point并将其值加上5。它与以下内容没有什么不同:

void f(int n) {
    printf ("%d\n", n);
}
:
int x = 7;
f(x);

你不会说在这种情况下没有定义n。在您的情况下,p也是如此。

也许代码中的一些注释会有所帮助:

#include <iostream>
using namespace std;
class Point
{

public :
    int X,Y;
    Point() : X(0), Y(0) {}       // Constructor sets X and Y to 0.
};

void MoveUp (Point * p)           // Take a Point pointer p.
{
    p -> Y += 5;                  // Add 5 to its Y value.
}
int main()
{
    Point point;                  // Define a Point.
    MoveUp(&point);               // Call MoveUp with its address.
    cout <<point.X << point.Y;    // Print out its values (0,5).
    return 0;
}

指针只是间接的一个层次。在代码中:

1   int X;
2   int *pX = &X;
3   X = 7;
4   *pX = 7;

第3行和第4行的效果相同。这是因为pX是指向X的指针,因此*pX的内容pX实际上是X

在您的情况下,p->Y(*p).Y相同,或Y指向的类的p成员。

答案 1 :(得分:3)

指针只是一个变量,就像任何其他变量一样,但它保存了一个内存地址。
阅读该行大约6次。名称指针本身对人们来说似乎很可怕,但它实际上只是为了让我们自己更容易思考正在发生的事情。

指针的类型(例如char *,int *,Point *)只是告诉编译器存储在该内存地址的内容。

因此所有指针都相似,因为它们都存储了一个简单的内存地址。但它们的不同之处在于指针的类型将告诉您该内存地址将包含的内容。

  

为什么没有宣布?

宣布:

//p is declared to be a variable that holds an address
// and at that address it holds a type Point
//p itself only holds an address not the actual data of the Point.
void MoveUp (Point * p)
{
  //The -> operator when applied to a pointer, means give me the object at
  // that address and then access the following member
  p->Y += 5;
}

  

什么是内存地址?它可以用作参数吗?

您可以将内存地址简单地视为一个数字。在32位程序上,此数字长度为4个字节。在64位程序上,此数字长度为8个字节。


请考虑以下代码:

Point point;
Point *p = &point;

point变量的类型为Point。它拥有类Point的对象 &point表达式返回Point变量point的地址。表达式&point的类型为Point* p变量包含Point类型的地址,p的类型为Point*p将地址保存在对象point的内存中。

答案 2 :(得分:1)

p在函数定义中声明

void MoveUp (Point * p)

答案 3 :(得分:1)

在你看指针之前,你必须自己澄清“class”,“instance”和“constructor”的含义。你的句子

  

创建一个类,X和Y是   声明,他们被放入一个   构造

显示需要进行此类澄清。以下是您可能想要阅读的书籍的讨论:Books to refer for learning OOP through C++

答案 4 :(得分:1)

将内存视为连续的小块。

现在,将Point类视为2块宽度的东西,然后将它放在内存的任何位置。无论在哪里,重要的是它只放在一个起点。你可以移动它并改变那个凝视点,但不可避免地会在一个起始点开始。

然后指针就是那个ONE起始点的编号。

当您将指针作为参数传递时,您执行

  • 你:'嘿功能,拿这点来做你知道的事情!'
  • 功能:'好的,但Point在哪里!?'
  • 你:'哦,对不起,我不带着它,它就在那里,就在那个街区!'

现在该函数将知道有一个Point,并且有了编译器的魔力,该函数将知道它的2个块宽度。然后函数接受它,改变它的Y(再次感谢编译器),并将它保留在之前的位置(事实上,它甚至没有从那里拿走它)。 几分钟后,你去那里,看看Point.Y已经改变了。你说'谢谢',你走了。

答案 5 :(得分:0)

p被声明为方法参数。

指针是一种特殊的变量,用于保存值的内存地址。 *符号是解除引用符号,它告诉您的代码去查找指针所占内存地址的值IN。现在也是介绍&amp; amp;符号,告诉您的代码获取值的内存地址。例如:

int i = 5;    //int
int *pointer; //int pointer

pointer = &i; //sets pointer to the memory address of i

doMath(&i); //passes a memory address, value inside that address is being used
doMath(pointer); //same as above
dontMath(i); //value of x will be 207, value of i is still 7 since a copy is being modified

//value of pointer is a memory address
//value of *pointer is the value stored inside that memory address, 7
//value of &pointer is the memory address of pointer, which itself holds the memory address


void doMath(int *p) {
   *p++; 
}
void dontMath(int x){
   x=x+200;
}

我早期的困惑是*符号的含义。在变量声明中,这意味着您正在为某个数据类型(一个包含内存地址的变量)声明一个指针。在其他地方,它是取消引用符号,告诉您的代码解析该值。

相关问题