Connecting Tech Pros Worldwide Forums | Help | Site Map

What happens when you perform arithmetic on an Array Variable?

Mercy
Guest
 
Posts: n/a
#1: Dec 4 '06
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


IR
Guest
 
Posts: n/a
#2: Dec 4 '06

re: What happens when you perform arithmetic on an Array Variable?


Mercy wrote:
Quote:
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
Victor Bazarov
Guest
 
Posts: n/a
#3: Dec 4 '06

re: What happens when you perform arithmetic on an Array Variable?


Mercy wrote:
Quote:
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


Victor Bazarov
Guest
 
Posts: n/a
#4: Dec 4 '06

re: What happens when you perform arithmetic on an Array Variable?


Victor Bazarov wrote:
Quote:
Mercy wrote:
Quote:
>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


Mercy
Guest
 
Posts: n/a
#5: Dec 4 '06

re: What happens when you perform arithmetic on an Array Variable?



Victor Bazarov wrote:
Quote:
Victor Bazarov wrote:
Quote:
Mercy wrote:
Quote:
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

Erik Wikström
Guest
 
Posts: n/a
#6: Dec 5 '06

re: What happens when you perform arithmetic on an Array Variable?


On 2006-12-04 23:54, Mercy wrote:
Quote:
Victor Bazarov wrote:
Quote:
>Victor Bazarov wrote:
Quote:
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 Wikström
Closed Thread