473,809 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array/Struct Initialization Confusion

Hey, quick question for anyone willing to listen.

I've always wondered why I can initialize an array of structs with
something like this (where "..." represents the 14 separate values for
the individual DIALOG struct) and it wll compile perfectly:

DIALOG darray[] = { {...}, {...}, {...}, {...}, {...} };

but something like this gives a syntax error (specifically: expected
primary-expression before '{' token):

DIALOG darray[5];
dialog = { {...}, {...}, {...}, {...}, {...} };

If I use a DIALOG darray[5] declaration (like if I wanted to declare
it globally) I always have to dereference every DIALOG in the array
(0-4) and then assign each of the 14 values for the DIALOG struct
individually, which is a major drag. I would much rather use a
bracketed list like in the first example, only I need "darray" to be
global.

Can someone please explain what the difference is and why the latter
is incorrect? Or, if you need more specific information, just let me
know.

Thanks in advance,
keeg
Jun 27 '08 #1
2 1875
In article <daafc171-591b-45af-a5d4-9eb4c651870b@
56g2000hsm.goog legroups.com>, da***********@g mail.com says...
Hey, quick question for anyone willing to listen.

I've always wondered why I can initialize an array of structs with
something like this (where "..." represents the 14 separate values for
the individual DIALOG struct) and it wll compile perfectly:

DIALOG darray[] = { {...}, {...}, {...}, {...}, {...} };

but something like this gives a syntax error (specifically: expected
primary-expression before '{' token):

DIALOG darray[5];
dialog = { {...}, {...}, {...}, {...}, {...} };
[ ... ]
Can someone please explain what the difference is and why the latter
is incorrect? Or, if you need more specific information, just let me
know.
The first is initialization; the second is assignment. Initialization of
arrays is allowed, but assignment of arrays is not allowed.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #2
So what I would call the bracket notation is only allowed in
initialization, not in assignment. Thanks for the quick response!
Jun 27 '08 #3

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

Similar topics

41
8344
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by reference?? Thanks, BB
16
2582
by: Steven T. Hatton | last post by:
As far as I know, there is no way to provide a default value for the argument to the constructor A::A(char (&array)) in this example. Correct? struct A{ A(char (&array) ){ std::copy(&array,&array,&_array); } char _array; };
8
3691
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
2
2467
by: kimimaro | last post by:
hi I wonder if array can be work along with structure? Below are the declaration of my structure struct employee{ char ID; char Name; char Department;
15
4908
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in the program I wish to return this array to its startup state, can this be accomplished by writing binary zeroes to the entire memory block with memset(). E.g., static struct mystruct_st { int x1;
10
3039
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
4
5071
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
2
8375
by: Antti Karanta | last post by:
Hi! Is it possible to inline initialize a struct whose one member is a string array of arbitrary length (terminated w/ a NULL ptr)? What I mean is something like this: typedef struct { char** x ; int y ;
6
2947
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on there position in the array. This should be possible but I can't figure out exactly how to accomplish this. The below code should better illustrate what I'm trying to do: template <int I> class Item
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.