C ++ Primer: Array implicitly converted into 5 exception pointer.


1. Background

  In most of the expression contains an array, the array is automatically converted into a pointer pointing to the first element of the array.

#include <iostream>
using namespace std;

int main()
{
    int i[3] = {1, 2, 3};
    int *p = i;

    cout << i << " " << i[0] << endl;
    cout << p << " " << *p << endl;
    return 0;
}

operation result

2. Exceptions

2.1 decltype keyword

  decltype a type specifier, for selecting and return type of the operands. When an array as a parameter decltype, it returns an array type, rather than a pointer. Not initialize or assign between arrays.

#include <iostream>
using namespace std;

int main()
{
    int i1[3] = {1, 2, 3};
    //decltype(i1)返回int [3], 而非int *
    //此时, i2是含有3个元素的数组,而非指针。
    decltype(i1) i2;
    //不能把数组赋给另一数组
    // i2 = i1;

    int *p1 = i1;
    //decltype(p1)返回int *
    decltype(p1) p2;
    //可以把数组赋值给指针,此时数组自动转换成指向首元素的指针
    p1 = i1;

    return 0;
}

2.2 fetch address character (&)

  Address-ampersand (&) used to address acquisition to assign a pointer variable. When the array fetch address, it returns a pointer to an array, rather than a pointer to a pointer.

#include <iostream>
using namespace std;

int main()
{
    int i1[3] = {1, 2, 3};
    int *p1;
    // p2是一个数组,数组有3个元素,每个元素都是int *
    int *p2[3];
    //p3是一个指针,指向一个数组,数组中有3个元素
    int(*p3)[3];
    //p4是一个二重指针
    int **p4;

    // p1 = &i1;//错误,类型不匹配
    // p2 = &i1;//错误,不能把指针赋给数组
    p3 = &i1;
    // p4 = &i1;//错误,类型不匹配

    return 0;
}

2.3 sizeof keyword

  sizeof expressions used to return a percentage of the number of bytes or a type name. Sizeof array performs arithmetic to obtain the size occupied by the entire array, sizeof pointer performs arithmetic to obtain the size of the pointer itself.

#include <iostream>
using namespace std;

int main()
{
    int i[3] = {1, 2, 3};
    int *p;

    //sizeof i;返回整个数组所占大小,若int占4个字节,i占3*4=12个字节
    size_t st1 = sizeof i;
    //sizeof p;返回指针本身所占空间大小
    size_t st2 = sizeof p;

    cout << st1 << endl;
    cout << st2 << endl;
    return 0;
}

operation result

2.4 typeid keyword

  typeid for obtaining object type. To obtain an array type, pointer type rather than performing an array operation typeid pair.

#include <iostream>
using namespace std;

int main()
{
    int i[3] = {1, 2, 3};
    int *p = i;

    cout << "int[3] type : " << typeid(int[3]).name() << endl;
    cout << "i type : " << typeid(i).name() << endl;

    cout << "-------------------------------------" << endl;
    cout << "int* type : " << typeid(int *).name() << endl;
    cout << "p type : " << typeid(p).name() << endl;

    return 0;
}

operation result

2.5 reference array

  The array itself is an object, you can define a reference to the array. Array reference bindings not be converted to a pointer, i.e. a reference to the operation is an array operation, instead of the pointer operation.

#include <iostream>
using namespace std;

int main()
{
    int i1[3] = {1, 2, 3};
    // i2是一个对数组的引用,用数组i1初始化引用。
    int(&i2)[3] = i1;

    // 不存在存放引用的数组,因为引用不是对象
    // int &i3[3] = i1;

    //错误,i4是对int的引用,而不是对数组的引用
    // int &i4 = i1;

    //数组之间不可以初始化或赋值
    // i2 = i1;

    return 0;
}
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/104021024