473,799 Members | 3,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What happens when you perform arithmetic on an Array Variable?

I guess my C++ is pretty darn rusty. I was just looking over sample
C++ code for practice... and I'm kind of confused about this code
fragment:
int sector2[512];
int i = 3;

memset(sector2, 128+i, 512);
memset(sector2+ 256, 255-i, 256);

I ran the code frag in visual studios, so I know what it does. But ...
I've don't remember ever seeing arithmetic done on an array variable
before. What exactly does sector2+256 mean? Does it just kinda of
change the starting memory address of the variable sector2?

Any help would be appreciated :-)

-Mercy

Dec 4 '06 #1
5 1812
IR
Mercy wrote:
int sector2[512];
int i = 3;

memset(sector2, 128+i, 512);
memset(sector2+ 256, 255-i, 256);

I ran the code frag in visual studios, so I know what it does.
But ... I've don't remember ever seeing arithmetic done on an
array variable before. What exactly does sector2+256 mean? Does
it just kinda of change the starting memory address of the
variable sector2?
It's rather pointer arithmetics than array arithmetics.

sector2 decays to int* (a pointer to the first element of the array),
then this pointer is added 256, which results in a pointer to the
257th element of the array.

--
IR
Dec 4 '06 #2
Mercy wrote:
I guess my C++ is pretty darn rusty. I was just looking over sample
C++ code for practice... and I'm kind of confused about this code
fragment:
int sector2[512];
int i = 3;

memset(sector2, 128+i, 512);
memset(sector2+ 256, 255-i, 256);

I ran the code frag in visual studios, so I know what it does. But
... I've don't remember ever seeing arithmetic done on an array
variable before. What exactly does sector2+256 mean? Does it just
kinda of change the starting memory address of the variable sector2?
'sector2' used in an expression *decays* into a pointer to the first
element. Addition applied to it is the same as taking the address
of the corresponding element. I.e., if you have

T array[<somedim>]

then

array + i

is equivalent to

& ( array[i] )

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '06 #3
Victor Bazarov wrote:
Mercy wrote:
>I guess my C++ is pretty darn rusty. I was just looking over sample
C++ code for practice... and I'm kind of confused about this code
fragment:
int sector2[512];
int i = 3;

memset(sector2 , 128+i, 512);
memset(sector2 +256, 255-i, 256);

I ran the code frag in visual studios, so I know what it does. But
... I've don't remember ever seeing arithmetic done on an array
variable before. What exactly does sector2+256 mean? Does it just
kinda of change the starting memory address of the variable sector2?

'sector2' used in an expression *decays* into a pointer to the first
element. Addition applied to it is the same as taking the address
of the corresponding element. I.e., if you have

T array[<somedim>]

then

array + i

is equivalent to

& ( array[i] )
(Well, that was probably wrong. I was trying to explain it in terms
which would be easier for you, but I assumed to much. In fact, the
expression 'array[i]' also has 'array' decaying into a pointer and
is interpreted by the compiler as '* (array + i)', i.e. dereference
the pointer expression obtained by adding the index to the pointer
to the first element of the array, the latter comes from conversion
of the array name into a pointer in any expression, IOW those two
are equivalent because for 'int*', &* is a no-op)

The only time where 'array' won't decay into a pointer is when a true
array is expected, like an argument of 'sizeof', 'typeid', '&', or
when initialising a reference to an array of the same dimension.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '06 #4

Victor Bazarov wrote:
Victor Bazarov wrote:
Mercy wrote:
I guess my C++ is pretty darn rusty. I was just looking over sample
C++ code for practice... and I'm kind of confused about this code
fragment:
int sector2[512];
int i = 3;

memset(sector2, 128+i, 512);
memset(sector2+ 256, 255-i, 256);

I ran the code frag in visual studios, so I know what it does. But
... I've don't remember ever seeing arithmetic done on an array
variable before. What exactly does sector2+256 mean? Does it just
kinda of change the starting memory address of the variable sector2?
'sector2' used in an expression *decays* into a pointer to the first
element. Addition applied to it is the same as taking the address
of the corresponding element. I.e., if you have

T array[<somedim>]

then

array + i

is equivalent to

& ( array[i] )

(Well, that was probably wrong. I was trying to explain it in terms
which would be easier for you, but I assumed to much. In fact, the
expression 'array[i]' also has 'array' decaying into a pointer and
is interpreted by the compiler as '* (array + i)', i.e. dereference
the pointer expression obtained by adding the index to the pointer
to the first element of the array, the latter comes from conversion
of the array name into a pointer in any expression, IOW those two
are equivalent because for 'int*', &* is a no-op)

