473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

array[2] == 2[array], why is that?

121 100+
Hello everyone:

Recently we were given this example:

int array[10]={0,1,2,3,4,5,6,7,8,9};

Of course if you printf("%d", array[2]); the output would be 2.

But our tutor raised such a confusing debate:

in math x + 2 = 2 + x.
and we knew array is but a pointer to int.
therefore array[2] means the location of the address of array plus two memory unit for int.

Thus

array[2] = array +2 = 2 + array = 2[array]

Given this formula, we were astonished to see the output of
printf("%d", 2[array]) is identical to the previous one.


We accepted this but at least for me, I really don't know why...
Sep 22 '07 #1
4 1577
Ganon11
3,652 Expert 2GB
Umm...wow. That's surprising, but it makes sense. So array is really just an address in memory, for sake of argument let's call this address 282. Saying array[2] is like saying "Go to the address pointed to by array, go 2 slots further, and use the information there." Substituting in the value of array, you get the memory location 284.

Reversing that, you take the memory location 2 and add the address pointed to by array, and you get 284.

I guess.
Sep 22 '07 #2
mattmao
121 100+
This makes fully sense.

However, wouldn't the OS block you from getting the address of "2"?

Or is it true that the Os automatically reverse the process and locate the address of array in memory prior to shifting two units?

If that is the case, then this rare expression would be a trick for us.
Sep 22 '07 #3
hey,
array+2 and 2+aray is not same as array[2] its a mistake.It should be like this
*(array+2) and *(2+array) respectively.the internal representation of array is converted as *(array+2) so array[2] and 2[array] are same.
Sep 23 '07 #4
Banfa
9,065 Expert Mod 8TB
However, wouldn't the OS block you from getting the address of "2"?
It doen't try to get the address of 2, it uses 2 as a number in calculating the required address.
Sep 23 '07 #5

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

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
0
by: sjaak538 | last post by:
Hello I've a question about printing complex array's with print_r $xmlC->obj_data I get this example bellow But can anybody give me one example on how to echo $xmlC->obj_data; I try'd with...
3
by: Paul Kirby | last post by:
Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
0
by: Marc van Boven | last post by:
I'm stuck with the following problem: My nusoap-client calls a server-function giveCombination(). The function giveCombination should return something like array( => array( 'a_id' => 6,...
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
19
by: Henry | last post by:
I finally thought I had an understanding of multi dimensional arrays in C when I get this: #include <stdio.h> #define max_x 3 #define max_y 5 int array;
7
by: Eric Boutin | last post by:
Hi ! I would like to generate an array of type char; I just dont really figure out how I could do it with malloc or calloc.. I mean.. I know how to allocate a simple array with both of them;...
5
by: pandapower | last post by:
Hi, I know about the equivalence of pointer and arrays.But my doubt comes when its for multidimentional arrays.I have read the C faq but still have some doubts. Suppose I have a declaration as...
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.