Thread: need help initializing a dynamic array

  1. #1
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55

    need help initializing a dynamic array

    correct this code for me please..
    Code:
    int main()
    {
        const int max = 5;
        int *array = new int [max];
        array[max] = {23,43,65,45,67};
        return 0;
    }
    i keep getting a 2 complier errors saying:
    32 C:\Dev-Cpp\cpp primer plus 5th edition\main.cpp expected primary-expression before '{' token
    32 C:\Dev-Cpp\cpp primer plus 5th edition\main.cpp expected `;' before '{' token

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    you cannot, use loop
    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. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    int arr[] = {23,43,65,45,67};
    int* p = arr;
    
    cout<<p[0]<<endl;

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    need help initializing a dynamic array
    ooops. You can't. Arrays can only be initialized when they are declared, and you are not "declaring" an array when you create a dynamic array. When you create a dynamic array, you are "declaring" a pointer variable, and then 'new' returns an address, which gets assigned to the pointer variable. No array declaration, no initializer list.
    Last edited by 7stud; 01-12-2007 at 02:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-21-2005, 12:39 AM
  2. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 09:19 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. dynamic memory alloc. for array elements
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2002, 05:14 PM