Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to initialize a structure

Reply
Thread Tools

how to initialize a structure

 
 
Ramprasad A Padmanabhan
Guest
Posts: n/a
 
      10-29-2003
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

 
Reply With Quote
 
 
 
 
Jirka Klaue
Guest
Posts: n/a
 
      10-29-2003
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

 
Reply With Quote
 
 
 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-29-2003
Ramprasad A Padmanabhan <(E-Mail Removed)> 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
((E-Mail Removed))
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      10-29-2003
Ramprasad A Padmanabhan <(E-Mail Removed)> 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
--
_ _____ _____
| ||_ _||_ _| http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
Reply With Quote
 
Kevin Bracey
Guest
Posts: n/a
 
      10-29-2003
In message <bnohft$13rf0i$(E-Mail Removed)-berlin.de>
Ramprasad A Padmanabhan <(E-Mail Removed)> 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/
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-29-2003
In <bnohft$13rf0i$(E-Mail Removed)-berlin.de> Ramprasad A Padmanabhan <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-30-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Initializing an array of a structure to default value, does compiler do a loop to initialize? vduber6er C++ 3 10-24-2006 02:03 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
How to initialize and array in a structure? Chad C Programming 16 12-14-2005 02:45 PM
initialize data structure or read text file Joan Java 11 08-31-2005 09:53 PM



Advertisments