473,545 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer Guide

Please review this guide for clarity, accuracy, etc. so I can
hopefully compile a very good tutorial on how to use pointers in C,
including advanced topics, that is easy to follow and exposes the
details.

http://thelinuxlink.net/~fingolfin/pointer-guide/

My e-mail address is fake. To really contact me, send mail to:
fi************* **********@thel inuxlink.net, subtracting the blatant
anti-spam component.

--
I'm so happy
'Cause today I found my friends
They're in my head

-- Kurt Cobain
Nov 14 '05 #1
40 1808

On Fri, 18 Jun 2004, Foobarius Frobinium wrote:

Please review this guide for clarity, accuracy, etc. so I can
hopefully compile a very good tutorial on how to use pointers in C,
including advanced topics, that is easy to follow and exposes the
details.

http://thelinuxlink.net/~fingolfin/pointer-guide/

Your use of "lvalue" and "rvalue" is slightly non-standard, but
IMO consistent enough. The usual terminology would have it
that an /expression/ /is/ an lvalue or an rvalue, depending on
whether the expression denotes an object with an /address/.
You use "lvalue" to mean /address/, and "rvalue" to mean
/value/. Consistent enough, but irking to pedants.

The HTMLification of the first sample program, the one that
begins '/* vim:ts=4 */', is absolutely unreadable on a white
background (the default in most GUI web browsers). Do not
use light green or cyan text on a white background. Prefer
no coloration, especially since you can't seem to decide on
a consistent scheme: why is 'fprintf' black but 'stdout'
magenta, for example?

HTML could be used to great advantage in the body text of
the tutorial, however. Consider bolding new terms on their
first appearance, to make it easier to skim the large
paragraphs; and I recommend using <tt></tt> or <i></i>
around code snippets in the text such as 'foo' and 'frob'.

The line '/* vim:ts=4 */' is completely unnecessary to
your tutorial, BTW, and will only confuse non-Unix types.
Suggest removing the hard tabs from your HTML source code
as well, since I doubt many browsers can effectively deal
with them. Suggest the use of <pre> to format source code.

Suggest 'return 0;' from 'main'. This is not required
by C99, but is a good practice in both C90 and C99. And
you are supposedly writing to instill good practices in
newbies...

"Therefore, the return type of *bar is int..." Delete
the word "return" from this sentence; operators /yield/
values. Functions /return/. Unary * is not a function,
and thus /returns/ nothing.

"The relevance of them will be revealed yet..." Insert
"not" before "be," of course.

s/interchangably/interchangeably/

"And since char's are one byte in length (at least on
all systems I know)..." 'sizeof(char)' is 1 (byte) by
definition. Whether any particular "system" has a
machine-level concept of "byte" is not relevant to C
programming. But then neither is using actual numbers
to refer to "addresses in RAM," so you'll just have to
pick a wording that will satisfy the pedants. Suggest
simply removing the parenthetical comment.

"...in short it is a data type that is made to
reference other data through its lvalue." Confusing.
Firstly, 'short' is a data type, so you've just majorly
confused the skimmers, who aren't paying much attention
to your grammatical flourishes. Secondly, either your
grammar or your content is wrong in the second half of
that sentence; a pointer is an object which references
other data through its "rvalue" (its /value/), or
equivalently a pointer /contains/ another datum's "lvalue"
(its /address/).

"The data an array accesses cannot be changed.
Therefore they are not pointers." Grammar strikes again!
First, the data accessed by an array *can* be changed;
consider 'arr[0] = 42; arr[0] = 43;'. Secondly, the
data accessed by an array *can* be pointers; consider
'int *arp[10];'. You mean, "An array cannot be changed
to 'point' to other data; therefore arrays are not pointers."

s/syntax sugar/syntactic sugar/

Your HTMLification is screwed up majorly in "Basic
Pointer Arithmetic," where it actually "eats" the 'for'
keyword in your loop. Yet another reason to drop the
colorization.

