Initializing array of structure!!

  • Thread starter Chandrashekar Tippur
  • Start date
C

Chandrashekar Tippur

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
 
N

Neo

Chandrashekar Tippur said:
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;
MYARRAY.p = NULL;
}
for(i=0; i < ELEMENT_COUNT; i++)
printf("%d : %p\t", MYARRAY.i, MYARRAY.p);

free(MYARRAY);
}
return 0;
}

-Neo
 
M

Method Man

Chandrashekar Tippur said:
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 said:
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]
 
P

Peter Smithson

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);
}
 
M

Mark McIntyre

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.
 
J

Jack Klein

Chandrashekar Tippur said:
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 said:
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.
 

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,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top