Tuesday, July 28, 2009

How many data types can all the elements of an array in c++ have?..?

this is a question in our report, and i would like to know, it.tnx=)

How many data types can all the elements of an array in c++ have?..?
One data type unless you are abusing an array of void pointers to point to other things of different lengths, or a struct containing unions.


C++ arrays needs to know the size of an element so it can calculate the offset to work out where the next element is, so every element must be the same size.


When you say myArray[x] you are actually saying "take the start of myArray in memory, plus x times by sizeof(typeOfElement) in bytes, and treat that memory as a typeOfElement object"





or in code that would be-


*((typeOfElement*)( %26amp;myArray[0] + (x*sizeof(typeOfElement)) ));


No comments:

Post a Comment