By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

use of char**

P: n/a
Hi ,

Can anyone tell me when should I use char**. Does it have nay extra
advantage over char*& ? "strtol" function takes char** as one of its
parameters. But,cant understand why that is needed? The same purpose
could have been served using char* only.

Looking forward to your replies and help inthis regard.

Thanks & Regards
Ambar

Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
am*********@gmail.com wrote:
Can anyone tell me when should I use char**.
You have a pointer to char. Take its address and you have a pointer to
a pointer to char. If you ever need to pass the address of a pointer to
char into a function, the type of the argument should be char**.
Does it have nay extra
advantage over char*& ?
Yes. Any pointer has an advantage over a reference: it can point to
"nothing" by being null and still be a valid pointer. References cannot
do that.
"strtol" function takes char** as one of its
parameters. But,cant understand why that is needed?
Because you can supply null there or the address of a variable that is
itself declared a pointer to char.
The same purpose
could have been served using char* only.
No, it couldn't. It's an argument that is returned changed.
Looking forward to your replies and help inthis regard.


V
Jul 23 '05 #2

P: n/a
am*********@gmail.com wrote in news:1121892595.540752.194860
@g44g2000cwa.googlegroups.com:
Hi ,

Can anyone tell me when should I use char**. Does it have nay extra
When you have a pointer that the callee is going to modify.
advantage over char*& ? "strtol" function takes char** as one of its
Um... no particular benefit .... unless you're calling a C function where
there are no references (like strtol). I personally prefer the reference.
parameters. But,cant understand why that is needed? The same purpose
could have been served using char* only.


strtol needs the char** because:

If endptr is not NULL, strtol() stores the address of the
first invalid character in *endptr. If there were no dig-
its at all, strtol() stores the original value of nptr in
*endptr (and returns 0). In particular, if *nptr is not
`\0' but **endptr is `\0' on return, the entire string is
valid.

Jul 23 '05 #3

P: n/a
Thanx for your reponse.
But,still some doubt remains.
You have a pointer to char. Take its address and you have a pointer to
a pointer to char. If you ever need to pass the address of a pointer to
My question is When shall I need to pass the address of a pointer to
char into a function?
No, it couldn't. It's an argument that is returned changed. My argument is char**. My question is what is changed over here char*
or & char*? If &char* is changed then whats the need for it?

Victor Bazarov wrote: am*********@gmail.com wrote:
Can anyone tell me when should I use char**.


You have a pointer to char. Take its address and you have a pointer to
a pointer to char. If you ever need to pass the address of a pointer to
char into a function, the type of the argument should be char**.
> Does it have nay extra
advantage over char*& ?


Yes. Any pointer has an advantage over a reference: it can point to
"nothing" by being null and still be a valid pointer. References cannot
do that.
> "strtol" function takes char** as one of its
parameters. But,cant understand why that is needed?


Because you can supply null there or the address of a variable that is
itself declared a pointer to char.
> The same purpose
could have been served using char* only.


No, it couldn't. It's an argument that is returned changed.
Looking forward to your replies and help inthis regard.


V


Jul 23 '05 #4

P: n/a
Hi,
Thanx for your response.
But, I still have some doubts.

You said : "When you have a pointer that the callee is going to modify.
"
It may sound odd but I still want to know why the callee would like to
modify a pointer ? specially with reference to strtol().

Whats the need ?

Jul 23 '05 #5

P: n/a
am*********@gmail.com wrote in news:1121919533.445623.313480
@g43g2000cwa.googlegroups.com:
Hi,
Thanx for your response.
But, I still have some doubts.

You said : "When you have a pointer that the callee is going to modify.
"
It may sound odd but I still want to know why the callee would like to
modify a pointer ? specially with reference to strtol().

Whats the need ?


Uh... I copied the text of what strtol does with that pointer...
Jul 23 '05 #6

P: n/a
am*********@gmail.com wrote:
Hi,
Thanx for your response.
But, I still have some doubts.

You said : "When you have a pointer that the callee is going to
modify. "
It may sound odd but I still want to know why the callee would like to
modify a pointer ? specially with reference to strtol().

Whats the need ?


This is from the VC++ 6.0 help:
* * *
long strtol( const char *nptr, char **endptr, int base );
....

Remarks

The strtol function converts nptr to a long. strtol stops reading the string
nptr at the first character it cannot recognize as part of a number. This
may be the terminating null character, or it may be the first numeric
character greater than or equal to base.

....

....If endptr is not NULL, a pointer to the character that stopped the scan
is stored at the location pointed to by endptr...

* * *

It would not be possible for the function to do with endptr as described if
it were not a pointer to a pointer.

DW


Jul 23 '05 #7

P: n/a
>> It may sound odd but I still want to know why the callee would like to
modify a pointer ?

When callee don't need to modify the object or pointer, parameter is
passed by value.
When callee need to modify the object, parameter is passed by pointer
or passed by reference.
When callee need to modify the address of the object, parameter is
passed by pointer to pointer, char** or char*&.

In context of strtol(const char *nptr,char **endptr, int base )
endptr is Pointer to character that stops scan.
In endptr, strtol returns the address of character at which scan is
stopped.

char *string = "-10110134932This stopped it", *stopstring;
l = strtol( string, &stopstring, 10 ); ///Here we are passing the
address of (*stopstring)
printf( "string = %s\n", string );
printf(" strtol = %ld\n", l );
printf(" Stopped scan at: %s\n\n", stopstring );

A simple example i want to show

#include <iostream>
using namespace std;
void test(char **l);
int main()
{
char *l = new char('A');
cout<<hex<<unsigned int (l)<<" value "<<*l<<endl;
test(&l);
cout<<hex<<unsigned int (l)<<" value "<<*l<<endl;
return 0;
}

void test(char **l)
{

*l = new char('B');
}
Output:
3018e0 value A
3018f0 value B ////see change in address, now it contains address of
another object.
Press any key to continue
Whats the need ?

Depends upon logic. Take a example of linked list. Suppose you want to
add an item to front of its head, then address of head of list will be
changed to its new item. This change should be reflected in our
program. Suppose we have a function AddItemToHeadOfList(List ** head,
Item i); which will take address of head of list and an item to be
added in front of list. Since this function will change the pointer to
head of list,we should pass as 'pointer to pointer **'

Jul 23 '05 #8

P: n/a
am*********@gmail.com wrote:
Hi,
Thanx for your response.
But, I still have some doubts.

You said : "When you have a pointer that the callee is going to modify.
"
It may sound odd but I still want to know why the callee would like to
modify a pointer ? specially with reference to strtol().

Whats the need ?

char* pch = some_address();
/* defined somewhere as following
void a_func1(char* p)
{
// some code using data at some_address()
p = new char[10];
// some code
}
*/
a_func1(pch);
// pch is not chainged at this point,
// so a_func1 can use the passed data
// but cannot return some data
// it alloc and initialize
/*
void a_func2(char** pp)
{
*pp = new char [10];
// some code;
}
*/
a_func2(&pch);
// here pch points to the data
// that was allocated in a_func2.

Normally, a function should return a pointer to allocated data.
But the ruturn value may be 'busy' with say error code.
So pointer-to-pointer is the standarc and "C-language compatable"
for out parameters of pointer type. You can user references for
the same purpose but in C++ only. strtol is a 'C' function AFAIK.

--
Serge
Jul 23 '05 #9

P: n/a
Andre Kostur schreef:
strtol needs the char** because:

If endptr is not NULL, strtol() stores the address of the
first invalid character in *endptr. If there were no dig-
its at all, strtol() stores the original value of nptr in
*endptr (and returns 0). In particular, if *nptr is not
`\0' but **endptr is `\0' on return, the entire string is
valid.


Of course, if it weren't for the fact that strtol is C, endptr
could have been a char*& and strtol could have had an 2-arg overload
without endptr. But if it was C++, the interface would have been
quite different.

Regards,
Michiel Salters

Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.