c*******9 发帖数: 6411 | 1 I have a c++ function with this signature void f(char ** input)
char a[10] = "":
strcpy( a, anotherstring.c_str());
f( &a );
The compiler complain about can not convert the type for function f
parameter.
any ideas what is wrong?
thanks, | X****r 发帖数: 3557 | 2 '&a' is of type 'char (*)[10]', not of type 'char **'.
【在 c*******9 的大作中提到】 : I have a c++ function with this signature void f(char ** input) : char a[10] = "": : strcpy( a, anotherstring.c_str()); : f( &a ); : The compiler complain about can not convert the type for function f : parameter. : any ideas what is wrong? : thanks,
| c*******9 发帖数: 6411 | 3 I think a is name of an array of char, so a is pointer to the a[0], which
should be of type char*, is that correct?
if so &a should be of type char** ?
thanks... | X****r 发帖数: 3557 | 4 No, this understanding is wrong.
If you declare 'char a[10];', 'a' has type 'char [10]', and '&a' has
type 'char (*)[10]'.
If you declare 'char *b;', 'b' has type 'char *', and '&b' has type
'char **'.
Type 'char []' can be implicitly converted into type 'char *', so 'a'
can be used in almost everywhere 'char *' is required. However,
'char (*)[]' cannot be converted to 'char **', so your code won't work.
which
【在 c*******9 的大作中提到】 : I think a is name of an array of char, so a is pointer to the a[0], which : should be of type char*, is that correct? : if so &a should be of type char** ? : thanks...
| X****r 发帖数: 3557 | 5 What I just said is on the syntactic level.
On the semantic level, a is an array -- it contains all its elements.
It is not the same as a pointer to its first element. The latter is
'&a[0]'. For example, sizeof(a) is 10, which is the length of the array
times size of its individual element; while sizeof(&a[0]) is 4 on
32-bit architecture, which is the size of a 'char *' pointer.
Back to your problem: 'f' requires a 'char ** input'. Now assuming
you could just pass '&a' in. What would happen if f
【在 c*******9 的大作中提到】 : I think a is name of an array of char, so a is pointer to the a[0], which : should be of type char*, is that correct? : if so &a should be of type char** ? : thanks...
| c*******9 发帖数: 6411 | 6 Thank you Xentar...
is the following OK and safe, is strcpy potentially wiping out existing data
(the memory after a)?
char* a:
strcpy( a, anotherstring.c_str());
f( &a );
all I want to do here is to pass a char** that point to a copy of value
anotherstring.c_str()...
what is the best way to do it?
Thanks again. | X****r 发帖数: 3557 | 7 No, that's wrong, either, because you didn't allocate memory
for a.
What you want to do could be the following:
char a[10], *b = a;
strcpy(a, anotherstring.c_str());
f(&b);
I didn't reply with the solution immediately because I wanted you
to understand the issue.
Another unrelated reminder is that strcpy is inherently dangerous.
Unless you're 100% sure the length of anotherstring is no greater
than 9, your code is not safe. Use strncpy instead.
existing data
value
【在 c*******9 的大作中提到】 : Thank you Xentar... : is the following OK and safe, is strcpy potentially wiping out existing data : (the memory after a)? : char* a: : strcpy( a, anotherstring.c_str()); : f( &a ); : all I want to do here is to pass a char** that point to a copy of value : anotherstring.c_str()... : what is the best way to do it? : Thanks again.
| c*******9 发帖数: 6411 | 8 Thanks Xenta...
will the following work also?
char a[10]:
strcpy( a, anotherstring.c_str());
f( &(&a[0]) ); | p***o 发帖数: 1252 | 9 Get a C book. You won't learn C from BBS.
BTW, the function f(char **p) looks quite suspicious.
It sounds to me it want to do some tricky things like
freeing *p following by allocating another buffer and
passing it outside, pretty much realloc. It will get
into serious problem if you pass in an array in this
case.
【在 c*******9 的大作中提到】 : Thanks Xenta... : will the following work also? : char a[10]: : strcpy( a, anotherstring.c_str()); : f( &(&a[0]) );
| X****r 发帖数: 3557 | 10 No. &a[0] is not a lvalue itself, so you cannot take its address.
【在 c*******9 的大作中提到】 : Thanks Xenta... : will the following work also? : char a[10]: : strcpy( a, anotherstring.c_str()); : f( &(&a[0]) );
|
|