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 1812
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 Wikstrm 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. For exmaple a
string like "354" or "295.395".
3. Either are strings containing a fraction. For example a string
like "46/987".
4. Either is a "fraction". By "fraction" I mean that it is either
an array of two numbers or two number strings like in...
|
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 another story.) The data is
written once and then read many times. Each primitive read requires
unboxing. The data reads are critical to overall app performance. In the
hopes of improving performance, we have tried to find a way to avoid the...
|
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 neighborhood C# programmer? The standard answer I get from computer sales people is: "It means that the CPU can process 64 bits of data at a time instead of 32." Ok... I guess I *kind* of understand what that means at an intuitive level, but what...
|
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 analyser (flex) find out variables, numbers and the '#'
character.
The syntaxic analyser (yacc) surveys "number diese number" patterns ,
or
"variable diese number" patterns .
Examples of correct syntax:
| |
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 paper written on this
subject.
On the Expressive Power of Programming Languages, by Matthias
Felleisen, 1990.
http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
|
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 it. Can
someone help me?
*(ioStruct.FrntScrPtrn + uj *(*ioStruct.FrntScrPtrn_NumItemsY)+ui) =
tempChar;
can someone re-write this for me in terms I can understand? i.e., my
first attempt was this, but it's incorrect:
|
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 ways of passing an array. In the following code, fun1(int
a1) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |