how to initialize a structure

  • Thread starter Ramprasad A Padmanabhan
  • Start date
R

Ramprasad A Padmanabhan

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
 
J

Jirka Klaue

Ramprasad said:
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
 
I

Irrwahn Grausewitz

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
 
J

Jens.Toerring

Ramprasad A Padmanabhan said:
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
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
K

Kevin Bracey

In message <[email protected]>
Ramprasad A Padmanabhan said:
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}
};
 
D

Dan Pop

In said:
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
 
P

pete

Ramprasad said:
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 */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top