Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What happens when you perform arithmetic on an Array Variable?

Reply
Thread Tools

What happens when you perform arithmetic on an Array Variable?

 
 
Mercy
Guest
Posts: n/a
 
      12-04-2006
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

 
Reply With Quote
 
 
 
 
IR
Guest
Posts: n/a
 
      12-04-2006
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-04-2006
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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-04-2006
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


 
Reply With Quote
 
Mercy
Guest
Posts: n/a
 
      12-04-2006

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

 
Reply With Quote
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      12-05-2006
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 Wikström
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::set::iterator cannot perform arithmetic? Christopher C++ 9 11-06-2012 11:03 PM
When you declare an array of chars and store a string in it, where isthe position of the null character \0? And what happens to the unused memorylocations? Gary C Programming 5 05-07-2008 03:11 AM
What happens when type conversion between signed and unsigned happens? NM C++ 6 09-20-2006 05:39 PM
TB 1.0.6 - How do you add files types and actions to the "Automatically perform" section? Z@no.spam Firefox 4 01-02-2006 04:38 PM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM



Advertisments