Proper Initialization of Pointer to Pointer

B

bwaichu

What is the proper way to initialize a pointer to a pointer?

Say, I have a pointer:

char *ptr

and I need to pass it's pointer in a function:

func(&ptr)

What's the correct way to setup the pointer?

Thanks,

Brian
 
A

Army1987

What is the proper way to initialize a pointer to a pointer?

Say, I have a pointer:

char *ptr

and I need to pass it's pointer in a function:

func(&ptr)

What's the correct way to setup the pointer?

What does func need to do with it?
 
B

Ben Bacarisse

What is the proper way to initialize a pointer to a pointer?

Say, I have a pointer:

char *ptr

and I need to pass it's pointer in a function:

func(&ptr)

What's the correct way to setup the pointer?

That's fine as it is. The function prototype will be something like

void func(char **pstr);

and inside the function *pstr refers to the modifiable object called
ptr in the calling function.
 
S

santosh

What is the proper way to initialize a pointer to a pointer?

With an address of an object of the type that it is a pointer to.

T **ptr must be initialised either to NULL, 0 or a legal value of type
T*.
Say, I have a pointer:

char *ptr

and I need to pass it's pointer in a function:

func(&ptr)

What's the correct way to setup the pointer?

The method that you use is correct. What exactly do you mean by "setup
the pointer"?
 
B

bwaichu

The method that you use is correct. What exactly do you mean by "setup
the pointer"?

What, if any, *initialization* must occur to the underlying pointer?
Is it acceptable to declare a pointer:

char *ptr

and pass it in a function that requires it's pointer:

func(char **ptr)

where it's passed as

func(&ptr)

without doing anything else? This just seems wrong to me.


Brian
 
S

santosh

What, if any, *initialization* must occur to the underlying pointer?
Is it acceptable to declare a pointer:

char *ptr

and pass it in a function that requires it's pointer:

func(char **ptr)

where it's passed as

func(&ptr)

without doing anything else? This just seems wrong to me.

Why would it be wrong? The function might want to set 'ptr' to some
object or array of objects of static duration. The Standard library
functions strto*() use this method.

It /would/ be wrong if the func() expected 'ptr' to be sensibly
initialised. In this case you must, of course, do so before calling
func(). However this needn't always be the case.

Passing a pointer to an uninitialised pointer is perfectly legal.
Whether it is semantically okay depends on the exact context.
 
A

Andrey Tarasevich

...
What, if any, *initialization* must occur to the underlying pointer?
Is it acceptable to declare a pointer:

char *ptr

and pass it in a function that requires it's pointer:

func(char **ptr)

where it's passed as

func(&ptr)

without doing anything else? This just seems wrong to me.
...

There's nothing wrong with it as long as the function understands that 'ptr'
might be pointing to an uninitialized pointer and behaves accordingly.
 
K

Keith Thompson

What, if any, *initialization* must occur to the underlying pointer?
Is it acceptable to declare a pointer:

char *ptr

and pass it in a function that requires it's pointer:

func(char **ptr)

where it's passed as

func(&ptr)

without doing anything else? This just seems wrong to me.

I think you're creating some unnecessary confusion by using the same
name "ptr" for two different things. Yes, they're both pointers, but
they're different kinds of pointers; one is an object (a variable) of
type char*, and the other is a parameter of type char**.

In real code, you presumably wouldn't use the name "ptr" for either of
them (nor would you call a function "func"). Entities should
generally have names that describe what you use them for, not how
they're declared (as a pointer or a function or whatever).

So let's try something *slightly* more realistic to demonstrate the
point:

#include <stdio.h>

void set_string(char **target)
{
*target = "Hello, world";
/*
* Of course this assignment doesn't copy the string;
* it assigns the address of the string literal to
* *target.
*/
}

int main(void)
{
char *message;
/*
* message is a char*, not yet initialized
* (it doesn't yet point to anything)
*/

/*
* Now we pass the address of message to the
* set_string function, allowing set_string
* to initialize message for us.
*/
set_string(&message);

/*
* Let's see if it worked.
*/
puts(message);
return 0;
}
 
C

CJ

I think you're creating some unnecessary confusion by using the same
name "ptr" for two different things. Yes, they're both pointers, but
they're different kinds of pointers; one is an object (a variable) of
type char*, and the other is a parameter of type char**.

In real code, you presumably wouldn't use the name "ptr" for either of
them (nor would you call a function "func"). Entities should
generally have names that describe what you use them for, not how
they're declared (as a pointer or a function or whatever).

So let's try something *slightly* more realistic to demonstrate the
point:

#include <stdio.h>

void set_string(char **target)
{
*target = "Hello, world";
/*
* Of course this assignment doesn't copy the string;
* it assigns the address of the string literal to
* *target.
*/

}

int main(void)
{
char *message;
/*
* message is a char*, not yet initialized
* (it doesn't yet point to anything)
*/

/*
* Now we pass the address of message to the
* set_string function, allowing set_string
* to initialize message for us.
*/
set_string(&message);

/*
* Let's see if it worked.
*/
puts(message);
return 0;

}

--
Keith Thompson (The_Other_Keith) <[email protected]>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Of course it won't work, you are returning a pointer from the previous
method stack. However if you malloc() the memory to be returned, this
is perfectly valid usage, and is often done.

cj
 
C

CJ

Of course it won't work, you are returning a pointer from the previous
method stack. However if you malloc() the memory to be returned, this
is perfectly valid usage, and is often done.

cj

Never mind, I totally misread your set_message(). That is fine code.

Thinking before I write has always been a challenge for me.
Sorry.

cj
 
B

Ben Bacarisse

CJ said:
On Feb 8, 12:24 pm, Keith Thompson <[email protected]> wrote:
<snip>

Please don't quote sigs.
Of course it won't work, you are returning a pointer from the previous
method stack.

What on is the "previous method stack"? If you mean that Keith
Thompsons's example returns a pointer to automatic storage (which is a
common beginner error) look again. Of course, I may also have missed
something, but it looks entirely correct to me.
 
B

Ben Bacarisse

Ben Bacarisse said:
<snip>

Please don't quote sigs.
Of course, I may also have missed
something, but it looks entirely correct to me.

.... and I missed seeing your posting pointing out you misread the code
by a few seconds.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top