how is the array offset determined?

B

bahadir.balban

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
 
R

Richard Bos

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
 
R

Richard Tobin

#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
 
B

bahadir.balban

Richard said:
#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
 
R

Ravi Uday

Emmanuel said:
#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
 
P

pete

Ravi said:
Emmanuel said:
#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?
 
F

Flash Gordon

Ravi said:
Emmanuel Delahaye wrote:
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.
 
A

Andrey Tarasevich

Flash said:
...
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;
 
K

Keith Thompson

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.
 

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,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top