Reference to Pointer

B

bansalvikrant

Hi,

C++ supports reference to pointer . I am wondering why C does not
support referece to pointers ?

Thanks
Vikrant
 
N

Nick Keighley

C++ supports reference to pointer . I am wondering why C does not
support referece to pointers ?

because C does not support references.

Then it becomes an "ask Dennis Richie question". He designed the
language and for some reason decided he didn't need references.
C is a very small and simple language that is close to the machine,
presumably references were just an abstraction that didn't seem
necessary (if, indeed, anything like references was even considered).

what do you want a reference to a pointer for? Will a pointer to
a pointer do?
 
B

bansalvikrant

because C does not support references.

Then it becomes an "ask Dennis Richie question". He designed the
language and for some reason decided he didn't need references.
C is a very small and simple language that is close to the machine,
presumably references were just an abstraction that didn't seem
necessary (if, indeed, anything like references was even considered).

what do you want a reference to a pointer for? Will a pointer to
a pointer do?

--
A designer knows he has achieved perfection
not when there is nothing left to add,
but when there is nothing left to take away.
       Antoine de Saint-Exupéry


yeah pointer to pointer could be replacement for reference to pointer
in C . I was thinking that in C++ , we have pointer to reference . I
was thinking what could be the reason why Denis has not put reference
support in C ..
 
T

Tim Harig

yeah pointer to pointer could be replacement for reference to pointer
in C . I was thinking that in C++ , we have pointer to reference . I
was thinking what could be the reason why Denis has not put reference
support in C ..

Pointers were in wide use when Dennis Richie created the C language. The
same was not true of references. I am not 100% sure in saying so; but, I
don't think any language was using references at the time. C++ was my
first exposure to references. Pointers predate references and references
where created as a direct answer to some of the problems that can be
created using pointers.

Whether Dennis Richie would have used references had they been more well
known is at this point academic.

That C has not adoped references is more clear. Many languages move
forward adding many new technologies to its core language. C++ itself has
done this and it is a very different language then it was when I first
started. The C community however, probably in a large account of us who
tend to dislike what C++ turned out to be, has chosen to stay closer to its
roots and recognize itself as a separate language then just as the
precursor to C++.
 
J

James Kuyper

in C . I was thinking that in C++ , we have pointer to reference . I
was thinking what could be the reason why Denis has not put reference
support in C ..

Probably for the same reason he didn't put in overloaded functions,
templates, default argument values, or any of the other innovations
added to C++. Which is to say, he quite possibly didn't even think about
such ideas; if he did, he probably rejected them as adding unnecessary
complication to the language.

Except for the capabilities that they allow in combination with operator
overloading, C++ references are little more than syntactic sugar for
const pointers (not "pointers to const"). Since C doesn't include
operator overloading, there's not much point in adding references.
 
B

Ben Bacarisse

Tim Harig said:
[...] I was thinking that in C++ , we have pointer to reference . I
was thinking what could be the reason why Denis has not put reference
support in C ..

Pointers were in wide use when Dennis Richie created the C language. The
same was not true of references. I am not 100% sure in saying so; but, I
don't think any language was using references at the time.

Algol-68 had a pretty well worked out notion of references by that
time. Ritchie notes that Algol-68 was a source of some of the
features of C and, in an aside, called pointers "references". To that
extent C does have references. What it does not have are any contexts
in which they are de-referenced automatically nor any in which a
reference is "picked up" automatically[1].

I like to think of C as having references (i.e. pointers) -- just not
auto-magic ones. I use the term auto-magic (in my mind) so as not to
confuse it with automatic which has its own meaning in C.

[1] Except of course for expressions of array or function type which
do get an automatic conversion to what C uses as a reference type.
 
B

Ben Bacarisse

James Kuyper said:
Probably for the same reason he didn't put in overloaded functions,
templates, default argument values, or any of the other innovations
added to C++. Which is to say, he quite possibly didn't even think
about such ideas; if he did, he probably rejected them as adding
unnecessary complication to the language.

