dimanche 6 juin 2021

What type is the next variable?

So I had this question at an exam. And I don't know which answer is correct.

    #include<bits/stdc++.h>
using namespace std;

struct Test{
    int note;
    char exam[20];
}qwerty;

int main()
{
    cout<<qwerty.exam;
}

What is the type of qwerty.exam in the previous code? Is it char or char[20]? They said the right answer is char[20]. But if the type is char[20], why can't I declare something like: char[20] exam; ?

Moreover, I found this on cplusplus website: Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:

type name [elements];

where type is a valid type (such as int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets []), specifies the length of the array in terms of the number of elements.

So I understand that the type does not depend on the number of elements. Please try to explain. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire