简体   繁体   English

指针和数组与指针和整数C ++

[英]Pointers and Arrays vs Pointers and Integers C++

I am learning C++ and have become a little confused to how a Pointer to an Integer Value and a Pointer to an Array differ... See my code below: 我正在学习C ++,并且对指向整数值的指针和指向数组的指针有何不同感到困惑...请参见下面的代码:

int main(void) 
{
    int* ptrOne;
    int VarOne = 25;

    ptrOne = VarOne; 

    int* ptrTwo;
    int ArrayTwo[6];
    ArrayTwo[0] = 2; //ect for the rest of the array, omitted here.

    ptrTwo = ArrayTwo;
}

Pointers are just variables that hold an address. 指针只是保存地址的变量。

For the line ptrOne = VarOne, it shoves the VALUE of 25 into ptrOne. 对于ptrOne = VarOne行,它将25的值推入ptrOne中。

For the line ptrTwo = ArrayTwo, it shoves the ADDRESS of ArrayTwo[0] into ptrTwo. 对于ptrTwo = ArrayTwo行,它将ArrayTwo [0]的地址推入ptrTwo。

Why is ptrTwo = ArrayTwo equivalent to ptrTwo = &ArrayTwo[0], but ptrOne = VarOne is NOT equal to ptrOne = &VarOne? 为什么ptrTwo = ArrayTwo等同于ptrTwo =&ArrayTwo [0],但是ptrOne = VarOne不等于ptrOne =&VarOne?

Is this due to the fact the operation is happening to an array vs an int? 这是由于操作发生在数组与int之间吗?

Thanks in advance for any help, I have stepping through this code in my compiler and looked at the addresses in memory and the associated values, I did also read the answer for how to differentiate integer pointer and integer array pointer , but it did not fully explain the differences. 在此先感谢您的帮助,我在编译器中逐步完成了这段代码,并查看了内存中的地址和关联的值,我也确实阅读了有关如何区分整数指针和整数数组指针的答案,但并没有完全解释差异。

Why is ptrTwo = ArrayOne equivalent to ptrTwo = &Array[0] 为什么ptrTwo = ArrayOne等效于ptrTwo =&Array [0]

Because in C the name of the array is the address of the first element. 因为在C语言中,数组的名称是第一个元素的地址。 You may want to check out further why: How come an array's address is equal to its value in C? 您可能想进一步检查原因: 数组的地址为什么等于C中的值?

but ptrOne = VarOne is NOT equal to ptrOne = &VarOne? 但是ptrOne = VarOne不等于ptrOne =&VarOne吗?

Because VarOne is a single variable, so VarOne is the value of the variable itself, not the address of that variable. 因为VarOne是单个变量,所以VarOne是变量本身的值,而不是该变量的地址。

Actually this code should be an outright error if you switch on compiler warning ( -pedantic-errors ) which tells you that you can't convert a pointer type to int. 实际上,如果您打开编译器警告( -pedantic-errors ),则该代码应该是彻底的错误,它告诉您不能将指针类型转换为int。

error: assignment makes pointer from integer without a cast [-Wint-conversion]
int A[5]={0,1,2,3,4};

The name of the array A is a constant pointer to the first element of the array. 数组A的名称是指向数组第一个元素的常量指针。 So A can be considered a const int*. 因此,可以将A视为const int *。 Since A is a constant pointer, A = NULL would be an illegal statement. 由于A是常量指针,因此A = NULL将是非法语句。 Arrays and pointers are synonymous in terms of how they use to access memory. 就如何使用数组和指针访问内存而言,它们是同义词。 A[0] returns the value in [the pointer of the array's first element]+0 yet returns the array element with the index of '0' . A[0]返回[数组的第一个元素的指针] +0中的值,但返回索引为'0'的数组元素

int* ptr= A; //= int* ptr= &A[0];

So since int is considered as a data type we can't use it as a pointer. 因此,由于将int视为数据类型,因此我们不能将其用作指针。 when we want to declare a static pointer of an int value it's written with the following syntax : 当我们要声明一个int值的静态指针时,它是使用以下语法编写的:

int X= 25;
int* ptr= &X;

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

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