"When you add a number to a number type, such as an int
or double, you get straight addition. But when you do it on
pointers, the number is automatically multiplied by the size
(in bytes) of the type to which the pointer points." This
is how it's usually explained, true. But your lead-in is
somewhat silly, since you never say what you mean by "straight
addition." Obviously the machine code for adding 1 to a
pointer is different from that for adding 1 to an 'int';
but the machine code for adding 1 to a 'double' is often
*far* more complicated. Yet you call the latter two "straight"
addition! IMO it would make more sense simply to tell the
reader that addition on pointers is /defined/ the way it
is, and let them determine for themselves whether it's
weird or not.

s/deference/dereference/

"...the arithmetic operators take precedence over pointer
[dereference] anyway..." This is just flat-out /wrong/.
Remove it, and the surrounding misdirection.

Finally, although the tutorial is obviously not yet
completed, I must urge you to add material on the []
indirection operator. The reader is currently left thinking
that '*(kochba+i)' is the only way to dereference a pointer.
[On a lighter note, I must say I've never seen 'kochba' used
as the third element of 'foo, bar,...'! Very... quirky.]
HTH,
-Arthur

Nov 14 '05 #2
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote in message news:<Pi******* *************** ************@un ix47.andrew.cmu .edu>...
On Fri, 18 Jun 2004, Foobarius Frobinium wrote:

Please review this guide for clarity, accuracy, etc. so I can
hopefully compile a very good tutorial on how to use pointers in C,
including advanced topics, that is easy to follow and exposes the
details.

http://thelinuxlink.net/~fingolfin/pointer-guide/

<snip rest of useful, verbose critique>
"...the arithmetic operators take precedence over pointer
[dereference] anyway..." This is just flat-out /wrong/.
Remove it, and the surrounding misdirection.
They don't?? I woulda sworn they did...damn, I'll have to check. Maybe
something is wrong with gcc, but maybe I was just being stupid

Finally, although the tutorial is obviously not yet
completed, I must urge you to add material on the []
indirection operator. The reader is currently left thinking
that '*(kochba+i)' is the only way to dereference a pointer.
[On a lighter note, I must say I've never seen 'kochba' used
as the third element of 'foo, bar,...'! Very... quirky.]
Yes, the very next thing I was planning was how the [] form works, and
tie that back into the equivalency of arrays and pointers. As for
'kochba', any Jewish hackers you might know might be able to tell you
of Simeon Bar Kochba, who invented subsurface operations as an element
of guerilla warfare (i.e., Whack-A-Guerilla), 1800 years before Dien
Bien Phu. It's true!

Quirkiness in amidst hopefully useful data is all part of my
adolescent inanities, and probably makes the reading a little easier.

HTH,
-Arthur

Nov 14 '05 #3
On 18 Jun 2004 12:07:20 -0700, fo******@yourem ailbox.com (Foobarius
Frobinium) wrote in comp.lang.c:
Please review this guide for clarity, accuracy, etc. so I can
hopefully compile a very good tutorial on how to use pointers in C,
including advanced topics, that is easy to follow and exposes the
details.

http://thelinuxlink.net/~fingolfin/pointer-guide/

My e-mail address is fake. To really contact me, send mail to:
fi************* **********@thel inuxlink.net, subtracting the blatant
anti-spam component.


I disagree with Arthur. You have this in the first paragraph:

"The lvalue is the variable's address in memory, and the rvalue is the
actual content of the variable."

You completely misunderstand the concepts of lvalues and rvalues in C.

In the snippet:

int x;
x = 3;

....the integer constant literal '3' is an rvalue that has nothing at
all to do with any variable.

The C standard defines the term 'lvalue' in part of a single sentence:

"An lvalue is an expression with an object type or an incomplete type
other than void" [6.3.2.1 P. 1]

Notice that the neither of the words 'address' or 'variable' appear in
this definition.

Also note that the C standard has dropped the word 'rvalue' from the
official terms of the language. It is mentioned only in a footnote:

"What is sometimes called ‘‘rvalue’’ is in this International Standard
described as the ‘‘value of an expression’’" [Footnote 53]

