array's access with address confusion

D

dam_fool_2003

Hai,
For a notes in web is saw the following lines:
Accessing an item at an arbitrary position:
If the items in the
sequence are numbered 0, 1, ... and we know the address in
memory of the first item, then the address of the ith
item is

(address of first item) + i * (size of an item)
So I tried the following to see the address of the first item (ie 2):

int main(void)
{
unsigned int y[5]={2,3,6,7,4},j;
printf("Adder =%p and the value =%d\n",&y[0] + 1 * sizeof (int),y[0]+1
* sizeof(int));
for(j=0;j<5;j++)
printf("%p %d\n",&y[j],y[j]);
return 0;

}

OUTPUT:

Adder =8fb28 and the value =6
8fb18 2
8fb1c 3
8fb20 6
8fb24 7
8fb28 4
In the above I was excepting
"Adder = 8fb18 and the value = 2"
But it gave me a different answer. Why?
 
R

Rob van der Leek

Hai,
For a notes in web is saw the following lines:
Accessing an item at an arbitrary position:
If the items in the
sequence are numbered 0, 1, ... and we know the address in
memory of the first item, then the address of the ith
item is

(address of first item) + i * (size of an item)
So I tried the following to see the address of the first item (ie 2):

Note: you should include "stdio.h" since you are using printf(...)
int main(void)
{
unsigned int y[5]={2,3,6,7,4},j;
printf("Adder =%p and the value =%d\n",&y[0] + 1 * sizeof (int),y[0]+1
* sizeof(int));
for(j=0;j<5;j++)
printf("%p %d\n",&y[j],y[j]);
return 0;

}

OUTPUT:

Adder =8fb28 and the value =6
8fb18 2
8fb1c 3
8fb20 6
8fb24 7
8fb28 4
In the above I was excepting
"Adder = 8fb18 and the value = 2"
But it gave me a different answer. Why?

If the sequence items are numbered 0, 1, ..., N and you want the first
item, that item has index 0 (and not 1). Following this rule, the second
item has index 1. This explains why &y[0] + 1 * sizeof(int) gives you
the second item of array y.

Regards, Rob
 
M

Mark McIntyre

On 29 Sep 2003 06:02:22 -0700, in comp.lang.c ,
If the items in the
sequence are numbered 0, 1, ... and we know the address in
memory of the first item, then the address of the ith
item is
(address of first item) + i * (size of an item)

this is correct in terms of bytes, but not in terms of how C is
translated. As far as C is concerned
x[j] == *x + j
irrespective of the size of each item.
unsigned int y[5]={2,3,6,7,4},j;
printf("Adder =%p and the value =%d\n",&y[0] + 1 * sizeof (int),y[0]+1
Adder =8fb28 and the value =6

looks like sizeof(int) is 4 on your system
&y[0] == 8fb18
sizeof(int) = 4
&y[0] + 4 = &y[4] = 8fb28;
 
L

Lew Pitcher

If the items in the
sequence are numbered 0, 1, ... and we know the address in
memory of the first item, then the address of the ith
item is
(address of first item) + i * (size of an item)

this is correct in terms of bytes, but not in terms of how C is
translated. As far as C is concerned
x[j] == *x + j

ITYM x[j] == * (x + j)

Remember, precedence and order of operation <grin>

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top