For example:
typedef struct
(int a = 0;
int b = 1;
x);
Does it work?
For example:
typedef struct
(int a = 0;
int b = 1;
x);
Does it work?
No, x is a shape, not a value.
You can initialize the instantiation:
Now, if you want that whenever you create an x it is automatically initialized with a = 0, b = 1 ...Code:1. ox = x (0,1);
... then it is time to move to C + + which provides manufacturers made to it:
Code:1. struct x ( 2. int a; 3. int b; 4. x () ( 5. a = b = 0; 6. )7. );
Ok thank you!
No, x is a shape, not a value.
You can initialize the instantiation:
Now, if you want that whenever you create an x it is automatically initialized with a = 0, b = 1 ...Code:ox = x (0,1);
... then it is time to move to C + + which provides manufacturers made to it:
Basically is there a difference between struct and class, made by COMPLÔ C + +?Code:struct x ( int a; int b; x () ( a = b = 0; ) );
It should be an encapsulation of history ...
If I have another question.
I still have my structure X:
typedef struct
(int a;
int b;
x);
And I want to know if we can compare two instances of X:
X foo;
X bar;
foo.a = 1;
toto.b = 2;
titi.a = 0;
titi.b = 5;
Is it possible to:
if (foo == titi) {...}
Default (if nothing is specified) is private in a class and public in a structure, right?
I have another question (I feel I have forgotten everything).
If I change my toto, is it better to move the pointer argument in toto or return another X:
void modify (X * foo)
(foo-> a = 54![]()
or
X modify (foo X)
(X temp;
temp = foo;
temp = 54;
return temp;
)
Ok but now I work in C so I can not redefine it if?
There's a solution to the so-called hard:
It works very well for your example!
Code:Ttoto a; Ttoto b; [...] if (memcmp (a, b, sizeof (ttoto) == 0) ( )
I have another question (I feel I have forgotten everything).
If I change my toto, is it better to move the pointer argument in toto or return another X:
void modify (X * foo)
(foo-> a = 54![]()
or
X modify (foo X)
(X temp;
temp = foo;
temp = 54;
return temp;
)
Me in this case I pass a pointer to the instance of foo in parmetre in case I want to change fields. AND I return the transaction status: OK or ERROR.
Code:Ttoto a; int initialize (void * foo, size) ( if (foo == NULL) return ERROR; if (size <1) return ERROR; memset (foo, 0, size); return OK; ) if (initialize (& a, sizeof (ttoto))! = OK) ( fprintf (stderr, "There's a testicle. n"); return or exit ... )
But if there are holes in the structure because of alignments is not guaranteed.Code:if (memcmp (& foo, & titi, sizeof (X) == 0) / * identical * /;
There remains a macro:
You do not want to move to C + + where possible?Code:# define Xequal (x1, x2) ((x1). a == (x2). a & & (x1). b == (x2). b)
If foo is large, it is faster to pass by address.Code:If I change my toto, is it better to move the pointer argument in toto or return another X:
If foo has many members to change, it is faster to pass / return by value.
If the function should be used in expressions, it is better to return by value.
Me, I prefer by address (reference C + +).
No, x is a shape, not a value.
You can initialize the instantiation:
Now, if you want that whenever you create an x it is automatically initialized with a = 0, b = 1 ...Code:ox = x (0,1);
... then it is time to move to C + + which provides manufacturers made to it:
For people who have more than 2 hours of C + + in their lives, they can put x (): a (0), b (1) ()Code:struct x ( int a; int b; x () ( a = b = 0; ) );
is much more elegant and avoids initializing ca 2 times the value.
edit: and by the way, a = b = 0 is not terrible either, it's downright pig.
how can we initialize a table structure in C
eg
struct text
(char id [20];
char pw [20];
);
typdef struct text;
text table [100];
text table [100] = (( "lol" "rofl"), .... , ( "Test", "test));
Bookmarks