need help initializing a dynamic array

This is a discussion on need help initializing a dynamic array within the C++ Programming forums, part of the General Programming Boards category; correct this code for me please.. Code: int main() { const int max = 5; int *array = new int ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,453
    you cannot, use loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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,662
    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 01: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-20-2005, 11:39 PM
  2. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 08: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

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