Also, I think it is hard for today's students of computer science to
imagine how limited the original target for C was. Today, even a
compiler for a large language hardly impinges on a small laptop's
resources, but a PDP-11 with a maximum of 64k[1] would have struggled
with a modern C++ compiler, let along the poor 8k PDP-7 that was the
original development system. No doubt a compiler for a "big" language
could have been written for some of these later machines (Algol-68 did
come to the PDP-11 but some time after C) but it is unlikely that
doing so would have helped in the main purpose of C which was, as I
understand it, to re-write Unix in a high-level language.

[1] That is, of course, 64k per process -- the machines often had more
total RAM. Later models allowed 64k for data and a separate 64k for
code.
Except for the capabilities that they allow in combination with
operator overloading, C++ references are little more than syntactic
sugar for const pointers (not "pointers to const"). Since C doesn't
include operator overloading, there's not much point in adding
references.

Agreed.
 
S

Stefan Ram

Ben Bacarisse said:
features of C and, in an aside, called pointers "references". To that
extent C does have references. What it does not have are any contexts
in which they are de-referenced automatically nor any in which a
reference is "picked up" automatically[1].

I could quote several parts of ISO/IEC 9899:1999 (E) to contradict this.
But to save time, here is just one:

Function references are resolved (»automatically«) in translation
phase 8:

»All external object and function references are resolved.«

ISO/IEC 9899:1999 (E), 5.1.1.2, 8
 
T

Tim Harig

Tim Harig said:
[...] I was thinking that in C++ , we have pointer to reference . I
was thinking what could be the reason why Denis has not put reference
support in C ..
Pointers were in wide use when Dennis Richie created the C language. The
same was not true of references. I am not 100% sure in saying so; but, I
don't think any language was using references at the time.
Algol-68 had a pretty well worked out notion of references by that
time. Ritchie notes that Algol-68 was a source of some of the
features of C and, in an aside, called pointers "references". To that

I guess that it might be important here to define what the "reference"
concept. From this:

http://rosettacode.org/wiki/Basic_pointer_and_reference_operations#ALGOL_68

I gather that Algol-68 had something it called ref that I would describe
as being something between C pointers and C++ references. They could be
re-assigned but they where incapable of doing pointer arithmetic. They
were implicitly dereferenced.
 
T

Tim Harig

Tim Harig said:
[...] I was thinking that in C++ , we have pointer to reference . I
was thinking what could be the reason why Denis has not put reference
support in C ..
Pointers were in wide use when Dennis Richie created the C language. The
same was not true of references. I am not 100% sure in saying so; but, I
don't think any language was using references at the time.
Algol-68 had a pretty well worked out notion of references by that
time. Ritchie notes that Algol-68 was a source of some of the
features of C and, in an aside, called pointers "references". To that

I guess that it might be important here to define what the "reference"
concept. From this:

http://rosettacode.org/wiki/Basic_pointer_and_reference_operations#ALGOL_68

I gather that Algol-68 had something it called ref that I would describe
as being something between C pointers and C++ references. They could be
re-assigned but they where incapable of doing pointer arithmetic. They
were implicitly dereferenced.


Nevermind, I didn't notice the difference between = and :=.
 
P

Peter Nilsson

James Kuyper said:
Probably for the same reason he didn't put in overloaded
functions, templates, default argument values, or any of
the other innovations added to C++.

Pass by reference was a stable of FORTRAN (et al) long before
C and C++.
Which is to say, he quite possibly didn't even think about
such ideas; if he did, he probably rejected them as adding
unnecessary complication to the language.

Ironic then that many language designers in the era dismissed
pointers because they added unnecessary complexity to
programming.
 
J

James Kuyper

Peter said:
Pass by reference was a stable of FORTRAN (et al) long before
C and C++.


Ironic then that many language designers in the era dismissed
pointers because they added unnecessary complexity to
programming.

K&R decided that passing by value would be the norm in C, and that
pointers passed by value would be used to serve the same purpose as pass
by reference in those other languages. Having made those decisions,
adding a reference type on top of pass-by-value gets complicated; just
take a look at C++ for an example of the complications that can ensue.
 
