Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer to array of structures 1

Status
Not open for further replies.

AQWQ

Programmer
Aug 5, 2001
33
IN
Dear all,

I need to know the how do i allocate memory to structure array dynamically.I tried this but couldn't help me..

struct A{
int i;
};

void main()
{

struct A *a1;
int i;
for(i=0;i<10;i++){
a1=(struct A*)malloc(sizeof(struct A*));
}
}

when i tried to compile this it threw some error..wud appreciate if someone could help me!!

Thanx in advance, Santhosh Ravindran
 
Try this:

struct A{
int i;
};

void main()
{

struct A *a1;
int array_size=10;
a1 = malloc(array_size*sizeof(struct A))
}

 
Code:
struct A{
  int i;
};

void main(){
  struct A *a1;
  int array_size=10;
  a1=(A*)calloc(array_size,sizeof(int));
}

//that should do it, although the int might not be what you
//are looking for.  sizeof(A) should work but I remember
//that can be a bit quirky.
//here is the function documentation for calloc
void *calloc( 
   size_t num,
   size_t size 
);

//whenever dynamicaly allocating an array calloc is the way
//to go

//GOOD LUCK!
MYenigmaSELF:-9
myenigmaself@myenigmaself.gaiden.com
&quot;If debugging is the process of removing bugs, then programming must be the process of putting them in.&quot; --Dykstra
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top