472,950 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 software developers and data experts.

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 2640
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rashmi | last post by:
Hi All, ************************************************* error: variable `hdlc_cdevsw' has initializer but incomplete type *************************************************** This is the error...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
3
by: farseer | last post by:
If i have an array of a certain type, is there away of initializing with without knowing it's type? for example (forgive me if some of this is syntactically incorrect) if i have a procedure...
2
by: mahesh_cbrl | last post by:
Hi, I am unable to intialize an array of structures and I keep getting an error. Here is how the data structure looks like. typedef struct { struct { int part_no; char item_name;
2
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hello, Suppose I have defined a structure.... Structure RenameDocs Public Facility As String Public Document As String Public Subtitle As String Public Page As String End Structure
4
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1...
2
by: Daugaard | last post by:
As the title says, I'm trying to put a function pointer in a structure at initialization time. The code below demonstrates this and also includes code for testing whether or not it works. It's fairly...
4
by: Peskov Dmitry | last post by:
class simple_class { int data; public: simple_class() {data=10;}; simple_class(int val) : data(val){} }; int main() {
1
by: dissectcode | last post by:
Hello - Please tell me what this is...It looks like an array/structure/prototype/pointer... STATIC void *_aname = { { &d0, &d1 } , { &d10, &d11 } } ; i've never seen this before please...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.