dynamic arrays

This is a discussion on dynamic arrays within the C++ Programming forums, part of the General Programming Boards category; hello first post here! w00t! anywho, i have a question about dynamic arrays. I've noticed that you need the star ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    13

    dynamic arrays

    hello first post here! w00t! anywho, i have a question about dynamic arrays. I've noticed that you need the star before the title and more stars if you need more than just one dimension. I'm wondering when you put the stars before and after the title.

    For example I've seen:

    const char * const * const maze = yada yada yada

    and then somewhere else:

    *** some type

    I'm trying to understand when to place the stars and the concept behind it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    If by title you mean variable name, note that maze is the name, so the asterisks never go after the variable name. If by title you mean type name, then of course the asterisks come after the type name.

    The trick is to look for the const keyword. If the const comes before or right after the type name, it means that the object pointed to is const. If the const comes right after an asterisk, it means that the pointer denoted by that asterisk is const.

    Oh, and of course the pointer need not point to an array, and if you need a dynamic array, reach for std::vector before reaching for new[] and delete[].
    Last edited by laserlight; 05-12-2008 at 10:30 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    13
    Ah ok. Yeah I meant the type name. :P and right now my prof is making us know how to use dynamic arrays so we're not dabbing into vectors quite yet.

    So my next question is to declare it.
    Here's a static array:
    char variable [x][y][z]

    so for a dynamic would it be:
    * char variable = (x,y,z)

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    the "star" is called the indirection operator. it points to a location in memory, instead of the value of the data located there directly

    what happens is that when you declare an array such as:

    int *a = new int[6];

    a now points to the "base address" of the array of ints you declared.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    I would say that thinking like that will only confuse you.
    The number of * does not determine dimensions. In fact, everything before the * is part of the actual type:

    int* n -> pointer to int
    int** n -> pointer to int*
    int***n -> pointer to int**

    If you want a strict set of dimensions, then I suggest you nest std::vector within each other:
    std::vector< std::vector< std::vector<type> > >
    A 3D array (or vector).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by sokermaniac View Post
    so for a dynamic would it be:
    * char variable = (x,y,z)
    no. that is never valid syntax.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    You might want to read your notes and then try what you think is the answer with a test program before asking.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    13
    Quote Originally Posted by Elysia View Post
    I would say that thinking like that will only confuse you.
    The number of * does not determine dimensions. In fact, everything before the * is part of the actual type:

    int* n -> pointer to int
    int** n -> pointer to int*
    int***n -> pointer to int**

    If you want a strict set of dimensions, then I suggest you nest std::vector within each other:
    std::vector< std::vector< std::vector<type> > >
    A 3D array (or vector).
    haha boy i need to learn vectors! But again we're just using dynamic arrays. So how would i make a 3D dynamic array then?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    In fact, everything before the * is part of the actual type
    Yes, I should have clarified that the type name I was talking about refers to the object pointed to, not the type of the variable declared.

    So how would i make a 3D dynamic array then?
    For starters, how would you create a one dimensional dynamically allocated array?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    13
    int *list;
    int arraysize;

    cout << blah blah
    cin>> arraysize;

    list = new int[arraysize];

    or something like that haha

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    13
    ok so im looking back at my notes and it seems that you would first initialize the first dimension and then make a for loop for the second then make another for loop for the 3rd?

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    If you think of it like this:
    We want a dynamic array, so that means we create it using new because the size is undefined at compile time. Therefore, we get a pointer to that array.
    OK, we have one array then.
    Now all the elements in that array must point to another array -> out 2nd array(s).
    And lastly, all those 2nd arrays elements must have more pointers to your 3rd array.
    And the 3rd array contains your data.

    It might a little tough to wrap your head around at first, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    ok so im looking back at my notes and it seems that you would first initialize the first dimension and then make a for loop for the second then make another for loop for the 3rd?
    I think you are on the right track. As I said, test it out with a program, and then come back and ask if you have a problem or want confirmation that your solution is correct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    13
    yeah a little bit to be honest. but that means to put a for loop on the 2nd pointer and another for loop for the 3rd pointer and another for loop for the nth pointer yes?

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by sokermaniac View Post
    Ah ok. Yeah I meant the type name. :P and right now my prof is making us know how to use dynamic arrays so we're not dabbing into vectors quite yet.
    Strange... I would have thought C would be a pre-requisite for a C++ course?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 10:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  4. dynamic arrays and structures
    By godofbabel in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 03:45 PM

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