What happens when you perform arithmetic on an Array Variable?

M

Mercy

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
 
I

IR

Mercy said:
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.
 
V

Victor Bazarov

Mercy said:
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 )

V
 
V

Victor Bazarov

Victor said:
Mercy said:
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 )


(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' 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
 
M

Mercy

Victor said:
Victor said:
Mercy said:
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 )


(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' 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


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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Victor said:
Victor said:
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 )


(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' 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


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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top