Just a little bit later, in your second sentence, you say:

"A pointer is a variable whose rvalue is the lvalue of another
variable."

This is just plain wrong. The value of a pointer object is an
'address'. A pointer object itself is an lvalue. The address that it
contains is not any sort of lvalue. Performing a dereference on a
pointer (other than pointer to void) creates an expression with an
object type, so *p is an lvalue expression.

Saying that the value of a pointer is an lvalue is like saying that
the value of an int is an lvalue.

Consider:

int x = 3;

....'x' is an lvalue, and has the value 3.

Now let's add:

int *ip = &x;

....'*ip' is an lvalue, which is an expression of object type (type
int) with the value of 'x', namely 3.

....'ip' is an lvalue, an expression of object type (type pointer to
int) with the value address of 'x'.

But the contents of 'ip', namely the address of x, is not an lvalue,
anymore than the contents of 'x', namely the integer value 3, is.

Have you actually obtained and read a copy of the ISO C Standard? The
PDF version is available for $18.00 US from ANSI. Until you do, I
seriously suggest you stop trying to write about things like lvalues
and rvalues, because you do not know their actual definitions and the
result is just plain incorrect.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
Jack,

I was very interested in reading this Pointer Guide, since I need a few.
Perhaps I should stick to K&R for now, but I'm tempted to give this a
read. Would it lead me too far astray?

I noticed on your web page http://jk-technology.com/c/standards.html#c99
that you mention a new book was in process by Dr. Bjarne Stroustrup. Do
you happen to know if it has been published yet, and if so by whom?

Mark Richards
Nov 14 '05 #5
Jack Klein <ja*******@spam cop.net> wrote:
In the snippet:

int x;
x = 3;

...the integer constant literal '3' is an rvalue that has nothing at
all to do with any variable.

The C standard defines the term 'lvalue' in part of a single sentence:

"An lvalue is an expression with an object type or an incomplete type
other than void" [6.3.2.1 P. 1]


'3' is an expression, it has type "int", and an int is an object,
therefore it is an expression with an object type. So your quote
seems to say that '3' is an lvalue. Where have I gone wrong..
Nov 14 '05 #6
Mark Richards wrote:
Jack,

I was very interested in reading this Pointer Guide, since I need a few.
Perhaps I should stick to K&R for now, but I'm tempted to give this a
read. Would it lead me too far astray?

I noticed on your web page http://jk-technology.com/c/standards.html#c99
that you mention a new book was in process by Dr. Bjarne Stroustrup. Do
you happen to know if it has been published yet, and if so by whom?

Mark Richards


The two places I learned pointers were:

1) K&R2

and

2) A Tutorial on Pointers and Arrays in C by Ted Jensen (which is like
reading K&R2 with expanded examples). The last url I have for this
document is: http://pweb.netcom.com/~tjensen/ptr/cpoint.htm

And thanks to all the folks here who helped me with restrict.

Cheers,

Brian
Nov 14 '05 #7
On 19 Jun 2004 20:50:49 -0700, in comp.lang.c , ol*****@inspire .net.nz (Old
Wolf) wrote:
Jack Klein <ja*******@spam cop.net> wrote:
In the snippet:

int x;
x = 3;

...the integer constant literal '3' is an rvalue that has nothing at
all to do with any variable.

The C standard defines the term 'lvalue' in part of a single sentence:

"An lvalue is an expression with an object type or an incomplete type
other than void" [6.3.2.1 P. 1]
'3' is an expression, it has type "int", and an int is an object,
therefore it is an expression with an object type. So your quote
seems to say that '3' is an lvalue.


if '3' were an lvalue, you could put it on the left of an expression
3 = x+2;
so you know your interpretation is wrong. <aside>this is a slight
exaggeration</aside>
Where have I gone wrong..


You have applied "all ducks have feet, therefore all feet belong to ducks"
logic.