The only time where 'array' won't decay into a pointer is when a true
array is expected, like an argument of 'sizeof', 'typeid', '&', or
when initialising a reference to an array of the same dimension.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks Victor :-)

Pointers and such get quite confusing don't they? I think I understand
what you were trying to say. I guess I just need a little bit more
practice. Lots of trial and error should make this all second nature
to me!

-Mercy

Dec 4 '06 #5
On 2006-12-04 23:54, Mercy wrote:
Victor Bazarov wrote:
>Victor Bazarov wrote:
Mercy wrote:
I guess my C++ is pretty darn rusty. I was just looking over sample
C++ code for practice... and I'm kind of confused about this code
fragment:
int sector2[512];
int i = 3;

memset(sector2 , 128+i, 512);
memset(sector2 +256, 255-i, 256);

I ran the code frag in visual studios, so I know what it does. But
... I've don't remember ever seeing arithmetic done on an array
variable before. What exactly does sector2+256 mean? Does it just
kinda of change the starting memory address of the variable sector2?

'sector2' used in an expression *decays* into a pointer to the first
element. Addition applied to it is the same as taking the address
of the corresponding element. I.e., if you have

T array[<somedim>]

then

array + i

is equivalent to

& ( array[i] )

(Well, that was probably wrong. I was trying to explain it in terms
which would be easier for you, but I assumed to much. In fact, the
expression 'array[i]' also has 'array' decaying into a pointer and
is interpreted by the compiler as '* (array + i)', i.e. dereference
the pointer expression obtained by adding the index to the pointer
to the first element of the array, the latter comes from conversion
of the array name into a pointer in any expression, IOW those two
are equivalent because for 'int*', &* is a no-op)

The only time where 'array' won't decay into a pointer is when a true
array is expected, like an argument of 'sizeof', 'typeid', '&', or
when initialising a reference to an array of the same dimension.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Thanks Victor :-)

Pointers and such get quite confusing don't they? I think I understand
what you were trying to say. I guess I just need a little bit more
practice. Lots of trial and error should make this all second nature
to me!
For most usages of C++ you don't have to use pointers at all, even less
so arrays. C++ has references and vector<>, which will often do the jub
just as well but are much safer and easier to use.

--
Erik Wikstrm
Dec 5 '06 #6

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

Similar topics

5
1561
by: greenflame | last post by:
How can I tell if a variable. I want ot tell whether two variables are: 1. Both numbers. Just a number. same as the number object in javascript. 2. Either one is a string containing a number. For exmaple a string like "354" or "295.395". 3. Either are strings containing a fraction. For example a string like "46/987". 4. Either is a "fraction". By "fraction" I mean that it is either an array of two numbers or two number strings like in...
43
6902
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app performance. In the hopes of improving performance, we have tried to find a way to avoid the...
58
30255
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly neighborhood C# programmer? The standard answer I get from computer sales people is: "It means that the CPU can process 64 bits of data at a time instead of 32." Ok... I guess I *kind* of understand what that means at an intuitive level, but what...
13
5061
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
1595
by: Hollywood | last post by:
Hello dear membres of the comp.unix.programmer. Please , I've got the following question to submit and I need your help. Here below you'll find a lexical and a syntaxic analysers. The lexical analyser (flex) find out variables, numbers and the '#' character. The syntaxic analyser (yacc) surveys "number diese number" patterns , or "variable diese number" patterns . Examples of correct syntax:
3
2231
by: farseer | last post by:
i am getting "error C2057: expected constant expression" with the following code: ifstream f( argv ); f.seekg( 0, ios::end ); const long fSize = f.tellg(); f.close(); char content;
669
26251
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
5
1331
by: dotnetchic | last post by:
I'm having some trouble interpreting some legacy code...here's a single line of the kind of pointer arithmetic that baffles me. I need help both interpreting and understanding the reasoning behind it. Can someone help me? *(ioStruct.FrntScrPtrn + uj *(*ioStruct.FrntScrPtrn_NumItemsY)+ui) = tempChar; can someone re-write this for me in terms I can understand? i.e., my first attempt was this, but it's incorrect:
11
3367
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9546
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
10490
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10260
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...
0
10030
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
9078
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 projectplanning, coding, testing, and deploymentwithout 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...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.