Thread: Arrays and use of const

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Arrays and use of const

    I am trying to understand the use of const. I understand that const is used to keep data from being changed. What I don,t understand is how you can use const with values that are not initailly known.
    For example, If I have a list of values that I read in from a file and I create an array declared as
    Code:
    double list[SIZE] = {'\0'};
    and I want to protect that data by making it
    Code:
    const double list[specific length read in]
    .

    Can I do this? AND Can you do it without pointers?

    Is the only way to use const in this manner by initilizing the values with const?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The thing with const is that the values must be known at compile time which is not the case if you read them from a file. I'm sure const is more of an optimization thingy rather than protection as you could always do a const_cast to modify the data :P.

    Initializing an array is done like this:
    Code:
    const double x[] =
    {
      1.2,
      3.4,
      5.6
    };
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed