473,387 Members | 1,859 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,387 software developers and data experts.

array's access with address confusion

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?
Nov 13 '05 #1
3 2645
In article <a3**************************@posting.google.com >,
<da***********@yahoo.com> wrote:
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
--
Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
Ricardishof 73-A | http://www.ricardis.tudelft.nl/~rob
2614 JE Delft, The Netherlands
+31 (0)6 155 244 60
Nov 13 '05 #2
On 29 Sep 2003 06:02:22 -0700, in comp.lang.c ,
da***********@yahoo.com wrote:
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;
8fb18 2
8fb1c 3
8fb20 6
8fb24 7
8fb28 4


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #3
On Mon, 29 Sep 2003 17:23:56 +0100, Mark McIntyre <ma**********@spamcop.net>
wrote:
On 29 Sep 2003 06:02:22 -0700, in comp.lang.c ,
da***********@yahoo.com wrote:
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')
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
16
by: ES Kim | last post by:
"A Book on C" explains a technique to use an arbitrary array index range. int* p = malloc(sizeof(int) * 10) - 1; This way, the allocated array can be accessed with index range 1 ~ 10, not 0 ~...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
15
by: Carramba | last post by:
hi! I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion, but how do I pass arrays to it if the imput is more then 1 sign? #include...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
18
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.