problem on dynamic array

This is a discussion on problem on dynamic array within the C++ Programming forums, part of the General Programming Boards category; This is the code I saw in mock paper. Code: struct Ant { int age, strength; }; int n; cin ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    problem on dynamic array

    This is the code I saw in mock paper.
    Code:
    struct Ant
    {
         int age, strength;
    };
    int n;
    cin >> n;
    Ant *AntArmy;
    
    AntArmy = new Ant[n];//can't get why the structure name can be the array name
    AntArmy[0].age=1;
    AntArmy[0].strength=5;
    What is the difference between class & struct?
    Why is the structure name used as the array name?
    What 's going on with AntArmy = new Ant[n];?
    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    The difference between class and struct is that everything in a class is private by default, but everything in a struct is public by default. Other than that a C++ class and a C++ struct are identical.

    The structure name is not used as an array name. The line "AntArmy = new Ant[n];" is an instruction to dynamically allocate and construct and array of n Ant's, and then give AntArmy a value which is the address of the first element in that dynamically allocated array.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    To add to what grumpy said, pay attention to these two lines

    Code:
    Ant *AntArmy;
    AntArmy = new Ant[n];
    AntArmy is first declared to be a pointer to the Ant type.
    Then AntArmy is assigned the the first element of a dynamically allocated array of Ant types.

    The expression new Ant[n] allocates an array of Ant types and returns a pointer to the first element

    AntArmy is assigned that pointer.

    That would have been the same as simply writing:

    Code:
    Ant* AntArmy = new Ant[n];
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thank you for your clear explanation, I got it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 01:51 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Dynamic array problem
    By new here in forum C++ Programming
    Replies: 9
    Last Post: 03-12-2003, 05:39 PM
  4. Dynamic Array Problem
    By adc85 in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2003, 01:29 PM
  5. Dynamic array problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2002, 09:55 AM

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