Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Proper Initialization of Pointer to Pointer

Reply
Thread Tools

Proper Initialization of Pointer to Pointer

 
 
bwaichu@yahoo.com
Guest
Posts: n/a
 
      02-08-2008
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
 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      02-08-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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")
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-08-2008
"(E-Mail Removed)" <(E-Mail Removed)> 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.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-08-2008
(E-Mail Removed) 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"?

 
Reply With Quote
 
bwaichu@yahoo.com
Guest
Posts: n/a
 
      02-08-2008
On Feb 8, 7:54 am, santosh <(E-Mail Removed)> 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
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-08-2008
(E-Mail Removed) wrote:

> On Feb 8, 7:54 am, santosh <(E-Mail Removed)> 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.

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-08-2008
(E-Mail Removed) 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-08-2008
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> On Feb 8, 7:54 am, santosh <(E-Mail Removed)> 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
CJ
Guest
Posts: n/a
 
      02-09-2008
On Feb 8, 12:24 pm, Keith Thompson <(E-Mail Removed)> wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
> > On Feb 8, 7:54 am, santosh <(E-Mail Removed)> 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) <(E-Mail Removed)>
> 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
 
Reply With Quote
 
CJ
Guest
Posts: n/a
 
      02-09-2008
On Feb 9, 10:23 am, CJ <(E-Mail Removed)> wrote:
> On Feb 8, 12:24 pm, Keith Thompson <(E-Mail Removed)> wrote:
>
>
>
> > "(E-Mail Removed)" <(E-Mail Removed)> writes:
> > > On Feb 8, 7:54 am, santosh <(E-Mail Removed)> 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) <(E-Mail Removed)>
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
proper initialization Dennis Jones C++ 4 04-13-2006 09:47 PM
Proper class initialization Christoph Zwerschke Python 12 03-03-2006 08:27 PM
proper initialization bob@coolgroups.com C++ 2 02-10-2006 11:14 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments