gpsd-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fw: [Git][gpsd/gpsd][master] 2 commits: serial.c: Fix some warnings.


From: Fred Wright
Subject: Re: Fw: [Git][gpsd/gpsd][master] 2 commits: serial.c: Fix some warnings.
Date: Fri, 10 Sep 2021 14:10:47 -0700 (PDT)
User-agent: Alpine 2.23 (OSX 453 2020-06-18)


On Fri, 10 Sep 2021, Gary E. Miller wrote:

699108bc by Fred Wright at 2021-09-10T00:37:27-07:00
gpsd.c: Fix an initializer warning.

Since devices is an array of structs, it needs extra braces in the
initializer to refer to a single element.

TESTED:
Warning is gone.


Yes, but the intent was to initialize the entire array, not just
a single element.

The C standard states that if any element of a composite variable is initialized, then all unspecified elements are initialized to 0. That's especially useful for auto variables.

-static struct gps_device_t devices[MAX_DEVICES] = {0};
+static struct gps_device_t devices[MAX_DEVICES] = {{{0}}};

The first form is equating an entire array of structs to a single scalar 0, which is technically a type mismatch, even if some compilers accept it. The second form initializes the first struct element of the first array element to 0, and lets the compiler implicitly initialize the rest to 0.

Since static forces devices to be all zeros, best to just do this:

static struct gps_device_t devices[MAX_DEVICES];

Also OK (as mentioned in the preexisting comment) but adding the braces was more consistent with what was already there.

What compiler?

Clang, which tends to be pickier about such things.

My understanding of the C standard is that {0} will initialize anything to all zeros. That is what gcc does.

See above.

Fred Wright



reply via email to

[Prev in Thread] Current Thread [Next in Thread]