What is the Point of Pointer's

B

Bill Potter

I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!
 
G

Goran Larsson

Bill Potter said:
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!

The same reason why we use pointers in human languages, e.g.
"home" instead of "17859 Main Street"
"the car behind us" instead of "car with registration no KN5567YZ"
"you" instead of "Bill Potter"
 
M

Mark F. Haigh

Bill said:
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!

Let's say you're shopping for a car at a car dealership and the dealer
asks you which car you like, what do you do?

1. __Point__ at a couple of cars...

or

2. Go to each car, and physically move each of them directly in front
of the dealer?


Probably #1, if you're at all concerned about efficiency. You can refer
to more cars quicker by pointing at them than moving them each directly
in front of you, one by one.

Does that help?


Mark F. Haigh
(e-mail address removed)
 
A

Allan Bruce

Bill Potter said:
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!

for many reasons! 2 simple ones:

1) when passing a large structure to a function, we dont want to copy every
element and slow things down to a snail-pace. So pass a pointer instead,
which is very quick.

2) what if you want some information to be passed to a function and that
function modifies some data. For example a sort function. We pass a
pointer to an array to the function and let it do all the work.

Basically, pointers speed things up. They also allow dynamic memory
allocation using malloc() or equivalent. Things start to get more advanced
when you have pointers to pointers ;-)
Allan
 
O

osmium

Bill said:
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!

In C - as distinct from C++ - using a pointer is the *only* way a called
function can alter the contents of a variable in the calling function.
Parameters are passed in C in what is called "pass by value", that is, a
*copy* is passed to the callee.
 
J

Jan Engelhardt

I am a learning programmer in C and i want to know why some one would use
In C - as distinct from C++ - using a pointer is the *only* way a called
function can alter the contents of a variable in the calling function.
Parameters are passed in C in what is called "pass by value", that is, a
*copy* is passed to the callee.

In fact, C++'s references could be taken as a syntactic modification of The
Pointer, e.g.:

C> void callee(int *ptr) {
C> ++*ptr;
C> }

C++> void callee(int &ptr) {
C++> ++ptr;
C++> }


Jan Engelhardt
--
 
S

Stephen Sprunk

Goran Larsson said:
The same reason why we use pointers in human languages, e.g.
"home" instead of "17859 Main Street"
"the car behind us" instead of "car with registration no KN5567YZ"
"you" instead of "Bill Potter"

Actually "17859 Main St" is a pointer as well; the house itself is not.
Obviously every time you invite someone over, you'd rather give them a
pointer to your house instead of building a copy and handing it to them.

S
 
D

Default User

Bill said:
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!


Look into a typical linked list implementation.




Brian Rodenborn
 
G

Goran Larsson

Stephen Sprunk said:
Actually "17859 Main St" is a pointer as well; the house itself is not.

No. "17859 Main St" is the name of the house (variable).
Obviously every time you invite someone over, you'd rather give them a
pointer to your house instead of building a copy and handing it to them.

No. You give them the name of the house. The problem starts when you
move and they continue to visit the hose by its name. The pointer "home"
should have been used instead.
 
M

Michael Scarlett

Bill Potter said:
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!

Pointers are direct. That's the point!
 
D

Default User

Goran said:
No. "17859 Main St" is the name of the house (variable).

No, it's not, it's the address of the house. If you tore it down and
built an office building called "the doctor's cooperative", it would
still be at 17859 Main St.




Brian Rodenborn
 
M

Mabden

Default User said:
No, it's not, it's the address of the house. If you tore it down and
built an office building called "the doctor's cooperative", it would
still be at 17859 Main St.

So you're saying the address of the house is the address of the house...?
;-)

City planning guarantees that zero is never a valid address for a house...
 
K

kal

Bill Potter said:
I am a learning programmer in C and i want to know why
some one would use pointers instead of going direct!

According to ak C there are a variety of pointers. Here is
what they say why someone would like to have a G S Pointer.

<OT>
The Shorthair is friendly, intelligent, and willing to
please. The first impression is that of a keen enthusiasm
for work without indication of nervous or flightly character.
</OT>
 
K

Kenneth Brody

Bill said:
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!

Example:

I wish to write a routine to return information about every device
attached to the computer. The struct for the device information
takes 128 bytes. There can be up to 128 devices attached to the
computer, for a total of 16,384 bytes maximum.

Should I pass a 16,384 byte buffer to the routine, and have the
routine return that 16,384 bytes back to the caller? Or should
I simply pass a pointer to that buffer?
 
E

Emmanuel Delahaye

Bill Potter wrote on 04/08/04 :
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!

What do you mean 'direct' ? Any object has an address. It's good to
know its location if you want to access it. A pointer is just another
variable that holds the address of an object. It's useful in many
cases, for example if you want to 'pass' an array to a function.
Actually, there is no choice, only the address of the array is passed
into a pointer to the same type.


int a[123] -> int *param_a

char b[123] -> char *param_b

Why? Probably for an question of efficiency. It could have been
technically possible to pass a copy of the whole array, but it would
have been highly unefficient.

Note that pointers are not a C-feature. They belong to most
architectures where some registers are designed to hold an address, and
to reach the value via some indirection :

[ES:DI]
(A6)

etc.

The C 'sin' is to not hide this feature. Some other languages use
different syntaxes to act as if the pointers were not existing,
providing an extra abstraction layer. Is it good or not is debatable. I
like C and its pointers, and I see no reason to use another language
for now.
 
E

Emmanuel Delahaye

Mabden wrote on 05/08/04 :
So you're saying the address of the house is the address of the house...?

Don't mix pointer and address. A pointer is avariable. An address is
the value of a pointer. An address is a pointer constant.
 
E

Emmanuel Delahaye

Allan Bruce wrote on 04/08/04 :
1) when passing a large structure to a function, we dont want to copy every
element and slow things down to a snail-pace. So pass a pointer instead,
which is very quick.

This wording is confusing. Actually, you pass the address of the
structure via a pointer of the same type. You dont 'pass a pointer'.
 
S

Stefan Ram

Emmanuel Delahaye said:
A pointer is avariable.

ISO/IEC 9899:1999 (E) does not contain this assertion.

In fact, "&x", for an appropriate variable x, has "pointer
type". Which indicates that the value of "&x" is a pointer.
And ISO/IEC 9899:1999 (E) indeed mentions the compound
"pointer value", which is a pointer, but not necessarily an
object (what you call "variable").
 

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,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top