Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have noted they don't really work the same as normal arrays that simply write
int x[20]={0};

this will make a array with 20 0's after one another.
but if i try to do the same thing with struct's it goes south fast.
typedef struct FancyName {
     int x[20];
};

int _tmain(int argc, _tchar* argv[])
{
     FancyName Fancy;
     Fancy.x={0};
}

the following errors would be "cannot convert from 'int' to 'int[100]'".
and nomatter how i write the compiler will not execute it.
Fancy.x[]={0};
Fancy.x[0]={0};
Fancy.x={0};

so my question becomes how do i easely change all the values of a struct array to one value?
Posted

the easiest way is
C++
typedef struct FancyName {
     int x[20];
};

FancyName Fancy;

memset( Fancy, 0, sizeof(Fancy) );

Another way is to initialize a struct and memcopy to another
C++
FancyName source;
source.x[0] = 3; //for illustration
FancyName Fancy;

memcpy( Fancy, source, sizeof(Fancy) );

The last way is stupid looping in arrays of struxts.

your struct is lagging of sense, because it is an array. structs making more sense like here:
C++
typedef struct ColorPoint{
 int x; //coordinate
 int y; //coordinate
 int z; //coordinate
 int color; //color value
 float alpha;//alpha value

ColorPoint colorPoint;
colorPoint.x = 1;
colorPoint.y = 2;
colorPoint.z = 3;
colorPoint.color = 0xff;//red
colorPoint.alpha = 0.5; //half transparent
 
Share this answer
 
Quote:
typedef struct FancyName {
int x[20];
};
Using the C++ programming language, you don't need the typedef.



You might perform the inizialization to zero this way
C++
FancyName fc = {{0}};


or, using a modern C++ compiler
C++
FancyName fc{};

This would default-initialize all the members of the struct (see C++11 - the new ISO C++ standard: "uniform initialization"[^]).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900