468,727 Members | 1,601 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,727 developers. It's quick & easy.

Initializing array of structure!!

All,

I need to initialize an array of structures but I don't know how may
elements are there. I tried to malloc the array but I am not sure how
to initialize them.

Snippet:
struct myarray{
int conf;
char * var;
};

int main(int args, char * argv[]){
struct myarray *MYARRAY[]= malloc(sizeof(struct myarray) *
30);
MYARRAY={{1,"x"}, {2,"y"} ...... }; // How do I initialize
this?
}

This comes out with segmentation fault. How do I do this?

- Shekar
Nov 14 '05 #1
7 2440
Neo

"Chandrashekar Tippur" <ct*****@msn.com> wrote in message
news:62**************************@posting.google.c om...
All,

I need to initialize an array of structures but I don't know how may
elements are there. I tried to malloc the array but I am not sure how
to initialize them.

Snippet:
struct myarray{
int conf;
char * var;
};

int main(int args, char * argv[]){
struct myarray *MYARRAY[]= malloc(sizeof(struct myarray) *
30);
MYARRAY={{1,"x"}, {2,"y"} ...... }; // How do I initialize
this?
}

This comes out with segmentation fault. How do I do this?

- Shekar


You can do it like this :

#include<stdio.h>
#include<stdlib.h>

#define ELEMENT_COUNT 4

struct mystruct {
int i;
char *p;
};

int main()
{
int i;
struct mystruct *MYARRAY = NULL;
if(MYARRAY = malloc(sizeof(struct mystruct) * ELEMENT_COUNT))
{
for(i=0; i < ELEMENT_COUNT; i++)
{
MYARRAY[i].i = i;
MYARRAY[i].p = NULL;
}
for(i=0; i < ELEMENT_COUNT; i++)
printf("%d : %p\t", MYARRAY[i].i, MYARRAY[i].p);

free(MYARRAY);
}
return 0;
}

-Neo
Nov 14 '05 #2

"Chandrashekar Tippur" <ct*****@msn.com> wrote in message
news:62**************************@posting.google.c om...
All,

I need to initialize an array of structures but I don't know how may
elements are there.
In your posted code, you seem to know the element count (30).
I tried to malloc the array but I am not sure how
to initialize them.
You're initialization technique works, but you've incorrectly declared your
array. See below.
Also, make sure to include this:

#include<stdio.h>

Snippet:
struct myarray{
int conf;
char * var;
};

int main(int args, char * argv[]){
struct myarray *MYARRAY[]= malloc(sizeof(struct myarray) *
30);


Change that to:
struct myarray *MYARRAY = malloc(sizeof(MYARRAY) * 30);

[snip]
Nov 14 '05 #3

"Method Man" <a@b.c> wrote in message
news:PX****************@read1.cgocable.net...

"Chandrashekar Tippur" <ct*****@msn.com> wrote in message
news:62**************************@posting.google.c om...
All,

I need to initialize an array of structures but I don't know how may
elements are there.


In your posted code, you seem to know the element count (30).
I tried to malloc the array but I am not sure how
to initialize them.


You're initialization technique works,


Oops, no it doesn't.
Nov 14 '05 #4

"Method Man" <a@b.c> wrote in message
news:PX****************@read1.cgocable.net...

"Chandrashekar Tippur" <ct*****@msn.com> wrote in message
news:62**************************@posting.google.c om...
All,

I need to initialize an array of structures but I don't know how may
elements are there.


In your posted code, you seem to know the element count (30).
I tried to malloc the array but I am not sure how
to initialize them.


You're initialization technique works,


Oops, no it doesn't.
Nov 14 '05 #5
In article <62**************************@posting.google.com >,
ct*****@msn.com says...
All,

I need to initialize an array of structures but I don't know how may
elements are there. I tried to malloc the array but I am not sure how
to initialize them.


How about -

struct myarray{
int conf;
char * var;
};

int main(int args, char * argv[])
{
struct myarray MYARRAY[]= {{1,"x"}, {2,"y"}};
/* Check that it worked - */
printf("%d %s\n", MYARRAY[0].conf, MYARRAY[0].var);
printf("%d %s\n", MYARRAY[1].conf, MYARRAY[1].var);
}
--
http://www.beluga.freeserve.co.uk
Nov 14 '05 #6
On 18 Nov 2004 22:25:46 -0800, in comp.lang.c , ct*****@msn.com
(Chandrashekar Tippur) wrote:
All,

I need to initialize an array of structures but I don't know how may
elements are there. I tried to malloc the array but I am not sure how
to initialize them.

Snippet:
struct myarray{
int conf;
char * var;
};

int main(int args, char * argv[]){
struct myarray *MYARRAY[]= malloc(sizeof(struct myarray) * 30);
MYARRAY is an array of pointers to struct myarray. You don't want that.
You want an array of structs.... so remove the square brackets.

Also, by convention ALL CAPS is reserved for macro names. Its bad practice
to use it for variables as it will confuse code maintainers later.

MYARRAY={{1,"x"}, {2,"y"} ...... }; // How do I initialize this?


If its dynamically created, you must dynamically assign values to it by
looping. Since you know how large it is, you could however have done

struct myarray TheArray[30] = {{1,"x"},{2,"y"}};
which would have populated the 1st to elements of the array, and set the
rest to zero or null.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #7
On Fri, 19 Nov 2004 04:23:04 -0500, "Method Man" <a@b.c> wrote in
comp.lang.c:

"Chandrashekar Tippur" <ct*****@msn.com> wrote in message
news:62**************************@posting.google.c om...
All,

I need to initialize an array of structures but I don't know how may
elements are there.
In your posted code, you seem to know the element count (30).
I tried to malloc the array but I am not sure how
to initialize them.


You're initialization technique works, but you've incorrectly declared your
array. See below.
Also, make sure to include this:

#include<stdio.h>

#include <stdlib.h>

Snippet:
struct myarray{
int conf;
char * var;
};

int main(int args, char * argv[]){
struct myarray *MYARRAY[]= malloc(sizeof(struct myarray) *
30);


Change that to:
struct myarray *MYARRAY = malloc(sizeof(MYARRAY) * 30);


No, no, NO!!!

struct myarray *MYARRAY = malloc(sizeof *MYARRAY * 30);

The OP wants an array of 30 structures, not 30 pointers to struct.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Steve | last post: by
2 posts views Thread by mahesh_cbrl | last post: by
2 posts views Thread by =?Utf-8?B?TWFyaw==?= | last post: by
4 posts views Thread by benn686 | last post: by
4 posts views Thread by Peskov Dmitry | last post: by
1 post views Thread by CARIGAR | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.