Computing.Net > Forums > Programming > internal structudre of Array in C++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

internal structudre of Array in C++

Reply to Message Icon

Name: Adnan
Date: March 4, 2002 at 10:46:24 Pacific
Comment:

hi all
Guide me PLZ..
Why the answer on the current code is same

main()
{
int a[5]={10,20,30,40,50};
cout>&a[0];
}
Explain the answer with the help of internal structure of Array specialy 1st element and the name of Array





Sponsored Link
Ads by Google

Response Number 1
Name: Apple
Date: March 5, 2002 at 14:11:42 Pacific
Reply:

An array is nothing more than a block of memory. This block of memory has a length of 5 (that's your subscript) times the sizeof(int) (which is 4). So, int a[5] is just a block of memory that is 20 bytes long. Nothing spectacular.

Ignoring the fact that your code is probably not going to do anything at all, let's look at a[3].

really, all you're getting with array subscripts is a memory location (that's all any variable is really). a[3] = a + 3 * sizeof(int) or the location of the first byte of the block of memory called a + 12 bytes. Four of these bytes are then returned. That is the integer found in the location a[3].

Unless I got this backward, your memory (starting at a) looks a bit like this:

0a 00 00 00 14 00 00 00 1e 00 00 00 28 00 00 00 32 00 00 00

The location within the data block (or whereever the compiler happened it put it) called "a" starts with the byte containing 0a. a[3] = a + 3 * sizeof(int) = a + 12 looks something like this:
V
0a 00 00 00 14 00 00 00 1e 00 00 00 28 00 00 00 32 00 00 00

and returns this block of data:
0a 00 00 00 14 00 00 00 1e 00 00 00 [28 00 00 00] 32 00 00 00

Now, you're using &a[0]. This is the memory address of the first element of array "a". (This is the same as saying a as a matter of fact.) &a[3] = a + 12. You're actually returning an integer value corresponding to the memory location.

It's all very simple as long as you ignore the qualifiers "int", "char", "double" etc. They really don't mean anything except 4, 1, and 8. (They do help the compiler decide which version of divide or add to use. Assembly actually has addb, addw, addd, addq and so on defining add 1 byte, add 2 bytes, add 4 bytes and add 8 bytes. Doubles and floats use the floating point instructions fdiv and so forth.)

How's that?


0
Reply to Message Icon

Related Posts

See More


Convert Dec To Hex?? how can be restarted !!!!



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: internal structudre of Array in C++

vector of arrays in c++ www.computing.net/answers/programming/vector-of-arrays-in-c/12919.html

How to use string array in C++? www.computing.net/answers/programming/how-to-use-string-array-in-c/9478.html

Dynamic Arrays in C www.computing.net/answers/programming/dynamic-arrays-in-c/3396.html