Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Student question about pointers.

0 views
Skip to first unread message

ywerther

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
I've been teaching myself c/c++ for some time now and am grasping it pretty
well except for one feature: pointers. I know what they are definitively and
how to use them but the one thing no one has been able to explain to me is
WHY? What is the advantage? What is the difference between using a pointer
to a variable and using the variable itself? I've been trying to find out
for some time (most of my classmates are almost as smart as they think they
are) but I've had no clear answer.
Thanks in advance
Matt K

Martin Ambuhl

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
ywerther wrote:
> What is the difference between using a pointer
> to a variable and using the variable itself?

Suppose you want to visit me. Consider two objects (assume the type
`dwelling' is defined):

dwelling my_house;
dwelling *a_piece_of_paper_with_my_address; /* the pointer */

a_piece_of_paper_with_my_address = &my_house;

Which do you feel more comfortable using if you would like to listen to
some music, drink some coffee, and warm yourself by the fire.

Which would you feel more comfortable carrying in your wallet?

Are they the same?

What's the difference?


--
Martin Ambuhl (mam...@earthlink.net)
Note: mam...@tiac.net will soon be inactive


Lawrence Kirby

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
In article <76cko0$1iq$1...@clarknet.clark.net>
ywer...@mnsinc.com "ywerther" writes:

>I've been teaching myself c/c++ for some time now and am grasping it pretty
>well except for one feature: pointers. I know what they are definitively and
>how to use them but the one thing no one has been able to explain to me is
>WHY? What is the advantage?

You can do many things with pointers that you couldn't do without in C.
Take a look at the standard library and specifically functions that
take pointer arguments or return pointer values. How would you implement
them without using pointers? (e.g. strcpy() fgets() qsort() malloc() ).

Pointers allow you to reference objects whose declarations you can't see and
allow you to select at runtime which object you want to act on (as a
specific example of this C uses pointer arithmetic to index arrays).
With pointers you can build separate objects into complex datastructures
like linked lists and trees. You can tell a function like strcpy where it is
supposed to be copying from and copying to. With malloc/calloc/realloc
a pointer can reference an object whose size is determined at runtime and
even resized at runtime.

>What is the difference between using a pointer
>to a variable and using the variable itself?

There is no difference whether you access an object through an identifier
in the source code or through pointer to that object, the access behaves
identically. That assumes however that you are able in the first place
to access the object using an identifier (i.e. the object even has
an associated identifier, the identifier is visible to the code in
question and it is the only object you want to access at that particular
point in the code).

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


Connie Fisher

unread,
Jan 1, 1999, 3:00:00 AM1/1/99
to
ywerther (ywer...@mnsinc.com) wrote:
: I've been teaching myself c/c++ for some time now and am grasping it pretty

: well except for one feature: pointers. I know what they are definitively and
: how to use them but the one thing no one has been able to explain to me is
: WHY? What is the advantage? What is the difference between using a pointer
: to a variable and using the variable itself? I've been trying to find out

: for some time (most of my classmates are almost as smart as they think they
: are) but I've had no clear answer.
: Thanks in advance
: Matt K


I'll aggree that pointers are one of the more difficult concepts to grasp,
as you must know something about the computer's heap. It's like this:
Pointers are just variable that have memory addresses as value. This is
useful when you need to do something with the variable being passed to
a function e.g.:

void increment( int *a )
{ *a++; }

I realize that a macro would be better, but this is an illistration.

Pointers can also be useful when you've got data trees or linked lists or
any time you have a structure in a structure.

- Hope this cleared things up
-- Meric
--- me...@babba.advancenet.net

Mark Waluk

unread,
Jan 1, 1999, 3:00:00 AM1/1/99
to
ywerther wrote:
>
> I've been teaching myself c/c++ for some time now and am grasping it pretty
> well except for one feature: pointers. I know what they are definitively and
> how to use them but the one thing no one has been able to explain to me is
> WHY? What is the advantage? What is the difference between using a pointer
> to a variable and using the variable itself? I've been trying to find out
> for some time (most of my classmates are almost as smart as they think they
> are) but I've had no clear answer.
> Thanks in advance
> Matt K

You can't pass structures, arrays, functions, or anything larger
than the standard data types. But you can pass pointers to them.
The reason for this is that when you call a function and pass
variables to the function the variables are pushed onto the
stack, and since the stack has limited space it can't accomadate
large amounts of data.
Pointers are also useful when you want to work with a memory
location that you can't define as a variable, video memory for
example.

Mark

Mark Waluk

unread,
Jan 1, 1999, 3:00:00 AM1/1/99
to
Michael Rubenstein wrote:

>
> On Fri, 01 Jan 1999 17:56:43 -0800, Mark Waluk
> <ma...@freenet.edmonton.ab.ca> wrote:
>
> >ywerther wrote:
> >>
> >> I've been teaching myself c/c++ for some time now and am grasping it pretty
> >> well except for one feature: pointers. I know what they are definitively and
> >> how to use them but the one thing no one has been able to explain to me is
> >> WHY? What is the advantage? What is the difference between using a pointer
> >> to a variable and using the variable itself? I've been trying to find out
> >> for some time (most of my classmates are almost as smart as they think they
> >> are) but I've had no clear answer.
> >> Thanks in advance
> >> Matt K
> >
> >You can't pass structures, arrays, functions, or anything larger
> >than the standard data types. But you can pass pointers to them.
> >The reason for this is that when you call a function and pass
> >variables to the function the variables are pushed onto the
> >stack, and since the stack has limited space it can't accomadate
> >large amounts of data.
>
> You can't pass arrays or functions, but you most certainly can pass
> structures as arguments.

Correct me if I'm wrong, but I do believe that when you pass the structure
name you are passing a pointer to the structure and not the actual structure.

>
> All memory is limited. With the C compilers I use stack space is
> limited by default to one megabyte, but its easy to increase this if
> necessary. If memory serves, the largest stack I've used was 16
> megabytes. That's room for quite a few moderate sized struct objects.

Why would you want to push that much data on the stack when you can just
use a pointer. Think of all the wasted processor time.

> Michael M Rubenstein

Mark

Michael Rubenstein

unread,
Jan 2, 1999, 3:00:00 AM1/2/99
to
On Fri, 01 Jan 1999 17:56:43 -0800, Mark Waluk
<ma...@freenet.edmonton.ab.ca> wrote:

>ywerther wrote:
>>
>> I've been teaching myself c/c++ for some time now and am grasping it pretty
>> well except for one feature: pointers. I know what they are definitively and
>> how to use them but the one thing no one has been able to explain to me is
>> WHY? What is the advantage? What is the difference between using a pointer
>> to a variable and using the variable itself? I've been trying to find out
>> for some time (most of my classmates are almost as smart as they think they
>> are) but I've had no clear answer.
>> Thanks in advance
>> Matt K
>
>You can't pass structures, arrays, functions, or anything larger
>than the standard data types. But you can pass pointers to them.
>The reason for this is that when you call a function and pass
>variables to the function the variables are pushed onto the
>stack, and since the stack has limited space it can't accomadate
>large amounts of data.

You can't pass arrays or functions, but you most certainly can pass
structures as arguments.

All memory is limited. With the C compilers I use stack space is


limited by default to one megabyte, but its easy to increase this if
necessary. If memory serves, the largest stack I've used was 16
megabytes. That's room for quite a few moderate sized struct objects.

Note that for all practical purposes, it looks like one can pass
functions as arguments since a bare function name is converted to a
pointer and declaring a parameter of type function is silently treated
as declaring a pointer to function. In many cases it appears that one
can pass arrays, since a parameter declared as an array is silently
treated as a pointer, but it isn't as transparent as for functions and
one must bear in mind that the argument seen by the function is really
a pointer.
--
Michael M Rubenstein

Michael Rubenstein

unread,
Jan 2, 1999, 3:00:00 AM1/2/99
to
On Fri, 01 Jan 1999 20:54:37 -0800, Mark Waluk
<ma...@freenet.edmonton.ab.ca> wrote:

>Michael Rubenstein wrote:
>>
>> On Fri, 01 Jan 1999 17:56:43 -0800, Mark Waluk
>> <ma...@freenet.edmonton.ab.ca> wrote:
>>
>> >ywerther wrote:
>> >>
>> >> I've been teaching myself c/c++ for some time now and am grasping it pretty
>> >> well except for one feature: pointers. I know what they are definitively and
>> >> how to use them but the one thing no one has been able to explain to me is
>> >> WHY? What is the advantage? What is the difference between using a pointer
>> >> to a variable and using the variable itself? I've been trying to find out
>> >> for some time (most of my classmates are almost as smart as they think they
>> >> are) but I've had no clear answer.
>> >> Thanks in advance
>> >> Matt K
>> >
>> >You can't pass structures, arrays, functions, or anything larger
>> >than the standard data types. But you can pass pointers to them.
>> >The reason for this is that when you call a function and pass
>> >variables to the function the variables are pushed onto the
>> >stack, and since the stack has limited space it can't accomadate
>> >large amounts of data.
>>
>> You can't pass arrays or functions, but you most certainly can pass
>> structures as arguments.
>

>Correct me if I'm wrong, but I do believe that when you pass the structure
>name you are passing a pointer to the structure and not the actual structure.
>
>>

>> All memory is limited. With the C compilers I use stack space is
>> limited by default to one megabyte, but its easy to increase this if
>> necessary. If memory serves, the largest stack I've used was 16
>> megabytes. That's room for quite a few moderate sized struct objects.
>

>Why would you want to push that much data on the stack when you can just
>use a pointer. Think of all the wasted processor time.
>
>> Michael M Rubenstein
>
>Mark

You are wrong. Of course the compiler might use some trickery to
actually pass only a pointer without making a copy in some cases, but
in others a copy is required. For example:

struct A
{
int n;
}

void f(struct A a)
{
a.n = 1;
/* ... */
}

void g()
{
struct A a;
g(a);
/* ... */
}

f() must not change the struct in g().

I didn't say I wanted to. It's unusual to pass a struct by value, but
appropriate in some cases. If the struct small it may be more
efficient. It also may be useful if the function changes the argument
and needs to make a copy anyways.
--
Michael M Rubenstein

Will Rose

unread,
Jan 2, 1999, 3:00:00 AM1/2/99
to
Mark Waluk (ma...@freenet.edmonton.ab.ca) wrote:
: Michael Rubenstein wrote:
: >
: > On Fri, 01 Jan 1999 17:56:43 -0800, Mark Waluk
: > <ma...@freenet.edmonton.ab.ca> wrote:
: >
: > >ywerther wrote:
: > >>
: > >> I've been teaching myself c/c++ for some time now and am grasping it pretty
: > >> well except for one feature: pointers. I know what they are definitively and
: > >> how to use them but the one thing no one has been able to explain to me is
: > >> WHY? What is the advantage? What is the difference between using a pointer
: > >> to a variable and using the variable itself? I've been trying to find out
: > >> for some time (most of my classmates are almost as smart as they think they
: > >> are) but I've had no clear answer.
: > >> Thanks in advance
: > >> Matt K
: > >
: > >You can't pass structures, arrays, functions, or anything larger
: > >than the standard data types. But you can pass pointers to them.
: > >The reason for this is that when you call a function and pass
: > >variables to the function the variables are pushed onto the
: > >stack, and since the stack has limited space it can't accomadate
: > >large amounts of data.
: >
: > You can't pass arrays or functions, but you most certainly can pass
: > structures as arguments.

: Correct me if I'm wrong, but I do believe that when you pass the structure
: name you are passing a pointer to the structure and not the actual structure.


No. You're thinking of K&R C. ANSI allows the passing of structures.


Will
c...@crash.cts.com


Al Bowers

unread,
Jan 4, 1999, 3:00:00 AM1/4/99
to

Mark Waluk wrote:

> Correct me if I'm wrong, but I do believe that when you pass the structure
> name you are passing a pointer to the structure and not the actual structure.

Just like, for example, type int, you can pass a copy of a struct and you
can pass a pointer of a struct.

#include <stdio.h>

typedef struct {
int i;
}NUM;

void testfunc(NUM anum);
void ptrtestfunc(NUM *anum);

int main(void) {
NUM num = {1};

printf("In main(), num.i = %d\n",num.i);
testfunc(num);
printf("In main(), num.i still = %d\n\n",num.i);
ptrtestfunc(&num);
printf("In main(), num.i now = %d\n",num.i);
return 0;
}

void testfunc(NUM anum) {
printf("In testfunc, anum.i = %d\n",anum.i);
puts("Changing anum.i");
anum.i = 2;
printf("In testfunc(), anum.i now = %d\n",anum.i);
}


void ptrtestfunc(NUM *anum) {
printf("In ptrtestfunc(), anum->i = %d\n",anum->i);
puts("changing anum->i");
anum->i = 3;
printf("In ptrtestfunc(),anum->i now = %d\n",anum->i);
}
--
Al Bowers
Tampa, FL
mailto:abo...@combase.com
http:www.gate.net/~abowers/index.html


William Boyle

unread,
Jan 11, 1999, 3:00:00 AM1/11/99
to
The basic reason for passing a point in C is that variables are passed
by value rather than reference. If you pass a non-point data type in a
function argument, any changes to the data made in the called function
are invisible to the caller. If the caller wishes to pass data to a
function to be changed in a way that it can see after the function
returns, then it needs to pass in a pointer. The contents of the data
item the pointer points to can be changed in the called function, and
since it is the same piece of memory passed to it, the changes will also
be visible to the caller. Example:


typedef struct foo { char name[10]; int value; } foo_t;

void initFoo( foo_t* aFoop )
{
if (aFoop != NULL)
{
/* value == -1 if name is empty string, 0 if not. */
aFoop->value = (aFoop->name[0] != '\0') ? 0 : -1;
}
}

foo_t* makeFoo( const char* name )
{
foo_t* p = calloc(1, sizeof(foo_t));
if (p)
{
if (name)
{
strncpy(p->name, name, 9);
name[10] = '\0';
}
initFoo(p);
if (p->value == -1) /* name not valid */
{
free(p);
p = NULL;
}
}
return p;
}

If you did not pass the object by pointer, then the data modified by
initFoo( foo_t* ) would not be properly initialized and the caller
makeFoo() would not be able to determine that the name was not valid.

This example is simplistic, but it should give you an idea why passing
pointers to functions in C is a necessary evil. In C++ you can pass an
object by reference which is safer as you don't need to verify that the
pointer is not NULL before you access it as we have to do above in
initFoo().

Bill Boyle
Principal Software Engineer
Brooks Automation, Inc.
Chelmsford, MA.

0 new messages