M

mecej4

| > On Jul 2, 12:26 pm, Nick Keighley <[email protected]>
| > wrote:
| >> Then it becomes an "ask Dennis Richie question". He designed the
| >> language and for some reason decided he didn't need references.
| >> C is a very small and simple language that is close to the machine,
| >> presumably references were just an abstraction that didn't seem
| >> necessary (if, indeed, anything like references was even considered).
| > yeah pointer to pointer could be replacement for reference to pointer
| > in C . I was thinking that in C++ , we have pointer to reference . I
| > was thinking what could be the reason why Denis has not put reference
| > support in C ..
|
| Pointers were in wide use when Dennis Richie created the C language. The
| same was not true of references. I am not 100% sure in saying so; but, I
| don't think any language was using references at the time.

Whether references were called "references or not", there were languages
that provided something similar. PL/I, for example, provided "based
variables". You could set a pointer to point to a string, for example.
Thereafter, everytime the pointer value was changed, the based variable
would acquire a new value, without any explicit deferencing being done as in
C. Here is an example:

TestProg: PROC OPTIONS(MAIN);

DCL Str char(20) init ("abcdefghijklmnopqrst");

DCL C char based (p),p pointer;

DCL i FIXED BIN(31);

p=addr(Str);

DO i=1 to 20;

put edit(C)(A(1)); p+=1;

end;

put skip;

END TestProg;

| C++ was my
| first exposure to references. Pointers predate references and references
| where created as a direct answer to some of the problems that can be
| created using pointers.
|
| Whether Dennis Richie would have used references had they been more well
| known is at this point academic.
|
| That C has not adoped references is more clear. Many languages move
| forward adding many new technologies to its core language. C++ itself has
| done this and it is a very different language then it was when I first
| started. The C community however, probably in a large account of us who
| tend to dislike what C++ turned out to be, has chosen to stay closer to
its
| roots and recognize itself as a separate language then just as the
| precursor to C++.
 
D

David Thompson

Pass by reference was a stable of FORTRAN (et al) long before
C and C++.
(You mean staple.) It wasn't fully standard in FORTRAN but was very
common; it was standard in COBOL and PL/I, and explicit in Pascal.
As noted elsethread a68 had generalized to both parameters and locals
(but reseatable, unlike C++); and PL/I had BASED(p) which functioned
the same way, as did 'Cray pointers' in many FORTRAN implementations
(but not the standard).
But a68 and PL/I were famed(?) as 'kitchen-sink' languages, containing
so much cruft they were hard to implement, especially as noted
elsethread on smaller (= affordable) minicomputers of the day.
 
D

David Thompson

OT Nit:
James Kuyper said:
Probably for the same reason he didn't put in overloaded functions,
templates, default argument values, or any of the other innovations
added to C++. Which is to say, he quite possibly didn't even think
about such ideas; if he did, he probably rejected them as adding
unnecessary complication to the language.

Also, I think it is hard for today's students of computer science to
imagine how limited the original target for C was. Today, even a
compiler for a large language hardly impinges on a small laptop's
resources, but a PDP-11 with a maximum of 64k[1] would have struggled
with a modern C++ compiler, let along the poor 8k PDP-7 that was the
original development system. <snip>

[1] That is, of course, 64k per process -- the machines often had more
total RAM. Later models allowed 64k for data and a separate 64k for
code.
Or less. Lower-end -11 models did not have memory mapping so you had
at most 56KB, including both OS or equivalent such as fixed drivers
plus user; and in its early days (when C was created) semiconductor
memory was quite expensive, as was core, and machines were commonly
furnished with only 32KB, 24KB, even 16KB. (Which 'partial' filling
UNIBUS and later Q-BUS could handle correctly.) Although you would not
by choice use those machines for program development.

And even with mapping, physical memory on 18-bit busses (until IIRC
about '76 or '77) was maximum 248K, not much more than the first Apple
Mac and IBM PC.

FWIW the -7 could directly address 8K *words* of 18 bits. And it was
the original machine for Unix, but using B; C started on the -11.
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top