Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help with structs. (http://www.go4expert.com/forums/help-with-structs-t26321/)

oror84 19Jul2011 22:53

Help with structs.
 
Hi,
I have a problem with sturcts.
I have something like :
Code:

struct stack1
{
  int number;
  struct stack1 *next;
};

I want to create structs as user request.
I mean, i`ll get some number from the user input, and create this number of startcts.
For example, i got the number 3 from the user.
Automaticly create :
stack1 s1; stack1 s2; stack1 s3; ( 3 structs ).
I dont care the structs names.
I hope the explanation was clear, Thanks for helpers.
Or.

xpi0t0s 20Jul2011 11:36

Re: Help with structs.
 
Arrays are your friend, and malloc.
Code:

int i=5; // or get this from the user
struct stack1 *ptr=(struct stack1 *)malloc(sizeof(struct stack1)*i);
// do whatever you want with ptr, e.g.
ptr[3].number=5; // which sets the array's *4th* entry number to 5.
// and DO NOT EVER FORGET:
free(ptr);


oror84 20Jul2011 17:57

Re: Help with structs.
 
Thanks you, you helped me alot :)


All times are GMT +5.5. The time now is 06:34.