C++/array

Advertisement


Question
int i[10];
i[10]=1;

lly char ch[10];
ch[10]='c';

Are these assignment statements valid? I am trying to put the value at the last position. In above declaration as per concept they dont allocate memory to store at the N position  

Answer
No, these assignments are _not_ valid.

You are _not_ putting values in the last array slots, you are putting them in the one past the end position.

This is because in C and C++ arrays are indexed from 0 not 1. Thus all arrays have valid index values from 0 to n-1 inclusive where n is the size (or bound) of the array.

Thus for your arrays where n would be 10 the valid index values are:

   0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Count them – there _are_ still 10 array slots.

The reason for this is to do with efficiency, obviousness and simplicity (from the compiler’s point of view!). Arrays in C and C++ are contiguous chunks of memory. The name of an array is synonymous to a pointer to (i.e. the address of) the first element of the array. The expression array[n] is the same as *(array+n). That is: take the address of the first element, add the index to obtain the address of the nth element, then de-reference the address (meaning to obtain the value at the address).

Thus the first element is obtained by adding zero to the address of the first element and dereferencing. Using an initial index of 1 would only complicate matters for no real benefit.

The value n is scaled by the size of the type of the array elements. This scaling by the size of the pointer type is true of all pointer arithmetic in C and C++. For example, assuming a byte addressed machine, then to access the element at index 3 for an array of byte sized elements (as is likely for your ch array) would add 3*1 to the address of the initial element. But to obtain the address of an element at index 3 in an array of 4 byte integers (as is probable for your i array) would add 3*4, i.e. 12 to the address of the initial element.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.