By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

Question on Pointer Indirections

P: n/a
rs
Just out of idle curiosity:

1)Regarding pointers; I don't have the standard/grammar with me, but T* for
type T mean pointer-to-T. Does that mean that with a declaration of type
T** is actually a pointer to a type pointer, T*** is a pointer to type
pointer to type pointer and so on?

2) Is there any history behind the use of the "*" operator in providing
dereferencing capabilities or is it arbitrary? How did B handle pointers?
(if it had any). I'm specifically interested in why K&R didn't just
introduce a set of keywords for memory refrencing/derefrencing

3) [OT - But I would appreciate an ans if you have one] C++ has the
reference operator, which, in my limited experience is just a convenient way
of doing pointers; is this so? Is there any major difference in the
semantics or am I right?

4)Is there any practical use for pointers of more than 2 levels of
indireciton? i can't think of any. If this is the case would anything have
been gained (I guess apart from slight grammar simplification and compiler
implementation simplicity) from introducing T** and leaving it at that?

Thanks
Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Thu, 19 Feb 2004 03:28:59 -0000, "rs" <no************@talk21.com>
wrote in comp.lang.c:
Just out of idle curiosity:

1)Regarding pointers; I don't have the standard/grammar with me, but T* for
type T mean pointer-to-T. Does that mean that with a declaration of type
T** is actually a pointer to a type pointer, T*** is a pointer to type
pointer to type pointer and so on?
Yes.
2) Is there any history behind the use of the "*" operator in providing
dereferencing capabilities or is it arbitrary? How did B handle pointers?
(if it had any). I'm specifically interested in why K&R didn't just
introduce a set of keywords for memory refrencing/derefrencing
Pass.
3) [OT - But I would appreciate an ans if you have one] C++ has the
reference operator, which, in my limited experience is just a convenient way
of doing pointers; is this so? Is there any major difference in the
semantics or am I right?
comp.lang.c++ is down the hall to the right. The differences between
pointers and references are topical there.
4)Is there any practical use for pointers of more than 2 levels of
indireciton? i can't think of any. If this is the case would anything have
been gained (I guess apart from slight grammar simplification and compiler
implementation simplicity) from introducing T** and leaving it at that?
Yes, sometimes.
Thanks


--
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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2

P: n/a
in comp.lang.c i read:
1)Regarding pointers; I don't have the standard/grammar with me, but T* for
type T mean pointer-to-T. Does that mean that with a declaration of type
T** is actually a pointer to a type pointer, T*** is a pointer to type
pointer to type pointer and so on?
yes.
2) Is there any history behind the use of the "*" operator in providing
dereferencing capabilities or is it arbitrary? How did B handle pointers?
(if it had any). I'm specifically interested in why K&R didn't just
introduce a set of keywords for memory refrencing/derefrencing
i have dim memories of this, iirc it was because it was terse and apropos
(usage follows definition).
3) [OT - But I would appreciate an ans if you have one] C++ has ...
sorry, that would just encourage other off-topicness. the people in
comp.lang.c++ can respond.
4)Is there any practical use for pointers of more than 2 levels of
indireciton? i can't think of any.


if you have a T** and you want to pass the object (not any of the referents
nor a copy) to a function (so the function can change it) then you need a
T***, and if that function ...

also, in c multidimensional arrays are actually arrays of arrays.

--
a signature
Nov 14 '05 #3

P: n/a
rs wrote:
1)Regarding pointers; I don't have the standard/grammar with me,
but T* for type T mean pointer-to-T.
Does that mean that with a declaration of type T**
is actually a pointer to a type pointer,
T*** is a pointer to type pointer to type pointer and so on?
Yes.
2) Is there any history behind the use of the "*" operator
in providing dereferencing capabilities or is it arbitrary?
It is arbitrary but I'm sure that K&R carefully considered the choices.
I'm specifically interested in why K&R didn't just introduce
a set of keywords for memory referencing/dereferencing.
Like the sizeof operator?
I think that their objective was simply to be concise.
Teletype was still popular when K&R were designing C.
They were probably just trying to minimize the number of keystrokes
that programmers would be obliged to type so they exploited
the punctuation available on an ASCII keyboard.
3) [OT - But I would appreciate an answer if you have one]
C++ has the reference operator which, in my limited experience,
is just a convenient way of doing pointers; Is this so?
Is there any major difference in the semantics or am I right?
A pointer is an object which contains the address of another object.
A reference is just another name for an object.
In the C computer programming language,

const char* p = "qwerty";

p is a *pointer* to a character with [constant] value 'q' and
*p is a *reference* to that character.
4)Is there any practical use
for pointers of more than 2 levels of indirection?
Rarely.
If this is the case,
would anything have been gained (I guess apart from
slight grammar simplification and compiler implementation simplicity)
from introducing T** and leaving it at that?


No.
Limiting the levels of indirection would probably just make the grammar
less comprehensible and the compiler more complicated.

Nov 14 '05 #4

P: n/a
rs wrote:
4)Is there any practical use for pointers of more than 2 levels of
indireciton?
Yes.
i can't think of any.
Doesn't mean there aren't any.
If this is the case would anything
have been gained (I guess apart from slight grammar simplification and
compiler implementation simplicity) from introducing T** and leaving it at
that?


