Zero elements of structure...

J

John T.

If I have:
struct test{
unsigned short test1;
unsigned short test2;
unsigned short test3;
unsigned char test4;
unsigned char test5;
};

int main(){

test mytest;

mytest.test1 = 0x11FF;
mytest.test2 = 0x22FF;
mytest.test3 = 0x33FF;
mytest.test4 = 0x11;
mytest.test5 = 0xFF;

return 0
}

Then how do I zero all elements of the struct mytest in the easiest possible
way?
The straight-forward way would be:
mytest.test1 = 0x0000;
mytest.test2 = 0x0000;
mytest.test3 = 0x0000;
mytest.test4 = 0x00;
mytest.test5 = 0x00;

But could'nt this be done smarter, or at least, shorter?

John
 
P

Pieter Droogendijk

If I have:
struct test{
unsigned short test1;
unsigned short test2;
unsigned short test3;
unsigned char test4;
unsigned char test5;
};

typedef struct test test;
int main(){

test mytest;

mytest.test1 = 0x11FF;
mytest.test2 = 0x22FF;
mytest.test3 = 0x33FF;
mytest.test4 = 0x11;
mytest.test5 = 0xFF;

return 0
}

Then how do I zero all elements of the struct mytest in the easiest possible
way?

You could define the variable like this:
test mytest = {0};
which defines all elements as 0.

To zero it anywhere else, you'll probably want to do something like this:
memset (&mytest, sizeof (mytest), 0);
 
I

Irrwahn Grausewitz

If I have:
struct test{
unsigned short test1;
unsigned short test2;
unsigned short test3;
unsigned char test4;
unsigned char test5;
};

int main(){

test mytest;

mytest.test1 = 0x11FF;
mytest.test2 = 0x22FF;
mytest.test3 = 0x33FF;
mytest.test4 = 0x11;
mytest.test5 = 0xFF;

return 0
}

Then how do I zero all elements of the struct mytest in the easiest possible
way?
The straight-forward way would be:
mytest.test1 = 0x0000;
mytest.test2 = 0x0000;
mytest.test3 = 0x0000;
mytest.test4 = 0x00;
mytest.test5 = 0x00;

But could'nt this be done smarter, or at least, shorter?
One possibility is to declare a special variable
for this purpose:

struct test
{
unsigned short test1;
unsigned short test2;
unsigned short test3;
unsigned char test4;
unsigned char test5;
};

int main( void )
{
struct test mytest;
struct test Zerotest = { 0, 0, 0, 0, 0 };

mytest.test1 = 0x11FF;
mytest.test2 = 0x22FF;
mytest.test3 = 0x33FF;
mytest.test4 = 0x11;
mytest.test5 = 0xFF;

/* do sth. with mytest, then:*/

mytest = Zerotest;

return 0;
}
 
I

Irrwahn Grausewitz

Addition:

With this method you can also declare
variables for other special cases, for example:

struct test Onetest = { 1, 1, 1, '1', '1' };

which you can't do with the straightforward
memset()-method that P.D. (correctly) suggested
for the "set-all-members-to-zero" case.

Irrwahn
 
P

Pieter Droogendijk

Addition:

With this method you can also declare
variables for other special cases, for example:

struct test Onetest = { 1, 1, 1, '1', '1' };

which you can't do with the straightforward
memset()-method that P.D. (correctly) suggested
for the "set-all-members-to-zero" case.

For special cases, I guess your solution is fine. However I will suggest using
memset() wherever possible, as I'm positive it's faster and it will not bloat
your program (not the source but the resulting binary). If you doubt that, take
a look at the glibc source for memset() and the compiler output of 'mytest=
Zerotest;'.
 
M

Martin Dickopp

Pieter Droogendijk said:
To zero it anywhere else, you'll probably want to do something like this:
memset (&mytest, sizeof (mytest), 0);

ITYM `memset (&mytest, 0, sizeof mytest);'.

It should be noted that this works in this case, because all the structure
members have integer types. It is not guaranteed to work for pointers or
floating-point types.

Martin
 
P

Pieter Droogendijk

ITYM `memset (&mytest, 0, sizeof mytest);'.

o_O
How did THAT happen?!
Yes, that's what I meant, excuse me.
It should be noted that this works in this case, because all the structure
members have integer types. It is not guaranteed to work for pointers or
floating-point types.

Agreed. Mr.Grausewitz' solution is more suitable for a situation like that.
 
R

Richard Delorme

Pieter Droogendijk a écrit :
For special cases, I guess your solution is fine. However I will suggest
using memset() wherever possible, as I'm positive it's faster and it will
not bloat your program (not the source but the resulting binary). If you
doubt that, take a look at the glibc source for memset() and the compiler
output of 'mytest= Zerotest;'.

I doubt that.
On my system, with optimisation set on, memset is not called but inlined in
the code, and look exactly the same as the assignment. So there is no speed
advantage to use memset, but some drawbacks, since it is not safe in codes
containing floating point variables or pointers.
 
P

Peter Nilsson

Pieter Droogendijk said:
You could define the variable like this:
test mytest = {0};

I'd prefer:

static const struct test mytest = { 0 };

[Redundant initialisation for readability.]
which defines all elements as 0.

To zero it anywhere else, you'll probably want to do something like this:
memset (&mytest, sizeof (mytest), 0);

Not if the OP wants to remain strictly conforming in C90. [C99 has a
pending TR that will correct this.]
 
D

Dave Thompson

Pieter Droogendijk <[email protected]> wrote in message news:<[email protected]>...
To zero it anywhere else, you'll probably want to do something like this:
memset (&mytest, sizeof (mytest), 0);

Not if the OP wants to remain strictly conforming in C90. [C99 has a
pending TR that will correct this.]

You mean a DR (defect report), 263. And note it is only for integer
types, which the specific case upthread is, but not all types/structs.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top