struct COLOR
{
unsignedchar R;
unsignedchar G;
unsignedchar B;
};
int main()
{
COLOR * color;
color = new COLOR[5];
cout << (int)color[3].G //Default value of a color part....(right?)
return 0;
}
The above, for SOME REASON, returns a value of 205.
205!? Why!?! Every R,G, and B of every COLOR in the array is set to 205!!!
You don't have a constructor for struct..
In fact what you're doing is making a pointer to a color
then assigning that pointer to an array of 5 colors (the first one is assigned)
You never even initialize the colors.. so they are set to random numbers.. it results in undefined behavior..
you need to assign values to those colors, either as default values in the struct, or later in the main function after creating them.
Also, since you used new to allocate memory, don't forget to use delete[] to free that memory before you exit the program.
in C, there isn't. But in C++ a struct and a class are identical. They are teh same thing with one major difference.
In C++ structs default to public. meaning unless you put private: or protected: in there, everything will be publicly accessible. classes however, default to private. everythign up to the first public: or protected: is private and unaccessible.
So yes, in C++ structs can have constructors.. however, in the case above.. none is needed. What I meant by my first line was that I figgured he was trying to assign '5' to the values when he declared
color = new color[5];
Which is making an array. Then I read his code again and realized my mistake.
When I said to try using default values in the struct itself, i meant as such:
1 2 3 4 5
struct colors {
char r = 0;
char g = 0;
char b = 0;
};
Or it can be assigned later in the main by doing this:
1 2 3 4 5 6
main {
color mycolor;
mycolor.r = 0;
mycolor.g = 0;
mycolor.b = 0;
}
dev c++ but what i said is the same for both.
though i'm tempted to look into the free msvc++ express edition.. especially if it comes with a graphical editor for making dialog boxes and adding icons and images to a resource file.
Dev C++ handles resource files.. or so it says, but has no graphical dialog maker that I've found thus far.
if you declare a variable say for example: int a then if it not initialised and you run the program then in the debugger it will show a value as follows: char type will be 0xcc (decimal 204) shortwill be 0xcccc int will be 0xcccccccc
and so on.
If you create a variable/struct using new then if you run the program but have not initialised the variable/structure values
then you will see in the debugger: char type will be 0xcd (decimal 205) ****** shortwill be 0xcdcd int will be 0xcdcdcdcd
and so on.
An unitialised pointer will show as 0xcccccccc
I believe it really does put those values in - because if you try to cout an uninitialised variable and ignore the 'Unitialised variable error dialog box'
then you really will see those values on the screen.
To follow on from guestgulkan's reply. Do NOT assume this is going to be true, because if someone else runs it on their compiler/ide/os it may not work like that :)