Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 指针和数组混淆_Arrays_Pointers - Fatal编程技术网

Arrays 指针和数组混淆

Arrays 指针和数组混淆,arrays,pointers,Arrays,Pointers,我试图理解指针和数组。在努力在网上找到指针(双关语!)之后,我在这里陈述我的困惑 //my understanding of pointers int d = 5; //variable d int t = 6; //variable t int* pd; //pointer pd of integer type int* pt; //pointer pd of integer type pd = &d; //assign address of d to pd pt

我试图理解指针和数组。在努力在网上找到指针(双关语!)之后,我在这里陈述我的困惑

//my understanding of pointers
int d = 5; //variable d
int t = 6; //variable t

int* pd;   //pointer pd of integer type 
int* pt;   //pointer pd of integer type 

pd = &d;   //assign address of d to pd
pt = &t;   //assign address of d to pd

//*pd will print 5
//*pt will print 6

//My understanding of pointers and arrays

int x[] = {10,2,3,4,5,6};
int* px; //pointer of type int

px = x; //this is same as the below line
px = &x[0]; 
//*px[2] is the same as x[2]
到目前为止我明白了。现在,当我执行以下操作并打印pd[0]时,它会显示类似于-1078837816的内容。这里发生了什么

pd[0] = (int)pt; 

有人能帮忙吗?

您已经告诉编译器将指针pt的字节解释为int,这样您将得到一个看似随机的结果,表示pt恰好位于内存中的任何位置

我想你想要

pd[0] = pt[0] 


int*pt;是指向整型变量的指针,而不是整型变量。通过指定(int)pt,您告诉编译器将pt类型转换为整数

要获取使用*pt的pt处的变量,这将返回存储在pt中的地址指向的整数变量


这有点让人困惑,因为你说的是pd[0],而pd不是数组。

在本文中,我读到:“。“设置页面表”部分声明了一个无符号长*页面表,然后在for循环中使用页面表[i]。但是page_表不是数组,所以我不明白为什么要这样使用它。你能解释一下吗?数组本质上是指向顺序内存的指针,数组的每个元素都是指针的变量类型的大小。在那个例子中,他们创建了一个无符号长指针(4字节),从内存中的某个特定点开始,然后循环1024次,基本上覆盖4096字节的内存。数组的每一个增量都是内存中的下一个无符号长指针。对不起,我不知道无符号长指针变量是如何用作数组的。您说“数组的每个增量都是内存中的下一个无符号长数组”,但这里没有声明数组。感谢您的帮助。从技术上讲,数组不是数据类型,数组是指针。它指向内存中的第一个位置,然后允许数组语法告诉它如何遍历内存。变量的数据类型决定了步长。示例:char*str=new char[20];允许str[0]、str[1]、str[2]等。str是指向char类型变量的指针,但可以使用数组语法。数组是一种不同的表示方法,更易于使用。您不必特别告诉编译器它是一个数组来使用语法。但使用它可能会指向无效内存!但是这个例子只声明了指针,没有使用“newunsignedlong[1024],所以编译器是否分配了无限大小?它是隐式的吗?
pd[0] = *pt;