473,326 Members | 2,114 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,326 software developers and data experts.

array

int a[10];
why does
printf("%p",a); and printf("%p",&a);
print the same result.

also printf("%p",&(a)); //this works fine
but printf("%p",.&(a+2));//results into error.
Dec 13 '09 #1

✓ answered by Banfa

You mean to say address of a[0] and a are stored at the same location.
Also what should &a+2 yield??
If you meant a[0] and a are stored at the same location then yes.
If you meant address of a[0] and address of a are the same then yes.
But if you meant (address of a[0]) and a are stored at the same location then no. (address of a[0]) has no location in memory since it is not an object.

&a+2

&a has type int(*)[10], a pointer to an array of 10 integers.

Assuming that integer has size 4 (which is very common now) then this is basic pointer arithmetic, the result is the original pointer plus 2 times the size of the object pointed to. Since the object pointed to is an array of 10 integers its size is 10 * sizeof(int) or 40 under our assumptions. so the expression

&a+2

is equivalent to

(char*)(&a) + (40 * 2)

in words

Base address of a + (size of array * increment)

The result is the address 80 bytes further along in memory.

7 1385
Banfa
9,065 Expert Mod 8TB
Because a and &a point at the same thing (an array) the difference is in the type.

a has the type int * the same as &a[0]

&a has the type int (*)[10]

But the location of both the array and the first integer in the array are the same so a and &a have the same value for any platform where the pointer to integer and pointer to structure are semantically the same.

&(a+2) doesn't work because the address (a+2) is not an object (not an l-value) so you can't get an address of it.
Dec 13 '09 #2
Banfa
You mean to say address of a[0] and a are stored at the same location.
Also what should &a+2 yield??
Dec 13 '09 #3
a is a pointer and a[0] is *(a+0). I think you mean to say *a and a[0] are stored in same location. I think yes.

&a+2 Since type of &a is (int*)[10]

Base address of a + size of array*size of pointer to integer.


Please correct me if i am wrong
Dec 13 '09 #4
Banfa
9,065 Expert Mod 8TB
You mean to say address of a[0] and a are stored at the same location.
Also what should &a+2 yield??
If you meant a[0] and a are stored at the same location then yes.
If you meant address of a[0] and address of a are the same then yes.
But if you meant (address of a[0]) and a are stored at the same location then no. (address of a[0]) has no location in memory since it is not an object.

&a+2

&a has type int(*)[10], a pointer to an array of 10 integers.

Assuming that integer has size 4 (which is very common now) then this is basic pointer arithmetic, the result is the original pointer plus 2 times the size of the object pointed to. Since the object pointed to is an array of 10 integers its size is 10 * sizeof(int) or 40 under our assumptions. so the expression

&a+2

is equivalent to

(char*)(&a) + (40 * 2)

in words

Base address of a + (size of array * increment)

The result is the address 80 bytes further along in memory.
Dec 13 '09 #5
Banfa
i read ur above post and it cleared my doubt but still there are some doubts somewhere in my mind.pls answer it.

Let us say i have int a[3]={1}
.Its first element i.e. a[0] will be stored at some location say 23FF60.If i write &a[0] i will get this value(23FF60) again if i write a i will get the same value.
My question is why are address of a[0] (i.e. &a[0]) and address of a(&a) are stored at the same location?
Dec 14 '09 #6
Banfa
9,065 Expert Mod 8TB
The address of a[0] and a are not stored anywhere, they are a property of a[0] and a. The address of a[0] and the address of a are the same because a[0] is the first element in a.

Think of a row of cars parked nose to tail. The start of the row of cars and the start of the first car in the row are at the same place. That place is not stored anywhere because there is no place for storing places on the street, there is only places for storing cars.
Dec 14 '09 #7
Car parking example was awesome.Really a gr8 explanation
A big thanx to you Banfa.You are gr8.
Dec 14 '09 #8

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); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
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>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.