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 5 1789
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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...
|
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...
|
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
|
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...
|
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;
|
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...
|
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...
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |