the & pointer operator to yield an address

A

Alef.Veld

Just wondering,

I sometimes use or see &ptr when passing it through a function so that
this specific function works with the actual pointer passed. But if we
just allocate memory to this pointer before, and then pass it as
func(ptr) instead of func(&ptr), what's the use of the & operator ?

Or is this just extra functionality of c. I can ofcourse imagine that
you would want to allocate memory within the function itself, in which
ofcourse the &operator is wanted.

Rgds,
alef
 
G

Gordon Burditt

I sometimes use or see &ptr when passing it through a function so that
this specific function works with the actual pointer passed. But if we
just allocate memory to this pointer before, and then pass it as
func(ptr) instead of func(&ptr), what's the use of the & operator ?

func(ptr) and func(&ptr) pass *DIFFERENT VALUES* and *DIFFERENT
TYPES* to func(). If ptr has type pointer-to-something, &ptr has
type pointer-to-pointer-to-something.

Often &ptr is passed to func because func is going to store something
in ptr. func(&ptr) is perfectly legal if ptr is uninitialized.
func(ptr) is not.
 
J

Joe Wright

Just wondering,

I sometimes use or see &ptr when passing it through a function so that
this specific function works with the actual pointer passed. But if we
just allocate memory to this pointer before, and then pass it as
func(ptr) instead of func(&ptr), what's the use of the & operator ?
Let's for sake of argument say main() calls bar() with ptr of interest.
Which of main() or bar() shall allocate memory and therefore determine
the value of ptr? In the case that main() allocates memory for ptr then
the & operator is of no interest..

void bar(char *ptr) {
strcpy(ptr, "Hi Sailor");
}
int main(void) {
char *ptr = malloc(20);
bar(ptr);
puts(ptr);
return 0;
}

If, on the other hand, malloc was being done in bar() you would want
something like..

void bar(char **ptr) {
*ptr = malloc(20);
strcpy(*ptr, "Hi Sailor");
}
int main(void) {
char *ptr;
bar(&ptr);
puts(ptr);
return 0;
}
Or is this just extra functionality of c. I can ofcourse imagine that
you would want to allocate memory within the function itself, in which
ofcourse the &operator is wanted.
The & operator is clearly wanted.
 
P

pete

Just wondering,

I sometimes use or see &ptr when passing it through a function so that
this specific function works with the actual pointer passed. But if we
just allocate memory to this pointer before, and then pass it as
func(ptr) instead of func(&ptr), what's the use of the & operator ?

Or is this just extra functionality of c. I can ofcourse imagine that
you would want to allocate memory within the function itself, in which
ofcourse the &operator is wanted.

If you look at the description for the getline function
http://www.crasseux.com/books/ctutorial/getline.html
which is not a standard function, you can see in the example
that the function is called with two of the arguments being
results of the & operator.
getline updates the values of my_string and nbytes,
so it needs their addresses.
 
D

David T. Ashley

I sometimes use or see &ptr when passing it through a function so that
this specific function works with the actual pointer passed. But if we
just allocate memory to this pointer before, and then pass it as
func(ptr) instead of func(&ptr), what's the use of the & operator ?

A called function often needs to modify the caller's pointer. As an
example, if a buffer serves as both input and ouput to a function, it may
need to make the buffer larger using a call to realloc(), which means that
it also needs to modify the caller's pointer to the buffer.

Using func(&ptr) is in effect "pass by reference" on the pointer. It is a
pointer to the pointer.

There are numerous other calling conventions possible: for example, the
called function may return through the function name an optional updated
pointer. But allowing the called function to modify the caller's pointer is
simpler and cleaner.

A separate use is allowing a function to return a pointer through the
argument list.
 
C

CBFalconer

I sometimes use or see &ptr when passing it through a function so
that this specific function works with the actual pointer passed.
But if we just allocate memory to this pointer before, and then
pass it as func(ptr) instead of func(&ptr), what's the use of the
& operator ?

Or is this just extra functionality of c. I can ofcourse imagine
that you would want to allocate memory within the function itself,
in which ofcourse the &operator is wanted.

For example the ggets function (non-standard, available below) is
called with a pointer to a pointer:

int ggets(char *p);

....
char *line;

while (0 == ggets(&line) {
process(line);
free(line);
}

See: <http://cbfalconer.home.att.net/download/>
 
A

Alef.Veld

func(ptr) and func(&ptr) pass *DIFFERENT VALUES* and *DIFFERENT
TYPES* to func(). If ptr has type pointer-to-something, &ptr has
type pointer-to-pointer-to-something.

I know that :). I just wanted to know what the many uses were, seeing
as
you could alloc before and just pass that address.
Often &ptr is passed to func because func is going to store something
in ptr. func(&ptr) is perfectly legal if ptr is uninitialized.
func(ptr) is not.

What happens exactly though if you would do that. It probably just
copy the
'nothingness'/garbage into the new functions argument pointer, making
it
a copy of a unintialized pointer.

Thanks Joe, that's just what i thought to. It's a design matter. I
will have a
look at your ggets function Chuck, thanks.

Kind regards,
alef
 
G

Guest

What happens exactly though if you would do that. It probably just
copy the
'nothingness'/garbage into the new functions argument pointer, making
it
a copy of a unintialized pointer.

You're not supposed to read invalid pointers in C, even if you don't
dereference them. If you do, the implementation is allowed to do
anything it wants. At least one implementation (optionally) uses this
permission to cause the program to warn the user that the code is
buggy, and to provide the user with an option to directly abort the
program.
 
B

Barry Schwarz

I know that :). I just wanted to know what the many uses were, seeing
as
you could alloc before and just pass that address.

Whether to perform the allocation prior to calling the function or
within the called function is a design decision.

This presupposes the only reason for passing &ptr to a function is for
allocations. The reason for passing &ptr is so the function can
update ptr directly. While allocation is certainly one case where you
might want to update the pointer, there are others.
What happens exactly though if you would do that. It probably just
copy the
'nothingness'/garbage into the new functions argument pointer, making
it
a copy of a unintialized pointer.

Forget probably. What it definitely does is invoke undefined
behavior. There are systems where simply evaluating an invalid
address (not actually attempting to access the data) causes an
abnormal condition similar to a segfault.


Remove del for email
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top