Path: utzoo!utgpu!water!watmath!clyde!att!pacbell!ames!pasteur!helios.ee.lbl.gov!nosc!ucsd!orion.cf.uci.edu!paris.ics.uci.edu!bonnie.ics.uci.edu!schmidt From: schmidt@bonnie.ics.uci.edu (Douglas C. Schmidt) Newsgroups: comp.lang.c++ Subject: const applied to arrays in C++ Message-ID: <658@paris.ICS.UCI.EDU> Date: 30 Aug 88 21:12:22 GMT Sender: news@paris.ics.uci.edu Reply-To: schmidt@bonnie.ics.uci.edu (Douglas C. Schmidt) Organization: University of California, Irvine - Dept. of ICS Lines: 50 Howdy, Is the following legal in C++? ------------------------------ const int array[] = {4,5,6,7,8}; main() { array[3] = 8; } ------------------------------ Basically, my question is: ``what does const modify in this context?'' Obviously, ``array'' is already a constant, so is const is potentially redundant. On the other hand, if it applies to the indexed objects, as it does with pointers, then the assignment to array[3] should be illegal. (If you inspect pages 62 and 63 of Stroustrup you'll see that he doesn't seem to define what occurs with applying const to arrays (as opposed to pointers)). Clearly, ------------------------------ const int *a = array; ... a[3] = 7; ------------------------------ is illegal. But the G++ compiler accepts the earlier code without hesitation. It seems to me that the const in this case should apply to the object, to be consistent with the similar const int *a; usage (arrays and pointers being quite similar). Can someone please clarify this? If G++ is correct in accepting this construct then is there a mechanism for declaring an array of k constants? thanks, Doug Schmidt -- schmidt@bonnie.ics.uci.edu (ARPA)