Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Elegant way to clear a struct ?

Reply
Thread Tools

Elegant way to clear a struct ?

 
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      07-01-2003
Giuseppe wrote:

>>> Otherwise, use
>>> const Mystruct null_mystruct; /* will be initialized with nulls */


That statement should either be at file scope, or it should be
static const Mystruct null_mystruct;

This time I got it right, I think

> Mystruct da_azzerare;
>
> da_azzerare = null_mystruct;


That's fine.

> *( Mystruct *) &da_azzerare = *( Mystruct *) &null_struct;


Oh my gosh. You could, but don't. BTW, it should have been
*( const Mystruct *) &null_struct;
since you should not cast away const when you don't need to.

--
Hallvard
 
Reply With Quote
 
 
 
 
Kevin Easton
Guest
Posts: n/a
 
      07-07-2003
bd <(E-Mail Removed)-ip.org> wrote:
> On Sun, 29 Jun 2003 15:28:00 +0000, Kevin Easton wrote:
>
>> Hallvard B Furuseth <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote:
>>> Nils Emil P. Larsen wrote:
>>>
>>>> I have a pointer to a struct or an array of structs and I know the
>>>> lenght of the struct or array of structs.
>>>>
>>>> I need a elegant way to clear it with zeroes.
>>>>
>>>> Mystruct *mystruct; // Mystruct is a typedef
>>>
>>> If the struct contains nothing but integer (and character) elements,
>>> you can just do
>>> memset(mystruct, 0, array_length * sizeof(Mystruct))
>>> Otherwise, use
>>> const Mystruct null_mystruct; /* will be initialized with nulls */

>>
>> What basis do you have for that comment? Did you perhaps mean either:
>>
>> const Mystruct null_mystruct = { 0 };
>>
>> or
>>
>> static const Mystruct null_mystruct;
>>
>> ?

>
> That's implicit in static and file scope variables.


It was neither static nor file scope (because the next line was a
statement, which can't appear at file scope).

- Kevin.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Most elegant way to clear all the text fields on asp.net page? Duk Lee ASP .Net 2 07-23-2007 08:25 PM
Re: Is there an elegant way to set an unsigned vector to 1 Jan De Ceuster VHDL 5 01-13-2005 07:26 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
Elegant way of returning FieldNames? Jay Balapa ASP .Net 1 08-07-2003 10:25 PM



Advertisments