Well, let's just see if T** is adequate, first. Let's resize a dynamically
allocated array of chars:

#include <stdlib.h>
int resize_string(char **s, size_t *oldlen, size_t newlen)
{
int rc = 1; /* assume failure */
char *t = realloc(*s, newlen);
if(t != NULL)
{
*s = t;
*oldlen = newlen;
rc = 0; /* it worked */
}
return rc;
}

So okay, T ** is adequate for resizing a string.

Now I want to resize a dynamically-allocated array of dynamically-allocated
strings:

int resize_string_array(char *** Oops, not allowed three stars in this
version of C.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #5

P: n/a
E. Robert Tisdale wrote:

rs wrote:
[ snip ]
2) Is there any history behind the use of the "*" operator
in providing dereferencing capabilities or is it arbitrary?


It is arbitrary but I'm sure that K&R carefully considered the choices.

K&R didn't write C. R did.
I'm specifically interested in why K&R didn't just introduce
a set of keywords for memory referencing/dereferencing.


Like the sizeof operator?
I think that their objective was simply to be concise.
Teletype was still popular when K&R were designing C.


Again, Dennis Ritchie designed C. Brian Kernighan was the author of the
book, not the co-designer of the language.
They were probably just trying to minimize the number of keystrokes
that programmers would be obliged to type so they exploited
the punctuation available on an ASCII keyboard.
3) [OT - But I would appreciate an answer if you have one]
C++ has the reference operator which, in my limited experience,
is just a convenient way of doing pointers; Is this so?
Is there any major difference in the semantics or am I right?
A pointer is an object which contains the address of another object.


Yes.
A reference is just another name for an object.
Absolutely not!
In the C computer programming language,

const char* p = "qwerty";

p is a *pointer* to a character with [constant] value 'q' and
*p is a *reference* to that character.

No. C has no 'reference' at all except as you may use the English word
as 'refers to' or somesuch. In C++ and other languages, 'pass by
reference' infers semantics and operations that simply can't be done in
C.
4)Is there any practical use
for pointers of more than 2 levels of indirection?


Rarely.
If this is the case,
would anything have been gained (I guess apart from
slight grammar simplification and compiler implementation simplicity)
from introducing T** and leaving it at that?


No.
Limiting the levels of indirection would probably just make the grammar
less comprehensible and the compiler more complicated.


--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #6

P: n/a
Groovy hepcat E. Robert Tisdale was jivin' on Wed, 18 Feb 2004
21:07:58 -0800 in comp.lang.c.
Re: Question on Pointer Indirections's a cool scene! Dig it!
rs wrote:
3) [OT - But I would appreciate an answer if you have one]
C++ has the reference operator which, in my limited experience,
is just a convenient way of doing pointers; Is this so?
Is there any major difference in the semantics or am I right?


A pointer is an object which contains the address of another object.
A reference is just another name for an object.
In the C computer programming language,

const char* p = "qwerty";

p is a *pointer* to a character with [constant] value 'q' and
*p is a *reference* to that character.


Wrong! References in C: there ain't no sech animal. References are a
feature of C++.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #7

P: n/a
On Thu, 19 Feb 2004 03:28:59 -0000, "rs" <no************@talk21.com>
wrote:
Just out of idle curiosity:

1)Regarding pointers; I don't have the standard/grammar with me, but T* for
type T mean pointer-to-T. Does that mean that with a declaration of type
T** is actually a pointer to a type pointer, T*** is a pointer to type
pointer to type pointer and so on?
Yes, but not quite. As an *abstract declarator* (used in a cast, or
sizeof, or a nondefining prototype declaration that chooses to omit
parameter names) it *is* such a type, yes. In a declaration the
asterisks parse as part of the declarator, not of the type-specifiers,
so T** x where x is a simple identifier declares x a pointer to
pointer to T, but for example T ** x(whatever) declares as *function
returning* pointer to pointer to T; effectively the result is as if
T** were (part of) the type but the syntax doesn't treat it that way.
2) Is there any history behind the use of the "*" operator in providing
dereferencing capabilities or is it arbitrary? How did B handle pointers?
(if it had any). I'm specifically interested in why K&R didn't just
introduce a set of keywords for memory refrencing/derefrencing
According to http://cm.bell-labs.com/cm/cs/who/dmr/chist.html B did
use *; this, like other syntax changes between BCPL and B, seems to be
arbitrary; certainly dmr doesn't present any rationale.

B and BCPL did have pointers, but just (typeless) integers treated as
addresses of equally typeless cells; they don't have stride or target
information as in C, and most other HLLs that have pointers at all.
4)Is there any practical use for pointers of more than 2 levels of
indireciton? i can't think of any. If this is the case would anything have
been gained (I guess apart from slight grammar simplification and compiler
implementation simplicity) from introducing T** and leaving it at that?

In programming, the counting system is very often [zero,] one, many.
Once you go to the trouble of doing two it is often as easy to do
several, and as noted elsethread *simpler* formally because you just
recurse rather than enumerating cases. And for pointers it is
sometimes useful, though much less often than 1 and 2.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.