Proper Initialization of Pointer to Pointer

Discussion in 'C Programming' started by bwaichu@yahoo.com, Feb 8, 2008.

  1. Guest

    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
    , Feb 8, 2008
    #1
    1. Advertising

  2. Army1987 Guest

    wrote:

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

    --
    Army1987 (Replace "NOSPAM" with "email")
    Army1987, Feb 8, 2008
    #2
    1. Advertising

  3. "" <> writes:

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

    --
    Ben.
    Ben Bacarisse, Feb 8, 2008
    #3
  4. santosh Guest

    wrote:

    > 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"?
    santosh, Feb 8, 2008
    #4
  5. Guest

    On Feb 8, 7:54 am, santosh <> wrote:

    >
    > 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
    , Feb 8, 2008
    #5
  6. santosh Guest

    wrote:

    > On Feb 8, 7:54 am, santosh <> wrote:
    >
    >>
    >> 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.


    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.
    santosh, Feb 8, 2008
    #6
  7. wrote:
    > ...
    > 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.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Feb 8, 2008
    #7
  8. "" <> writes:
    > On Feb 8, 7:54 am, santosh <> wrote:
    >> 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.


    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) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 8, 2008
    #8
  9. CJ Guest

    On Feb 8, 12:24 pm, Keith Thompson <> wrote:
    > "" <> writes:
    > > On Feb 8, 7:54 am, santosh <> wrote:
    > >> 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.

    >
    > 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) <>
    > 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
    CJ, Feb 9, 2008
    #9
  10. CJ Guest

    On Feb 9, 10:23 am, CJ <> wrote:
    > On Feb 8, 12:24 pm, Keith Thompson <> wrote:
    >
    >
    >
    > > "" <> writes:
    > > > On Feb 8, 7:54 am, santosh <> wrote:
    > > >> 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.

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


    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
    CJ, Feb 9, 2008
    #10
  11. CJ <> writes:

    > On Feb 8, 12:24 pm, Keith Thompson <> wrote:

    <on passing ** parameters>
    >> 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;
    >>
    >> }
    >>
    >> --

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

    --
    Ben.
    Ben Bacarisse, Feb 9, 2008
    #11
  12. Ben Bacarisse <> writes:

    > CJ <> writes:
    >
    >> On Feb 8, 12:24 pm, Keith Thompson <> wrote:

    > <on passing ** parameters>
    >>> So let's try something *slightly* more realistic to demonstrate the
    >>> point:

    <snip code>
    >>> --

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

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

    --
    Ben.
    Ben Bacarisse, Feb 9, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    941
  2. Christoph Zwerschke

    Proper class initialization

    Christoph Zwerschke, Mar 1, 2006, in forum: Python
    Replies:
    12
    Views:
    532
    Christoph Zwerschke
    Mar 3, 2006
  3. Replies:
    2
    Views:
    270
    Victor Bazarov
    Feb 10, 2006
  4. Dennis Jones

    proper initialization

    Dennis Jones, Apr 13, 2006, in forum: C++
    Replies:
    4
    Views:
    570
    Pete Becker
    Apr 13, 2006
  5. ptb
    Replies:
    1
    Views:
    234
Loading...

Share This Page