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 | | | | 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 | | | | 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 | | | | 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 | | | | 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 | | | | 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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|