jeudi 21 novembre 2019

Why is an array's dimension part of it's type?

While reading the C++ Primer book, I came across this statement: "The number of elements in an array is part of the array's type." So I wanted to find out using the following code:

#include<iostream>

int main()
{
    char Array1[]{'H', 'e', 'l', 'p'};
    char Array2[]{'P', 'l', 'e', 'a', 's', 'e'};

    std::cout<<typeid(Array1).name()<<std::endl;        //prints  A4_c
    std::cout<<typeid(Array2).name()<<std::endl;        //prints  A6_c

    return 0;
}

And interestingly the result of typeid on the two arrays showed they are somehow different.

  • What's going on behind the scenes?
  • Why is it necessary to for array's to have a type that includes it's size? Is it just because it's size should not change?
  • How will this affect comparing arrays?

Just want to be able to deeply understand the concept.

Aucun commentaire:

Enregistrer un commentaire