Connecting Tech Pros Worldwide Forums | Help | Site Map

What is the Point of Pointer's

Bill Potter
Guest
 
Posts: n/a
#1: Nov 14 '05
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!





Goran Larsson
Guest
 
Posts: n/a
#2: Nov 14 '05

re: What is the Point of Pointer's


In article <yvOdnT_BcYSo9Y3cRVn-hQ@comcast.com>,
Bill Potter <loosejeans@comcast.net> wrote:
[color=blue]
> I am a learning programmer in C and i want to know why some one would use
> pointers instead of going direct![/color]

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"

--
Göran Larsson http://www.mitt-eget.com/
Mark F. Haigh
Guest
 
Posts: n/a
#3: Nov 14 '05

re: What is the Point of Pointer's


Bill Potter wrote:
[color=blue]
> I am a learning programmer in C and i want to know why some one would use
> pointers instead of going direct!
>[/color]

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
mfhaigh@sbcglobal.net
Allan Bruce
Guest
 
Posts: n/a
#4: Nov 14 '05

re: What is the Point of Pointer's



"Bill Potter" <loosejeans@comcast.net> wrote in message
news:yvOdnT_BcYSo9Y3cRVn-hQ@comcast.com...[color=blue]
> I am a learning programmer in C and i want to know why some one would use
> pointers instead of going direct!
>
>[/color]

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


osmium
Guest
 
Posts: n/a
#5: Nov 14 '05

re: What is the Point of Pointer's


Bill Potter writes:
[color=blue]
> I am a learning programmer in C and i want to know why some one would use
> pointers instead of going direct![/color]

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.


Jan Engelhardt
Guest
 
Posts: n/a
#6: Nov 14 '05

re: What is the Point of Pointer's


>> I am a learning programmer in C and i want to know why some one would use[color=blue][color=green]
>> pointers instead of going direct![/color]
>
>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.[/color]

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
--
Stephen Sprunk
Guest
 
Posts: n/a
#7: Nov 14 '05

re: What is the Point of Pointer's


"Goran Larsson" <hoh@invalid.invalid> wrote in message
news:I1wont.Cn5@approve.se...[color=blue]
> In article <yvOdnT_BcYSo9Y3cRVn-hQ@comcast.com>,
> Bill Potter <loosejeans@comcast.net> wrote:[color=green]
> > I am a learning programmer in C and i want to know why some one would[/color][/color]
use[color=blue][color=green]
> > pointers instead of going direct![/color]
>
> 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"[/color]

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

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov

Default User
Guest
 
Posts: n/a
#8: Nov 14 '05

re: What is the Point of Pointer's


Bill Potter wrote:[color=blue]
>
> I am a learning programmer in C and i want to know why some one would use
> pointers instead of going direct![/color]


Look into a typical linked list implementation.




Brian Rodenborn
Goran Larsson
Guest
 
Posts: n/a
#9: Nov 14 '05

re: What is the Point of Pointer's


In article <U27Qc.33288$vX4.19460@cyclops.nntpserver.com>,
Stephen Sprunk <stephen@sprunk.org> wrote:
[color=blue]
> Actually "17859 Main St" is a pointer as well; the house itself is not.[/color]

No. "17859 Main St" is the name of the house (variable).
[color=blue]
> 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.[/color]

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.

--
Göran Larsson http://www.mitt-eget.com/
Michael Scarlett
Guest
 
Posts: n/a
#10: Nov 14 '05

re: What is the Point of Pointer's


"Bill Potter" <loosejeans@comcast.net> wrote in message news:<yvOdnT_BcYSo9Y3cRVn-hQ@comcast.com>...[color=blue]
> I am a learning programmer in C and i want to know why some one would use
> pointers instead of going direct![/color]

Pointers are direct. That's the point!
Wayne Rasmussen
Guest
 
Posts: n/a
#11: Nov 14 '05

re: What is the Point of Pointer's




Bill Potter wrote:
[color=blue]
> I am a learning programmer in C and i want to know why some one would use
> pointers instead of going direct![/color]

Power!


Default User
Guest
 
Posts: n/a
#12: Nov 14 '05

re: What is the Point of Pointer's


