什么是数组和指针的指针的指针之间的区别? [英] What is the difference between pointer to array and pointer to pointer?

查看:220
本文介绍了什么是数组和指针的指针的指针之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在新的编程和了解数组指针。我现在有点糊涂。看看下面的程序:

I'm new in programming and learning about pointers in array. I'm a bit confused right now. Have a look at the program below:

#include <stdio.h>
int fun();
int main()
{
    int num[3][3]={23,32,478,55,0,56,25,13, 80};
    printf("%d\n",*(*(num+0)+1));
    fun(num);
    printf("%d\n", *(*(num+0)+1));
    *(*(num+0)+0)=23;
    printf("%d\n",*(*(num+0)));
    return 0;
}
int fun(*p)  // Compilation error
{
    *(p+0)=0;
    return 0;
}

这是写在我的老师的笔记程序。在这里,在的main()功能,在的printf()函数引用操作被使用两次,因为 NUM 是指针数组,以便第一次引用操作会给指针 INT ,然后第二个会给出数值将指针指向。

This was the program written in my teacher's notes. Here in the main() function, in the printf() function dereference operator is being used two times because num is pointer to array so first time dereference operator will give pointer to int and then second one will give the value at which the pointer is pointing to.

我的问题是,当我传递数组名作为参数传递给函数乐趣()为什么 * P 被使用;为什么不 ** P NUM 是一个指针数组?

My question is that when I'm passing the array name as argument to the function fun() then why *p is used; why not **p as num is a pointer to array?

第二件事为什么 *(P + 0)是用来改变数组的第零元素的值;为什么不 *(*(P + 0)+0)= 0 的main()功能 *(*(NUM + 0)+0)是用来改变零个元素的价值?

Second thing why *(p+0) is used to change the value of zeroth element of the array; why not *(*(p+0)+0)=0 as in the main() function *(*(num+0)+0) is used to change the value of zeroth element?

整个事情对我来说是非常混乱,但我总得去了解它。我已搜查这件事,发现有数组和指针的指针的指针之间的差别,但我不明白了。

The whole thing is very confusing for me but I have to understand it anyway. I have searched about this and found that there is a difference between pointer to array and pointer to pointer but I couldn't understand much.

推荐答案

诀窍是数组的指针 - 衰减的:当你提到一个数组的名字,它会衰变成一个指针在几乎所有环境中的第一个元素。这就是 NUM 是一个简单的三个整数三个数组的数组(类型= INT [3] [3]

The trick is the array-pointer-decay: When you mention the name of an array, it will decay into a pointer to its first element in almost all contexts. That is num is simply an array of three arrays of three integers (type = int [3][3]).

让我们分析前pression *(*(NUM + 1)+ 2)

Lets analyse the expression *(*(num + 1) + 2).

当你提到 NUM 在EX pression *(NUM + 1),它衰变为一个指针到它的第一元件,它是三个整数数组(类型= INT(*)[3] )。在此指针指针运算被执行,并且任何指针所指向的大小被添加到指针的值。在这种情况下,它是三个整数(这是在许多机器12字节)的阵列的大小。取消引用指针后,你留下一个类型为int [3]

When you mention num in the expression *(num + 1), it decays into a pointer to its first element which is an array of three integers (type = int (*)[3]). On this pointer pointer arithmetic is performed, and the size of whatever the pointer points to is added to the value of the pointer. In this case it is the size of an array of three integers (that's 12 bytes on many machines). After dereferencing the pointer, you are left with a type of int [3].

不过,这解引用仅涉及的类型,因为提领操作之后,我们看到int类型的前pression *(/ *前pression [3] * / + 2),所以内前pression衰变返回到一个指向第一个数组元素。该指针包含了相同的地址,从 NUM + 1 产生的指针,但它有一个不同的类型:为int * 。因此,在这个指针的指针算法由两个整数(8字节)前进的​​指针。所以前pression *(*(NUM + 1)+ 2)产生以偏移的整数单元12 + 8 = 20 字节,这是阵列中的第六整数

However, this dereferencing only concerns the type, because right after the dereferencing operation, we see expression *(/*expression of type int[3]*/ + 2), so the inner expression decays back into a pointer to the first array element. This pointer contains the same address as the pointer that results from num + 1, but it has a different type: int*. Consequently, the pointer arithmetic on this pointer advances the pointer by two integers (8 bytes). So the expression *(*(num + 1) + 2) yields the integer element at an offset of 12 + 8 = 20 bytes, which is the sixth integer in the array.

关于你提到的有关)的大呼过瘾(,该呼叫实际上是断的问题,只能是因为你的老师并没有包括在向前声明的参数乐趣()。在code

Regarding your question about the call of fun(), that call is actually broken, and only works because your teacher did not include the arguments in the forward declaration of fun(). The code

int fun(int* arg);

int main() {
    int num[3][3] = ...;
    ...
    fun(num);
}

会产生一个编译时错误是由于错误的指针类型。你的老师的code作品,因为指向第一个数组中的 NUM 是一样的指针在第一个数组的第一个元素 NUM ,我。即他的code相当于

would have generated a compile time error due to the wrong pointer type. The code of your teacher "works", because the pointer to the first array in num is the same as the pointer to the first element of the first array in num, i. e. his code is equivalent to

int fun(int* arg);

int main() {
    int num[3][3] = ...;
    ...
    //both calls are equivalent
    fun(num[0]);
    fun(&num[0][0]);
}

这将编译没有错误。

which would compile without error.

这篇关于什么是数组和指针的指针的指针之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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