Thread: HELP!! Urgent !!

  1. #16
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Compile with Visual C++.

    At compile time, we may not know how many elements are need to be allocated.
    So, we would not know what array size to be defined. If that is the case, how are we going to dynamically allocate the array of pointers?

  2. #17
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Why do you need an array? use vectors
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Code:
    int* someData;
    
    //at one point you know how many elements you need
    someData = new int[ELEMENTS];
    
    //fill the array
    delete [] someData;

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There are few if any reasons to use the dynamic array in indigo0086's example over a vector. This does basically the same thing as that. You can also use functions like push_back or resize to make things easier on yourself.
    Code:
    vector<int> someData(ELEMENTS);

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    1 quick question:
    Code:
    menuItem_t **ptr_1;
    and
    Code:
    menuItem_t *ptr_1[];
    Are they the same?
    As you might have guessed, but no one's actually said, the second one shouldn't compile. Unless it's a parameter to a function, in which case both of those code samples are the same. After all, this
    Code:
    int main(int argc, char *argv[])
    is the same as this
    Code:
    int main(int argc, char **argv);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Quote Originally Posted by indigo0086 View Post
    Code:
    int* someData;
    
    //at one point you know how many elements you need
    someData = new int[ELEMENTS];
    
    //fill the array
    delete [] someData;

    What if the data type is a structure?for eg: menuItem_t
    Is the below definition correct?
    Code:
    menuItem_t **someData;
    
    someData = new (*menuItem_t)[MAXSIZE];
    
    delete [] someData;

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There are still few if any good reasons to go this route rather than use a vector, but if you are more comfortable with it (or the other person is making the decision) then the syntax would be:
    Code:
    menuItem_t **someData;
    
    someData = new menuItem_t*[MAXSIZE];
    
    // later, when you are done:
    delete [] someData;
    However, using MAXSIZE for the size of the array is something you usually do with static arrays, not dynamic ones. You would use the same code you had above with the 5, but instead of 5 you would use a constant like MAXSIZE.

    If MAXSIZE is not constant, then the code I just posted would work. Remember also that the pointers aren't initialized, so you'll have to do that yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  2. beginner plzz help urgent
    By sara101 in forum C Programming
    Replies: 11
    Last Post: 01-14-2007, 10:38 PM
  3. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM