473,396 Members | 2,030 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,396 software developers and data experts.

how to initialize a structure

I have a simple structure defined like this

struct userid {
char uid[MAXUIDLENGTH];
int insize;
int outsize;
};
typedef struct userid user;
Now I want to define a user array and initialize it

I tried something like this but doesnt work

user *list[] = {
{"user1",10,20},
{"user2",0,20},
{"user3",11,2}
};
Is there a way I can initialize this array while its declaration.

Thanks
Ram

Nov 13 '05 #1
6 54954
Ramprasad A Padmanabhan wrote:
I have a simple structure defined like this

struct userid {
char uid[MAXUIDLENGTH];
int insize;
int outsize;
};
typedef struct userid user;

Now I want to define a user array and initialize it

I tried something like this but doesnt work

user *list[] = { ^
Drop this.
{"user1",10,20},
{"user2",0,20},
{"user3",11,2}
};

Is there a way I can initialize this array while its declaration.


struct userid {
char uid[MAXUIDLENGTH];
int insize, outsize;
} list[] = {
{"user1", 10, 20},
{"user2", 0, 20},
{"user3", 11, 2}
};

Jirka

Nov 13 '05 #2
Ramprasad A Padmanabhan <ra*******@netcore.co.in> wrote:

<snip>
I tried something like this but doesnt work

user *list[] = {
{"user1",10,20},
{"user2",0,20},
{"user3",11,2}
};


Drop the spurious * and you'll be fine.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #3
Ramprasad A Padmanabhan <ra*******@netcore.co.in> wrote:
I have a simple structure defined like this struct userid {
char uid[MAXUIDLENGTH];
int insize;
int outsize;
};
typedef struct userid user; Now I want to define a user array and initialize it I tried something like this but doesnt work user *list[] = {
This declares an array of *pointers* to such structures, but not
an array of structures. I guess you should get rid of the '*' in
front of 'list'.
{"user1",10,20},
{"user2",0,20},
{"user3",11,2}
};


And that's how you would initialize an array of structures. That's
ok when you remove the '*'.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #4
In message <bn*************@ID-166953.news.uni-berlin.de>
Ramprasad A Padmanabhan <ra*******@netcore.co.in> wrote:
I have a simple structure defined like this

struct userid {
char uid[MAXUIDLENGTH];
int insize;
int outsize;
};
typedef struct userid user;
Now I want to define a user array and initialize it

I tried something like this but doesnt work

user *list[] = {
{"user1",10,20},
{"user2",0,20},
{"user3",11,2}
};
Is there a way I can initialize this array while its declaration.


You can make list an array of "user"s rather than an array of "user *"s,
as others have suggested. Alternatively, if you really do want an array of
pointers, in C99 you can do it with compound literals:

user *list[] = {
&(user) {"user1",10,20},
&(user) {"user2",0,20},
&(user) {"user3",11,2}
};

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 13 '05 #5
In <bn*************@ID-166953.news.uni-berlin.de> Ramprasad A Padmanabhan <ra*******@netcore.co.in> writes:
I have a simple structure defined like this

struct userid {
char uid[MAXUIDLENGTH];
int insize;
int outsize;
};
typedef struct userid user;
Don't typedef struct's, unless you have a *good* reason for doing it.
Saving a few keystrokes in declarations doesn't count as a good reason.

All you can achieve with gratuitous typedef's is render the code less
readable.
Now I want to define a user array and initialize it

I tried something like this but doesnt work

user *list[] = {
{"user1",10,20},
{"user2",0,20},
{"user3",11,2}
};

Is there a way I can initialize this array while its declaration.


You need to learn how to declare things in C. Your initialiser is OK,
your declaration isn't. You're declaring an array of pointers to user,
NOT an array of user.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
Ramprasad A Padmanabhan wrote:

I have a simple structure defined like this

struct userid {
char uid[MAXUIDLENGTH];
int insize;
int outsize;
};
typedef struct userid user;

Now I want to define a user array and initialize it

I tried something like this but doesnt work

user *list[] = {
{"user1",10,20},
{"user2",0,20},
{"user3",11,2}
};

Is there a way I can initialize this array while its declaration.


Program output:
C:\Program Files\DevStudio\SharedIDE\bin\Debug>new
user1, 10, 20
user2, 0, 20
user3, 11, 2

/* BEGIN new.c */

#include <stdio.h>

#define LIST \
{ \
{"user1",10,20}, \
{"user2", 0,20}, \
/* { "EXP",99,99}, \
*/ {"user3",11, 2} \
/* {"user4",10,20}, \
*/}
#define MAXUIDLENGTH (sizeof "user4")
#define STRUCTURES (sizeof array/ sizeof *array)

int main(void)
{
struct userid {
char uid[MAXUIDLENGTH];
int insize;
int outsize;
} array[] = LIST;
size_t structure;

for (structure = 0; structure != STRUCTURES; ++structure) {
printf("%s, %2d, %2d\n",
array[structure].uid,
array[structure].insize,
array[structure].outsize);
}
return 0;
}

/* END new.c */

--
pete
Nov 13 '05 #7

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

Similar topics

6
by: steveneng | last post by:
C++ Primer Plus Programming Exercises 4th Ed - Prate Help I'm trying to refresh myself and I'm stuck on this problem (not homework/school related but for personal advancement). 6: Do...
6
by: Kai Wu | last post by:
#include <string.h> #include <fstream> #include <time.h> typedef unsigned char BYTE; struct Dex { BYTE status; struct timeval timestamp; }; int main(){
7
by: Andi.Martin | last post by:
Hi, how is it possible, to only initialize parts of a structure. Example: typedef struct{ int a, b; ... (huge lot of members); double x,y;
18
by: vib | last post by:
Hi there, By chance, I came to learn that it is bad programming practice to initialize global variables at outside of programs. Is it that bad? In order to fullfil this, I had to declare them...
5
by: Bill Nicholson | last post by:
I declared a UDT... Structure TeamRankingsStruc Public intRankings() As Int16 ' The dimension must be omitted in the context of a data type definition Public strName As String End...
1
by: Ghislain Tanguay | last post by:
Hi, I have two structures. One containing(EmailMessage) an array of the other(AttFile). When I try to work with in my VB.NET app iIalways receive an error off this type Object reference not set to...
3
by: vduber6er | last post by:
Lets say I have this structure: typedef struct numbers { double first = 0.0; double second = 0.0; double third = 0.0; } MYVALUES; and I initialize an array of MYVALUES like the following
5
by: Bob Altman | last post by:
Hi all, I have a private static structure in a C++ class (it's a CRITICAL_SECTION structure) that needs to be initialized by passing its address to a routine (InitializeCriticalSection). Since...
3
by: Bob Altman | last post by:
Hi all, If I have a class that includes an instance of a struct as a member, how do I initialize that struct? I can't find a syntax for the constructor "initializer list" that works. For...
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...
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...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.