how can i assign a CONSTANT VALUE TO WHOLE ARRAY..?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dsir
    New Member
    • Sep 2010
    • 1

    #1

    how can i assign a CONSTANT VALUE TO WHOLE ARRAY..?

    how can i assign a CONSTANT VALUE TO WHOLE ARRAY..?

    plz reply...i will we thankful to you...
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You need to do this when you define the array:

    Code:
    const int array[3] = {1,2,3};

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      If perchance the constant you want to assign is zero, then you can take advantage of the fact that the default initializer value is zero:
      Code:
      const int array[3];

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        If perchance the constant you want to assign is zero, then you can take advantage of the fact that the default initializer value is zero:
        Expand|Select|W rap|Line Numbers const int array[3];
        .

        Only in C.

        C++ requires const values to be supplied when the variable is created.

        C++ will have a default initializer of 0 only if the array is static.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Only in C.

          C++ requires const values to be supplied when the variable is created.

          C++ will have a default initializer of 0 only if the array is static.
          C and C++ are the same in this respect and exhibit the behaviour you have described.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            To answer the op, the above answer concern initialisation, setting the variables value as it is created. You can't assign (set a variables value after it has been created) a constant value to a whole array in C; except the value 0 which you could do using the standard library function memset.

            You have more options in C++ for instance std::fill_n from the C++ standard library should work nicely if you want all array entries to have the same value.

            If you want to assign values from some series then std::generate_n could be of use.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              The term static in the earlier replies refers to static duration, not static linkage. That is, it refers to variables that are neither automatic nor heap.

              Question for the OP: when you refer to CONSTANT VALUE, do you mean const or do you mean the same value is stored in each element of the array?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                C and C++ are the same in this respect and exhibit the behaviour you have described.
                I based my comment on that fact that:

                Code:
                const int array[3];
                compiles with Visual Studio.NET 2008 when requested to compile as C but will not compile when requested to compile as C++.

                Apparently, whatever C is used by Visual Studio.NET 2008is not the C to which you are referring.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  OK I tried this code

                  Code:
                  #include "stdio.h"
                  
                  const int array1[3];
                  const int array2[3] = {5};
                  const int array3[3] = {5,6,7};
                  
                  void print(const int(*pa)[3])
                  {
                      printf("%d %d %d\n", (*pa)[0], (*pa)[1], (*pa)[2]);
                  }
                  
                  int main (void)
                  {
                      const int array4[3];
                      const int array5[3] = {5};
                      const int array6[3] = {5,6,7};
                  
                      print(&array1);
                      print(&array2);
                      print(&array3);
                      print(&array4);
                      print(&array5);
                      print(&array6);
                  
                      return 0;
                  }
                  Compiled as C and C++ using gcc.

                  C++ does not like array1 or array4 and raises errors, actually that sort of makes sense as one of those "we can do this better" extensions because what is the point of having a const variable if you do not initialise its value.

                  C compiled it as is and gave the following output

                  0 0 0
                  5 0 0
                  5 6 7
                  11456704 134513947 11345908
                  5 0 0
                  5 6 7

                  This makes the point I was trying to make (which I think we all already know) in a round about way which is that automatic storage class variables are not automatically initialised to 0 where as static/global scope variables are.

                  This appears to be true for const variables too in C. In C++ it is not also true for const variables but because C++ does not allow uninitialised const variables rather than they are initialised to 0 automatically.

                  Comment

                  Working...