By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,806 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,806 IT Pros & Developers. It's quick & easy.

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

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

plz reply...i will we thankful to you...
Sep 7 '10 #1
Share this Question
Share on Google+
8 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You need to do this when you define the array:

Expand|Select|Wrap|Line Numbers
  1. const int array[3] = {1,2,3};
Sep 7 '10 #2

Expert 100+
P: 2,400
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|Wrap|Line Numbers
  1. const int array[3];
Sep 7 '10 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
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|Wrap|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.
Sep 7 '10 #4

Banfa
Expert Mod 5K+
P: 8,916
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.
Sep 7 '10 #5

Banfa
Expert Mod 5K+
P: 8,916
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.
Sep 7 '10 #6

Expert 100+
P: 2,400
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?
Sep 7 '10 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
C and C++ are the same in this respect and exhibit the behaviour you have described.
I based my comment on that fact that:

Expand|Select|Wrap|Line Numbers
  1. 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.
Sep 8 '10 #8

Banfa
Expert Mod 5K+
P: 8,916
OK I tried this code

Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2.  
  3. const int array1[3];
  4. const int array2[3] = {5};
  5. const int array3[3] = {5,6,7};
  6.  
  7. void print(const int(*pa)[3])
  8. {
  9.     printf("%d %d %d\n", (*pa)[0], (*pa)[1], (*pa)[2]);
  10. }
  11.  
  12. int main (void)
  13. {
  14.     const int array4[3];
  15.     const int array5[3] = {5};
  16.     const int array6[3] = {5,6,7};
  17.  
  18.     print(&array1);
  19.     print(&array2);
  20.     print(&array3);
  21.     print(&array4);
  22.     print(&array5);
  23.     print(&array6);
  24.  
  25.     return 0;
  26. }
  27.  
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.
Sep 8 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.