473,382 Members | 1,362 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,382 software developers and data experts.

Initialization of array of structures

Is there any way I can initialize the value of a single member of a structure in the declaration of an array of these structures?

Eg.

struct StructEx
{
int a;
int b;
}

main()
{
struct StructEx asEx[100] = <??>;
}

I want to initialize 'b' to 0 in all the structures. Is this possible?
Jul 8 '06 #1
2 8539
Banfa
9,065 Expert Mod 8TB
C or C++?

In C++ you could
#
Expand|Select|Wrap|Line Numbers
  1. struct StructEx
  2. {
  3. int a;
  4. int b;
  5.  
  6. StructEx()
  7. {
  8.     b=0;
  9. };
  10. }
  11.  
I.E. add a constructor to do it for you.

In C or C++ I think if you do something like

Expand|Select|Wrap|Line Numbers
  1.  struct StructEx asEx[100] = {0};
  2.  
It will initialise the entire array of structures to 0 or you can do it explicitly

Expand|Select|Wrap|Line Numbers
  1.  struct StructEx asEx[100];
  2.  
  3.  memset( asEx, 0, sizeof asEx );
  4.  
Jul 10 '06 #2
cdev
2
Hi,
I am also facing similar kind of problem...
I have created a structure and created a global variable of that structure.
After creation of the variable, in the next line I was trying to initialize one member of that struture. However, in CodeWarrior compiler, i was getting declaration syntax error for that statement.

However, when i wrote the same statement inside a function, i was able to compile teh code successfully.

I didnt get why this is happening....
Could any one help me to sort this problem?

here is an example:
//////////////////////// aa.h //////////////////////////
typedef struct
{
int a;
int b;
} St;

////////////////////////// bb.c //////////////////////////////
#include "aa.h"

St str_bb;
str_bb.a = 100;

void func (void)
{
....
...
}

*********************************************
i was getting the compilation error at the statement: str_bb.a = 100;

Cant we initialize single element of a structure variable gloabally?

The strange thing was, when i put that statement inside the function body of func(), i was able to compile the code.....
Aug 24 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
6
by: aruna | last post by:
Why int *k={1,2,3} illegal in C?
2
by: rmathieu | last post by:
Hi, I want to initialize a static String array in MC++. What I want to do is to initialize my String array like the C# way: new String {"11", "22"} but I could not find an equivalent in MC++. The...
6
by: Daniel Rudy | last post by:
Hello Group. Please consider the following code: /* this table is used in the wipedevice routine */ static const struct wipe_t { uchar wte; /* wipe table entry */ } wipetable = {...
8
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to...
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
17
by: jb.simon | last post by:
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...
17
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to...
3
by: nexusdarkblue | last post by:
Hello everyone, I am somewhat new to C programming (I started last year) and I'm fairly good at it so far...that is, until I've recently started working with structures. In my current program,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.