Because an object can have type int, it does not follow that all things of
type int are objects. You can have constants of type int. A constant need
not be a region of data storage, it could be built into the machine code of
the program (and often is).
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #8
"Mark McIntyre" <ma**********@s pamcop.net> wrote in message
news:aq******** *************** *********@4ax.c om...
On 19 Jun 2004 20:50:49 -0700, in comp.lang.c , ol*****@inspire .net.nz (Old
Wolf) wrote:
Jack Klein <ja*******@spam cop.net> wrote:
In the snippet:

int x;
x = 3;

...the integer constant literal '3' is an rvalue that has nothing at
all to do with any variable.

The C standard defines the term 'lvalue' in part of a single sentence:

"An lvalue is an expression with an object type or an incomplete type
other than void" [6.3.2.1 P. 1]


'3' is an expression, it has type "int", and an int is an object,
therefore it is an expression with an object type. So your quote
seems to say that '3' is an lvalue.


if '3' were an lvalue, you could put it on the left of an expression
3 = x+2;
so you know your interpretation is wrong. <aside>this is a slight
exaggeration</aside>
Where have I gone wrong..


You have applied "all ducks have feet, therefore all feet belong to ducks"
logic.

Because an object can have type int, it does not follow that all things of
type int are objects. You can have constants of type int. A constant need
not be a region of data storage, it could be built into the machine code of
the program (and often is).
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>


I just use a very naive definition that seems to
work for me in most cases:

1. "lvalue"; the "l" (lower case ell) means "left". It's
something that can appear on the left side of an assignment.

2. "rvalue"; the "r" means "right". It's something that
can *only* appear on the right side of an assignment (an
expression that doesn't satisfy the requirements for lvalue).

The compiler will tell me when I get it wrong. That's
good enough for me.
--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!
Nov 14 '05 #9
"Old Wolf" <ol*****@inspir e.net.nz> wrote:
'3' is an expression, it has type "int", and an int is
an object, therefore it is an expression with an object
type. So your quote seems to say that '3' is an lvalue.
Where have I gone wrong..


You went wrong where you said an int is an object. Expressions of type int
do not necessarily refer to objects.

An integer constant between -32767 and 32767 is guaranteed to be of type
int. An integer constant from -2147483647 to -32768, or from 32768 to
2147483647 is guaranteed to be of type int if it fits in an int, otherwise
of type long.

An exception: Take as an example the expression -32768, which is actually
the integer constant 32768 which is then negated. Consider a 2's complement
system where int is 16 bits. Because the value of positive 32768 does not
fit into an int, it is given type long. Then, when negated, it still has
type long despite that the value now does fit into an int.

On Borland's Turbo C (a 16-bit MS-DOS compiler):
sizeof(-32768) == 4
sizeof(-32767-1) == 2

--
Simon.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1934
by: hari4063 | last post by:
Hay, I decalre pointer-to-member and it's sizeof iz 12 (in Borland 5) or 4 (in MinGW)? When I debugged code, only parameters pushed on stack is 'this' and call was indirect? What other 2 dwords are?
2
1196
by: Foobarius Frobinium | last post by:
Still not complete, as I need a cycle of feedback to be successful, but much improved. After corrections, I won't have much more to do. http://thelinuxlink.net/~fingolfin/pointer-guide/ Check out stuff on the main site as well if you wish...
2
1538
by: Foobarius Frobinium | last post by:
http://thelinuxlink.net/~fingolfin/pointer-guide/ I've made a lot of corrections, and put in more new stuff. I have malloc and pointers to struct's and maybe a few more things and I'm done. Examine it and tell me what you think.
48
2345
by: Foobarius Frobinium | last post by:
http://thelinuxlink.net/~fingolfin/pointer-guide Tell me what you think...
16
2768
by: Dave | last post by:
I'm having a problem trying to set up code in VB to handle the case where an argument in an API definition is defined as a pointer to a pointer. An excerpt from the API's definition is: void ** Object //Pointer to an interface pointer Object Pointer to an interface pointer variable that receives a pointer for
0
7490
marktang
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7425
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...
0
7682
Oralloy
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. ...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7780
tracyyun
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...
0
5069
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...
0
3479
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...
1
1911
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
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.