C++ 指针查询 [英] C++ pointers query

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

问题描述

这是我对 C++ 指针的非常简单的介绍性代码,

This is my very simple introductory code to C++ pointers,

#include <iostream>

int main(int argc, const char* argv[])
{
    int *p = 0, numbers[5];

    *p = 10;
    *(p+1) = 20;
    *(p+2) = 30;
    *(p+3) = 30;
    *(p+4) = 40;

    for (int i = 1; i < 5; i++)
        numbers[i] = *(p + i);

    for (int i = 1; i < sizeof(numbers); i++)
        std::cout << numbers[i] << '\n';

    return 0;
}

我在 Xcode 中运行它,它显示构建成功"但给了我堆栈输出并且没有显示实际结果.

I am running this in Xcode and it shows "build succeeded" but gives me stack output and doesn't show me the actual result.

推荐答案

指针是存储数字的变量,就像其他任何变量一样,但是因为您告诉编译器它是一个指针,所以编译器允许您将该值用作内存中事物的地址和语言提供了解引用"作为一种表达该变量描述的地址处的值"的方式.

Pointers are variables which store a number, like any other, but because you tell the compiler it is a pointer, the compiler allows you to use that value as an address of things in memory and the language provides "dereferencing" as a way of saying "the value AT the address this variable describes".

想象一下你要去机场赶飞机.您抓起一张便利贴并写下您的航班号,然后在登机口随身携带第二张便利贴.

Imagine you're going to the airport to catch a flight. You grab a post-it note and write down your flight number, and you take a second post-it with you for your gate.

第一篇是flight*"指针,第二篇是gate*",但现在gate*指针是空的.

The first post it is a "flight*" pointer and your second is a "gate*" but right now the gate* pointer is empty.

当您到达机场时,您在黑板上查找您的航班并记下登机口号码.3A".现在您的登机门*便利贴有效.

When you get to the airport, you look up your flight on the board and jot down the gate number. "3A". Now your gate* post-it is valid.

但是便利贴本身不是您的登机口,它只是指向它:您仍然必须取消引用"便利贴才能到达您的航班 - 也就是说,穿过机场到 3A 登机口 :)

But the post-it itself is NOT your gate, it just points to it: you still have to "dereference" the post-it note to get to your flight - that is, walk across the airport to gate 3A :)

在编程中,变量不能为空,所以已经约定地址为0的指针为空"和非法".程序在尝试使用空指针时会崩溃.按照惯例,C 中有一个名为NULL"的宏,旨在将空指针"与值 0 区分开来.C++ 继承了这一点,但在 C++11 中,他们引入了一个新常量nullptr".

In programming, a variable can't be empty, so it has been agreed upon that a pointer with an address of 0 is "null" and "illegal". Programs crash when they attempt to use a null pointer. By convention there is a macro called "NULL" in C which is intended to distinguish "empty pointer" from the value 0. C++ inherited this but in C++11 they have introduced a new constant, "nullptr".

您的代码打开如下:

int *p = 0;

这吸引了很多人.它看起来与我们通过指针赋值时的语法相同:

Which catches a lot of people out. It looks like the same syntax as when we assign through a pointer:

*p = 10;

但实际发生的是,您声明了一个类型为int *"的变量 p 并为 p 赋值,而不是 *p.

But what is actually happening is that you are declaring a variable p which is of type "int *" and assigning a value to p, not *p.

int *p;
p = 0;
*p = 10;

您需要将p"指向一些用于存储的整数,或者:

You need to point "p" at some integers for storage, either:

p = numbers;

但这将使 p 指向与数字相同的存储.但是要意识到,像

but this will make p point to the same storage as numbers. But realize, then, that operations like

numbers[i] = *(p+i);

现在是多余的 -- if (p == numbers) then *(p+i) 等同于说 numbers[i].

are now redundant -- if (p == numbers) then *(p+i) is the same as saying numbers[i].

这篇关于C++ 指针查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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