Goran Larsson wrote:[color=blue]
>
> In article <U27Qc.33288$vX4.19460@cyclops.nntpserver.com>,
> Stephen Sprunk <stephen@sprunk.org> wrote:
>[color=green]
> > Actually "17859 Main St" is a pointer as well; the house itself is not.[/color]
>
> No. "17859 Main St" is the name of the house (variable).[/color]

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
Mabden
Guest
 
Posts: n/a
#13: Nov 14 '05

re: What is the Point of Pointer's


"Default User" <first.last@boeing.com.invalid> wrote in message
news:411165B6.EEDA96FC@boeing.com.invalid...[color=blue]
> Goran Larsson wrote:[color=green]
> >
> > In article <U27Qc.33288$vX4.19460@cyclops.nntpserver.com>,
> > Stephen Sprunk <stephen@sprunk.org> wrote:
> >[color=darkred]
> > > Actually "17859 Main St" is a pointer as well; the house itself is[/color][/color][/color]
not.[color=blue][color=green]
> >
> > No. "17859 Main St" is the name of the house (variable).[/color]
>
> 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.[/color]

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

--
Mabden


kal
Guest
 
Posts: n/a
#14: Nov 14 '05

re: What is the Point of Pointer's


"Bill Potter" <loosejeans@comcast.net> wrote in message news:<yvOdnT_BcYSo9Y3cRVn-hQ@comcast.com>...[color=blue]
> I am a learning programmer in C and i want to know why
> some one would use pointers instead of going direct![/color]

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>
red floyd
Guest
 
Posts: n/a
#15: Nov 14 '05

re: What is the Point of Pointer's



I think it's out of print now, but the best explanation of pointers and
the reasoning behind them I ever read was in Cooper&Clancy's "Oh
Pascal!" (http://www.amazon.com/exec/obidos/tg...-/0393960749/).

Yeah, it's a Pascal book, but it explained the how and why, etc...
Kenneth Brody
Guest
 
Posts: n/a
#16: Nov 14 '05

re: What is the Point of Pointer's


Bill Potter wrote:[color=blue]
>
> I am a learning programmer in C and i want to know why some one would use
> pointers instead of going direct![/color]

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?


--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+

Emmanuel Delahaye
Guest
 
Posts: n/a
#17: Nov 14 '05

re: What is the Point of Pointer's


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

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.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Emmanuel Delahaye
Guest
 
Posts: n/a
#18: Nov 14 '05

re: What is the Point of Pointer's


Mabden wrote on 05/08/04 :[color=blue][color=green][color=darkred]
>>> No. "17859 Main St" is the name of the house (variable).[/color]
>>
>> 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.[/color]
>
> So you're saying the address of the house is the address of the house...?[/color]

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

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Emmanuel Delahaye
Guest
 
Posts: n/a
#19: Nov 14 '05

re: What is the Point of Pointer's


Allan Bruce wrote on 04/08/04 :
[color=blue]
> 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.[/color]

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

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Foobarius Frobinium
Guest
 
Posts: n/a
#20: Nov 14 '05

re: What is the Point of Pointer's


"Bill Potter" <loosejeans@comcast.net> wrote in message news:<yvOdnT_BcYSo9Y3cRVn-hQ@comcast.com>...[color=blue]
> I am a learning programmer in C and i want to know why some one would use
> pointers instead of going direct![/color]

Well, plenty of reasons. Since, obviously, you don't want to make all
data global, you can use them to reference data defined in other
functions. Dynamic allocation in C is done with pointers, as well as
command line arguments (int argc, char **argv). Plus, by referencing
data instead of copying it, you don't waste space.
pete
Guest
 
Posts: n/a
#21: Nov 14 '05

re: What is the Point of Pointer's


Emmanuel Delahaye wrote:[color=blue]
>
> Mabden wrote on 05/08/04 :[color=green][color=darkred]
> >>> 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.[/color]
> >
> > So you're saying the address of the house is the address of the house...?[/color]
>
> Don't mix pointer and address. A pointer is avariable. An address is
> the value of a pointer.[/color]

The address of an object, has a pointer type and can be
used in a function call, where a pointer argument is required.
Closed Thread