From: GT on
hi,
Although the definition of pointers is simple and apparently directly
inherited from assembly language, I can understand the point of using this
notion in assembler since there are no variables definitions, hence the need
to put values in memory address and be able to retrieve this value.
Having (I think) understood this concept as a teenager, I managed to write
Z80 code. So I think I got the point.
Now, since high level languages, such as C++ have the facility to define
variables : a var is defined, the compiler assigns it an address in memory,
and whatever value you give to this var is actually put at that memory
address.
So when you play with this var, you actually play with a value stored at a
memory location.
As far as I understand, this is exactly what the (basic) purpose of a
pointer is.
So why two different ways to do the same thing? (using var and using
pointer)

Unless this is just one of the things a pointer does ?

The books I have seem to consider this such a trivial matter that they do
not explain this. They just state it.
So I feel I am missing something either very fundamental, or looking for a
deeper meaning that's just not there.
But then why do C/C++ programmers use pointers? I can write a program in
C/C++ without a single one.
(why do languages like fortran not use them? what do they lack by not having
pointers?)
Yep, I do feel very stupid!

Thanks for any replies, and please, don't drown your answer with code
(unless necessary). I am trying to grasp a concept and its purpose here.

Thanks.
G



From: osmium on
"GT" wrote:

> Although the definition of pointers is simple and apparently directly
> inherited from assembly language, I can understand the point of using this
> notion in assembler since there are no variables definitions, hence the
> need to put values in memory address and be able to retrieve this value.
> Having (I think) understood this concept as a teenager, I managed to write
> Z80 code. So I think I got the point.
> Now, since high level languages, such as C++ have the facility to define
> variables : a var is defined, the compiler assigns it an address in
> memory, and whatever value you give to this var is actually put at that
> memory address.
> So when you play with this var, you actually play with a value stored at a
> memory location.
> As far as I understand, this is exactly what the (basic) purpose of a
> pointer is.
> So why two different ways to do the same thing? (using var and using
> pointer)

You probably have had only a very minimal exposure to data structures.
Consider how you would implement code for a linked list or a tree without
pointers.

http://en.wikipedia.org/wiki/Linked_list
http://en.wikipedia.org/wiki/Binary_tree


From: Richard Heathfield on
GT said:

<snip>

> But then why do C/C++ programmers use pointers? I can write a program in
> C/C++ without a single one.

But you can write more powerful C programs with pointers than you can write
without them.

For example, imagine you have 131072 512-byte records. You have to produce
ten reports, each of which requires a different sort order on the same
information. Let's say that sorting requires (n log n) / 2 swaps. That's
over a million swaps per sort. If you sort the actual arrays, you have to
shovel something like a Gigabyte of data around. But if you set up an array
of pointers to the data, then you need only move around 8 Megabytes around,
which is a huge economy.

Or let's say you want to build a dynamic data structure such as an object
tree. Perhaps you would find this easy to do without pointers. Me? I
wouldn't know where to start. But with pointers, it's a cinch.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
From: Jerry Coffin on
In article <44cba4c7$0$844$ba4acef3(a)news.orange.fr>,
igthibau(a)wanadoo.fr says...

[ ... ]

> Unless this is just one of the things a pointer does ?

A pointer only does one thing: hold the address of something (well,
technically, it can also be a null pointer, which means it doesn't
hold the address of anything, or it can be uninitialized, in which
case you don't know what it holds).

There are, however, a number of different ways of USING pointers,
most of which you haven't really touched upon.

First of all, pointers are often used to create data structures such
as linked lists and binary trees. In a linked list, you have a number
of nodes. Each node contains 1) some data, and 2) a pointer to the
next node in the list. In a typical case, the nodes are dynamically
allocated. You can (for example) create a new node, and insert it
into the middle of the list by manipulating the pointers. A typical
definition might look like:

struct node {
void *data;
struct node *next;
};

node *list = NULL;

Inserting a new node at the beginning of the list looks something
like this:

node *temp; // the node we're going to add

temp->next = list;
list = temp;

More importantly, given a pointer X to a node, we can insert a new
node before X in essentially the same way -- thus, we can insert a
new item in the middle of the list without moving around all the
existing nodes.

I won't go into binary (and n-way) trees, except to note that many of
the basic principles are similar -- by manipulating pointers, we can
change the apparent order of the data without moving all the data
itself around.

A rather different use is as parameters to functions. Here we're
often dealing only with a single variable at any given time, but we
can specify _which_ variable the function is going to deal with. An
obvious example is scanf:

scanf("%d", &my_int);

scanf (in this case) is going to read a number and put its value into
my_int. If we just passed the _value_ of my_int, it wouldn't be able
to change the original variable. By passing the address of the
variable, scanf can modify the variable's value. The next time we
call scanf, we might pass it the address of a different variable, so
scanf can change it instead. By receiving a pointer, scanf can now
change essentially any variable we designate.

As for FORTRAN (for one comparison): since FORTRAN doesn't have
pointers (or didn't classically, anyway) it passes all parameters by
reference, which means it's basically using a pointer internally, but
hiding it from you. The other purposes didn't used to be possible in
FORTRAN, except rather indirectly -- e.g. we could create a linked
list, but instead of dynamically allocating memory and storing its
address, we had to create an array of objects, and use an index into
the array as if it was an address -- and to do that, we had to write
our own dynamic allocation routines to keep track of which objects in
the array were in use, etc.

If memory serves, FORTRAN 90 added dynamic allocation, so some of
that has probably changed though -- in fact, I'm not at all sure it
may not now have pointers, or some reasonably close analog thereof.

--
Later,
Jerry.

The universe is a figment of its own imagination.
From: GT on
Thank you for replying,
Well, I have done some theoretical programming at university,which included
such concepts, but at that time c/c++ were not in use (89-92) and pascal did
not prevent such constructs, although, to the best of my knowledge
it does not include pointers (if my memory serves me right). I also have
used several languages for many years (basic, VB, Pascal, Assembler,
fortran, dcl) and none seemed to need pointers. C uses them but you can go
around that.
Establishing linked lists it simply a matter of establishing an abstract
connection between elements contained in a
vector (same or different vector). this can be achieved without pointers. in
fact I think relational databases do this.

Right, my university computer courses are a little far away (I am a
mathematician) and I would not presume to contradict your point of view or
be rude, but I don't really see the point between my question and linked
lists.

G

a
"osmium" <r124c4u102(a)comcast.net> a crit dans le message de news:
4j1ovbF59bt7U1(a)individual.net...
> "GT" wrote:
>
>> Although the definition of pointers is simple and apparently directly
>> inherited from assembly language, I can understand the point of using
>> this notion in assembler since there are no variables definitions, hence
>> the need to put values in memory address and be able to retrieve this
>> value.
>> Having (I think) understood this concept as a teenager, I managed to
>> write Z80 code. So I think I got the point.
>> Now, since high level languages, such as C++ have the facility to define
>> variables : a var is defined, the compiler assigns it an address in
>> memory, and whatever value you give to this var is actually put at that
>> memory address.
>> So when you play with this var, you actually play with a value stored at
>> a memory location.
>> As far as I understand, this is exactly what the (basic) purpose of a
>> pointer is.
>> So why two different ways to do the same thing? (using var and using
>> pointer)
>
> You probably have had only a very minimal exposure to data structures.
> Consider how you would implement code for a linked list or a tree without
> pointers.
>
> http://en.wikipedia.org/wiki/Linked_list
> http://en.wikipedia.org/wiki/Binary_tree
>
>
>