Thread: What's the best way to initialize a large const array?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    What's the best way to initialize a large const array?

    I have an array that looks like this in my header,

    Code:
    const char a[100];
    obviously that's too big for me to write out all 100 elements in the header file, I would much rather do it with a for loop(especially when each element is not the same). I am wondering how I can initialize the array in my .cpp file without the compiler complaining about not initializing a const variable. Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I don't believe there is a way. You could either just go ahead and initilize it manually in the header file, or strip it of it's const status.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I am wondering how I can initialize the array in my .cpp file without the compiler complaining about not initializing a const variable.
    A variable wouldn't be too constant if you could change it's value, now would it?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    just put it in a header file - that's what they're for. if you REALLY don't feel like typeing it out, and it can be initialized using a for loop, then just whip up a little prog to write it to a file, then copy and paste. (don't forget take indention into consideration either)
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    You could create an array of pointers to const char, that way you could initialize them at run-time using "new" with a file or loop or whatever other method you had planned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. const array?
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 07-24-2006, 07:03 AM
  3. Representing a Large Integer with an Array
    By random_accident in forum C Programming
    Replies: 3
    Last Post: 03-03-2005, 08:56 PM
  4. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM