Re: Structure initialization



Walter Roberson wrote:
In article <13f6f4be-d45e-48e6-a203-4aa8f66b9ccc@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
<jb.simon@xxxxxxxx> wrote:

Recently I was pinged in a code review about my use of the
initialization method
AStruct myStruct = { 0 } ;

Which initializes all elements of the myStruct to 0.

I was questioned on it because no one had seen this construct.

It does what you expected, and is, I believe, recognized by
experienced C programmers.

On the other hand, code reviews are often useful to catch constructs
that might be unclear to the student programmer who is going to
be asked to work on the code several years from now.

In my opinion, the code is correct and there is no value in changing
the code to explicitly initialize all the values "just in case" someone
doesn't understand the code; the explicit initialization would just
become a source of potential errors. But for future code maintenance,
it wouldn't hurt to put in a comment indicating that the entire
structure was being initialized; the comment code include a reference
to the specific section of the Standard for those who might be
a little too convinced that the code is mistaken.

If the only reason that a programmer
can't understand a particular C code construct,
is because the programmer doesn't know enough C,
then it's time to learn more C.

What kind of a programmer needs to be told that?
Are child labor laws no longer being enforced?

--
pete
.



Relevant Pages

  • Re: Contructing a dir. tree
    ... Perl programmer to think that variables must be initialized, ... The reason I set it to zero is for the same reason I set ... Again, while *you* may know the initialization is not needed, a novice ... he could be printing the Gutenberg Bible for every directory level. ...
    (comp.lang.perl.misc)
  • Re: How do I delete a folder through code?
    ... programmer assigned to the first member. ... initialized static duration data was stored in the DATA segment, ... the initialization data and copy it to these objects. ... This is an idiomatic way to initialize a data structure whose first member ...
    (microsoft.public.vc.mfc)
  • Re: automatic variables are not initialized correctly?
    ... Fortran ALLOCATABLE in COMMON. ... F77 did not have initialization statements, so it was less confusing there. ... out of context, then it is not clear whether that initialization is done once with an implied SAVE, or whether it is done repeatedly every time the subprogram in invoked. ... fortran, then you might be able to deduce what is most likely, but I think that is asking too much of a programmer, especially a modern programmer who must use several languages and where different languages treat this situation differently. ...
    (comp.lang.fortran)
  • Re: Beginner level VB.NET question
    ... were a C# programmer. ... object to nothing somewhere in the code, but not realizing it is the same ... NullReferenceException;(Am I checking whether my reference Object is ... ' Initialization and other good stuff ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Beginner level VB.NET question
    ... were a C# programmer. ... object to nothing somewhere in the code, but not realizing it is the same ... NullReferenceException;(Am I checking whether my reference Object is ... ' Initialization and other good stuff ...
    (microsoft.public.dotnet.languages.vb)