Question on Pointer Indirections

R

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
 
J

Jack Klein

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.
 
T

those who know me have no need of my name

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.
 
E

E. Robert Tisdale

rs said:
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.
 
R

Richard Heathfield

rs said:
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.
 
J

Joe Wright

E. Robert Tisdale said:
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.
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.
 
P

Peter Shaggy Haywood

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 said:
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"?
 
D

Dave Thompson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top