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

how is the array offset determined?

Hi,

When you define an array base with a define statement, like

#define ARRAY_BASE 0x1000;

and access it, such as

ARRAY_BASE[x]

how does the compiler distinguish the offset? It is one-word wide by
default perhaps? (i.e. usually 32 bits)

Bahadir

Nov 14 '05 #1
9 2109
ba************@gmail.com wrote:
When you define an array base with a define statement, like

#define ARRAY_BASE 0x1000;

and access it, such as

ARRAY_BASE[x]


You don't; that is a syntax error. One of the operands to [] must be a
pointer, which isn't the case here unless x is one.

Richard
Nov 14 '05 #2
In article <11*********************@z14g2000cwz.googlegroups. com>,
<ba************@gmail.com> wrote:
#define ARRAY_BASE 0x1000;
Presumably you didn't mean the semicolon there.
ARRAY_BASE[x]
I'm assuming you intend x to be an integer.
how does the compiler distinguish the offset?


It doesn't. It prints out an error message, such as "subscripted
value is neither array nor pointer".

-- Richard
Nov 14 '05 #3
ba************@gmail.com wrote on 18/03/05 :
#define ARRAY_BASE 0x1000;

and access it, such as

ARRAY_BASE[x]


This expands to

0x1000;[x]

obvioulsy, it's not C...

Note that

#define ARRAY_BASE 0x1000

ARRAY_BASE[x];

is not C either because an integer and a pointer are different animals.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

Nov 14 '05 #4

Richard Tobin wrote:
In article <11*********************@z14g2000cwz.googlegroups. com>,
<ba************@gmail.com> wrote:
#define ARRAY_BASE 0x1000;


Presumably you didn't mean the semicolon there.
ARRAY_BASE[x]


I'm assuming you intend x to be an integer.
how does the compiler distinguish the offset?


It doesn't. It prints out an error message, such as "subscripted
value is neither array nor pointer".

-- Richard

Perhaps the former was my lamest post in last 5 years. Sorry for that.
The place I saw it was actually with an int * cast, and obviously that
cast determines the offset. I just got confused. Sometimes your mind
just stops you know.

Bahadir

Nov 14 '05 #5


Emmanuel Delahaye wrote:
ba************@gmail.com wrote on 18/03/05 :
#define ARRAY_BASE 0x1000;

and access it, such as

ARRAY_BASE[x]

This expands to

0x1000;[x]

obvioulsy, it's not C...

Note that

#define ARRAY_BASE 0x1000

ARRAY_BASE[x];

is not C either because an integer and a pointer are different animals.

But this does -

#define ARRAY_BASE 1000

and access it, such as

ARRAY_BASE[x] = <value>;

perhaps a typo in O.P code !

- Ravi

Nov 14 '05 #6
Ravi Uday wrote:

Emmanuel Delahaye wrote:
ba************@gmail.com wrote on 18/03/05 :
#define ARRAY_BASE 0x1000;

and access it, such as

ARRAY_BASE[x]

This expands to

0x1000;[x]

obvioulsy, it's not C...

Note that

#define ARRAY_BASE 0x1000

ARRAY_BASE[x];

is not C either because an integer
and a pointer are different animals.

But this does -


Does what?
There's no previous "doesn't" in this thread for reference.
#define ARRAY_BASE 1000

and access it, such as

ARRAY_BASE[x] = <value>;

perhaps a typo in O.P code !


Is x supposed to be a pointer?

--
pete
Nov 14 '05 #7
Ravi Uday wrote:

Emmanuel Delahaye wrote:


<snip>
Note that

#define ARRAY_BASE 0x1000

ARRAY_BASE[x];

is not C either because an integer and a pointer are different animals. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

But this does -

#define ARRAY_BASE 1000

and access it, such as

ARRAY_BASE[x] = <value>;

perhaps a typo in O.P code !


No, that is invalid for the reason Emmanuel Delahaye stated above.
Integers and pointers are not the same thing. All you have done is
change the value of the integer from 1000 hex to 1000 decimal probably
making it even worse.

The "correct" code if your system has something at address 1000 hex that
you are both allowed to access as an array of some_type and want to so
access would be

#define ARRAY_BASE ((some_type *)0x1000)

ARRAY_BASE[x];

This invokes undefined behaviour, however things like this are not too
uncommon in embedded systems where you have a memory mapped hardware
device you want to access.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #8
Flash Gordon wrote:
...
But this does -

#define ARRAY_BASE 1000

and access it, such as

ARRAY_BASE[x] = <value>;

perhaps a typo in O.P code !


No, that is invalid for the reason Emmanuel Delahaye stated above.
Integers and pointers are not the same thing. All you have done is
change the value of the integer from 1000 hex to 1000 decimal probably
making it even worse.


The important thing is that ';' is removed. In this case

ARRAY_BASE[x] = <value>;

has a chance to become a valid statement. Nothing says that 'x' must be
an integer. 'x' could be a pointer or an array, in which case the above
statement is perfectly valid.

#define ARRAY_BASE 1000

int x[2000];

ARRAY_BASE[x] = 5;

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #9
ba************@gmail.com writes:
[...]
Perhaps the former was my lamest post in last 5 years. Sorry for that.
The place I saw it was actually with an int * cast, and obviously that
cast determines the offset. I just got confused. Sometimes your mind
just stops you know.


It happens to all of us now and then.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

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

Similar topics

0
by: Free Rider | last post by:
Hi, i have a problem determining a matrix value by using a variable as index of the array.: I'm using php 4.3.11 + win xp SP2 + apache 1.3.33 I wrote a class in which there is an array defined as...
5
by: Richard Delorme | last post by:
The n869 draft says: J.2 Undefined behavior The behavior is undefined in the following circumstances: -- An array subscript is out of range, even if an object is ...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
3
by: Eric Lilja | last post by:
Assignment: Create a 3x4 2-dimensional array of integers. Populate each element using some non-random scheme so that no two elemens contain the same value. Then create two functions for printing...
9
by: removeps-generic | last post by:
I have struct X { double array; }; I want to form a pointer to the 5th element of X::array. The type of the pointer should be "double X::*" or "double* X::*" or something along those...
1
by: Manish | last post by:
The code is as ... $folderlistfile = $path."xmlfile.xml"; /* <list> <details id="2"> <name>Books</name>
9
by: herobeat | last post by:
Hi all, I'm having a hell of a time with declaring a struct to hold some binary data I'm trying to read from some files on disk. What I would like to do is something like this: public struct...
4
by: somenath | last post by:
I have a question regarding the memory allocation of array. For example int array; Now according to my understanding 10 subsequent memory location will be allocated of size sizeof(int) *10...
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
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.