473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

trouble initialising array

Hi,

is this correct:-

typedef struct
{
long frequency;
} Structure;

Structure my_struct [32] = {0};

specifically the initialisation of the struct. I get warnings about
missing braces. Should there be two sets of braces?
--
Nick Keighley

Jun 21 '07 #1
4 1765
In article <11**********************@p77g2000hsh.googlegroups .com>,
Nick Keighley <ni******************@hotmail.comwrote:
>Hi,

is this correct:-

typedef struct
{
long frequency;
} Structure;

Structure my_struct [32] = {0};

specifically the initialisation of the struct. I get warnings about
missing braces. Should there be two sets of braces?
This initialization is correct and sensible, and warnings about use of
"{0}" as an initializer for aggregate types should be ignored.

If you were attempting to initialize with nonzero values, you'd want to
use more braces for clarity and resistance to changes; but a single zero
will explicitly initialize the first member of the first struct in the
array and implicitly zero-initialize the rest, which is exactly what you
want, and the compiler isn't clever enough to recognize the difference
between the all-zero and the not-all-zero cases.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I presume the difference is that Laurier's off-campus environment includes the
University of Waterloo, whereas the best we can offer by way of off-campus
environment is, well, Laurier. . . . --Chris Redmond in uw.general
Jun 21 '07 #2
On Jun 21, 8:13 am, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
Hi,

is this correct:-

typedef struct
{
long frequency;
} Structure;

Structure my_struct [32] = {0};

specifically the initialisation of the struct. I get warnings about
missing braces. Should there be two sets of braces?
>From ©ISO/IEC ISO/IEC 9899:1999 (E)
§6.7.8 Language page 127:
21 If there are fewer initializers in a brace-enclosed list than there
are elements or members of an aggregate, or fewer characters in a
string literal used to initialize an array of known size than there
are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration.

Jun 21 '07 #3
On 21 Jun, 17:37, dj3va...@csclub.uwaterloo.ca (Dave Vandervies)
wrote:
In article <1182438810.100081.182...@p77g2000hsh.googlegroups .com>,Nick Keighley <nick_keighley_nos...@hotmail.comwrote:
is this correct:-
typedef struct
{
long frequency;
} Structure;
Structure my_struct [32] = {0};
specifically the initialisation of the struct. I get warnings about
missing braces. Should there be two sets of braces?

This initialization is correct and sensible, and warnings about use of
"{0}" as an initializer for aggregate types should be ignored.
note that I have an *array* of structs. Does that make a difference.
This part of my c-code is generated and a warning in there is a little
irritating.
If you were attempting to initialize with nonzero values, you'd want to
use more braces for clarity and resistance to changes; but a single zero
will explicitly initialize the first member of the first struct in the
array and implicitly zero-initialize the rest, which is exactly what you
want, and the compiler isn't clever enough to recognize the difference
between the all-zero and the not-all-zero cases.

Jun 22 '07 #4
Nick Keighley wrote:
>
On 21 Jun, 17:37, dj3va...@csclub.uwaterloo.ca (Dave Vandervies)
wrote:
This initialization is correct and sensible,
and warnings about use of
"{0}" as an initializer for aggregate types should be ignored.

note that I have an *array* of structs. Does that make a difference.
It makes no difference.
{0} is the universal initializer.
Objects of any and all types may be initialized with {0}.

Initializing an array with a single {0},
is the same as initializing each and every element with {0}.

--
pete
Jun 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mattias Brändström | last post by:
Hello all! I am trying to write code that allows me to initialise one of my classes inline (with a vector like structure). Inline might not be the best term to use here but I can't think of any...
12
by: Ney André de Mello Zunino | last post by:
Hello. I have been having some trouble dealing with bit fields. The following is a simple program that demonstrates it. #include <iomanip> #include <iostream> struct instrucao_i {
2
by: Dylan | last post by:
what is the best way of initializing a static std::vector data member with some values? (currently I just push_back some values in the constructor if the size == 0) thanks
8
by: Nick Keighley | last post by:
Hi, is there a way of initialising a vector in a similar fashion to initialising an array? consider: Shape* triangle = new Triangle (); Shape* square = new Square (); Shape* shape_arr =...
6
by: Jeremy Targett | last post by:
Hello, I'm trying to initialise a two-dimensional array of characters, 12 by 60, with each entry maximum 6 characters wide. (It's a fix I'm making to an old program which I learned just enough C...
3
by: mark.bergman | last post by:
Running lint on code initialising a local 2-D array gives a warning. void f(void) { int a = { 0 }; ... lint gives "warning: Partially elided initialisation..." Should this be happening,...
107
by: DaveC | last post by:
I always used to initialise variables at declaration, then a couple of colleagues started telling me it was bad practice and that the compiler should be left to spot the use of uninitilised...
9
by: Jim | last post by:
Hi, I want to declare that that a valarray of a certain name exist at the beginning of some code, but I can't instatiate it until I've read in some parameters later on in a for loop i.e. int...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.