a,& a和& a [0]之间的类型区别是什么? [英] What's the type difference between a, &a, and &a[0]?

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

问题描述

#include <iostream>

void arrayFunctionA(int* p)
{
}

int main()
{
    int a[3] = {7, 7, 7};
    arrayFunctionA(a);
    arrayFunctionA(&a[0]);
    arrayFunctionA(&a);
    return 0;
}

这将无法编译,并且在 arrayFunctionA(& a); 上出现错误.

This will not compile, with an error on arrayFunctionA(&a);.

因此, a & a [0] 的评估结果为 int * ,而& a 没有.

So, a and &a[0] evaluates as int*, but &a does not.

这3个有什么区别?

推荐答案

a 是3个 int 类型( int [3] ).

固定数组会衰减为其第一个元素的指针,因此在 arrayFunctionA(a)中, a 会衰减为 int * 指向它的第一个元素的指针.

A fixed array decays to a pointer to its first element, so in arrayFunctionA(a), a will decay into an int* pointer to it's first element.

& a [0] 是数组第一个元素的地址,类型为 int * .

&a[0] is the address of the array's first element, and is of type int *.

& a 是数组本身的地址,其类型为 int(*)[3] (指向3个 int的数组的指针)).

&a is the address of the array itself, and is of type int (*)[3] (pointer to an array of 3 int).

因此,对于具有签名的功能

So, for a function with signature

void arrayFunctionA(int* p);

传递 a & a [0] 将达到相同的目的,而传递& a 将导致两者之间的不兼容传递的参数值和函数参数.

Passing a or &a[0] will serve the same purpose, while passing &a will result in an incompatibility between the passed argument value and the function parameter.

这篇关于a,&amp; a和&amp; a [0]之间的类型区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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