Help with structs.

Discussion in 'C' started by oror84, Jul 19, 2011.

  1. oror84

    oror84 New Member

    Joined:
    Jan 15, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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);
    
     
  3. oror84

    oror84 New Member

    Joined:
    Jan 15, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks you, you helped me alot :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice