creating structures help

This is a discussion on creating structures help within the C++ Programming forums, part of the General Programming Boards category; hello, I have read the tutorial on strutures and I have a simple question. I would try it out myself ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    creating structures help

    hello,

    I have read the tutorial on strutures and I have a simple question.
    I would try it out myself but don't have a compiler on hand.

    Say I've defined a struture

    Code:
    struct example
    {
      int x;
    };
    example an_example;  //Treating it like a normal variable type
    an_example.x=33;     //How to access it
    Now the above has "created" a variable called an_example to access this particular structure.

    As my program runs it will need to access different instances of the structure so how to I get it to "create/access an instance of structure?

    In in the above code, rather than "an_example" can I actually have an array or something? that way I can access different elements in that array hence different instances of the structure?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Code:
    example an_example[3];
    an_example[0].x = 8;   //1st variable
    an_example[1].x = 32;   //2nd variable
    an_example[2].x = 5;   //3rd variable
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    12
    thanks,

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>what do you mean it does not work?
    That's just his signature. Ignore it.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    12
    lol, I feel so n00bi, I realized it and changed the post but you beet me to it!

    Thanks again to funkydude9

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating structures
    By HAssan in forum C Programming
    Replies: 7
    Last Post: 01-26-2009, 06:36 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  3. Help needed with structures
    By esmeco in forum C Programming
    Replies: 7
    Last Post: 03-08-2008, 01:29 PM
  4. creating structures in functions
    By Marksman in forum C Programming
    Replies: 3
    Last Post: 06-01-2005, 11:56 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21