473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

the & pointer operator to yield an address

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

Jan 30 '07 #1
8 1569
>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.
Jan 31 '07 #2
Al*******@gmail.com wrote:
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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jan 31 '07 #3
Al*******@gmail.com wrote:
>
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.

--
pete
Jan 31 '07 #4
<Al*******@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
>
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.
--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 31 '07 #5
Al*******@gmail.com wrote:
>
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/>

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Jan 31 '07 #6
On Jan 31, 1:25 am, gordonb.hr...@burditt.org (Gordon Burditt) wrote:
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.
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

Jan 31 '07 #7
Al*******@gmail.com wrote:
On Jan 31, 1:25 am, gordonb.hr...@burditt.org (Gordon Burditt) wrote:
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.
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.

Jan 31 '07 #8
On 31 Jan 2007 01:55:55 -0800, Al*******@gmail.com wrote:
>On Jan 31, 1:25 am, gordonb.hr...@burditt.org (Gordon Burditt) wrote:
>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.

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.
>>
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.
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
Feb 4 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
10
by: grocery_stocker | last post by:
Given: int main(void) { char *ptr = "test me"; printf("%s\n", &ptr); } Why would the output be
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
3
by: Michael | last post by:
Hi, I am a little confused about & and int &. Do they both mean reference? int a; int* b= & a ; (& here mean reference of a) int& b = a ; ( How to understand & precisely here?) Thanks...
6
by: Roman Mashak | last post by:
Hello, I encountered a piece of code, which I can't entirely understand. Here it is: #define TX_BUF_SIZE 1024 struct priv { ... unsigned char *tx_buf; unsigned char *tx_bufs;
12
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ...
6
by: Angel Tsankov | last post by:
How can an overloaded operator& take the address of its argument: template<typename T> Smth operator &(T& SomeObject) { // The address of SomeObject is needed here }
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.