由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - a c++ question.
相关主题
请教一道c/c++题C++一个string的小问题
typedef basic_string string;question about structure initializationa and reference
What is wrong with the constructor calling?请教boost::any compile错误。
a string define question (c++)问个简单的memory allocation 的问题。
再问一个free()的问题[合集] &x[1]和x+1是一回事吧?不管c还是c++?
Array in C关于 exception 的一个问题
array allocation in cstrcat()
C puzzle 一日一题请帮忙看看这个字符函数的错误在哪里
相关话题的讨论汇总
话题: char话题: type话题: strcpy话题: what话题: str
进入Programming版参与讨论
1 (共1页)
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]) );

1 (共1页)
进入Programming版参与讨论
相关主题
请帮忙看看这个字符函数的错误在哪里再问一个free()的问题
问个char*的问题Array in C
请教一个C里面string copy的问题array allocation in c
C++ STL map find does not work ???C puzzle 一日一题
请教一道c/c++题C++一个string的小问题
typedef basic_string string;question about structure initializationa and reference
What is wrong with the constructor calling?请教boost::any compile错误。
a string define question (c++)问个简单的memory allocation 的问题。
相关话题的讨论汇总
话题: char话题: type话题: strcpy话题: what话题: str