only const could be passed to arrays ?

This is a discussion on only const could be passed to arrays ? within the C++ Programming forums, part of the General Programming Boards category; My book said array could only accept const argument, I tested it and found variable could also do. any ideas ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    only const could be passed to arrays ?

    My book said array could only accept const argument, I tested it and found variable could also do. any ideas ?

    this works pretty fine except the number are not what we expeted because of not be initialized.
    PHP Code:
    #include <iostream>

    void main()
    {
      
    int i;
      
    int t=5;
      
    int arr[t];
      for(
    i=0i<ti++)
      {
        
    cout << arr[i] << endl;
      }

    Never end on learning~

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Arrays need to know their size at compile time. There are numerous ways to get around it (resizing the arrays with dynamic memory, just using as big as you could ever need constant, using std::vector). For the time being, I'd recommend just using a large enough constant integer for the size. If you keep reading the book I am sure it will get into pointers, dynamic memory, and (hopefully, if it's a worthwhile book), get into the stl with the std::vector class.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by SilentStrike
    Arrays need to know their size at compile time. There are numerous ways to get around it (resizing the arrays with dynamic memory, just using as big as you could ever need constant, using std::vector). For the time being, I'd recommend just using a large enough constant integer for the size. If you keep reading the book I am sure it will get into pointers, dynamic memory, and (hopefully, if it's a worthwhile book), get into the stl with the std::vector class.
    I didnt reach those chapter now. I'll read more. thanx~
    Never end on learning~

  4. #4
    Unregistered
    Guest
    So.. maybe your code is working this time, but what in another one?
    It is safer to use really constant size for an array(if you want an constant, not dynamic array), because your code maybe won't work with another compiler..

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Well you can use the keyword new to dynamicaly allocate array space..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The so annoying LNK2005... please help!
    By Mikey_S in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2009, 03:22 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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