473,809 Members | 2,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2129
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************ *********@z14g2 000cwz.googlegr oups.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 "subscripte d
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************ *********@z14g2 000cwz.googlegr oups.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 "subscripte d
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_Keit h) 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
1195
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 follows. There is also a method to get the value of a given element of the array. class obj1 { .....
5
3440
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 apparently accessible with the given subscript (as in the lvalue expression a given the declaration
51
23751
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 10018-clc.c: In function `main': 10018-clc.c:22: warning: array subscript has type `char' I don't like warnings ... or casts.
3
10583
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 the 2D array. One should only work for set column size, accepting only the number of rows as an additional argument, one should accept both the number of rows and columns as arguments. So, I came up with: #include <stdio.h>
9
1820
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 lines.
1
13056
by: Manish | last post by:
The code is as ... $folderlistfile = $path."xmlfile.xml"; /* <list> <details id="2"> <name>Books</name>
9
7315
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 binHeader { public UInt32 Id; public UInt32 Offset;
4
2475
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 and this is done at compile time. Does it mean C compiler reserve some amount of memory for the array
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.