変数constchar** stringTableを宣言した場合、それがconstである場合、どのように値を設定できますか?(私が使用することになっている関数は、パラメーターとしてconst char **を受け取るため、constである必要があります。)
編集:いいえ、暗黙的にchar**からconstchar**に変換することはできません。コンパイラが文句を言います:パラメータ3を'char**'から'constchar**'に変換できません
うわー、誰もこれを持っていないことに驚いています!多分私はネクロマンサーバッジを手に入れることができます。暗黙のconstキャストは、1レベルの深さのみをスキャンします。したがって、achar*
はになりますconst char*
が、タイプを深く掘り下げて、char**
タイプをに変更するために何を変更する必要があるかを見つけることはできませんconst char**
。
#include <iostream>
using namespace std;
void print3( const char **three ) {
for ( int x = 0; x < 3; ++ x ) {
cerr << three[x];
}
}
int main() {
// "three" holds pointers to chars that can't be changed
const char **three = (const char**) malloc( sizeof( char** ) * 3 );
char a[5], b[5], c[5]; // strings on the stack can be changed
strcpy( a, "abc" ); // copy const string into non-const string
strcpy( b, "def" );
strcpy( c, "efg" );
three[0] = a; // ok: we won't change a through three
three[1] = b; // and the (char*) to (const char*) conversion
three[2] = c; // is just one level deep
print3( three ); // print3 gets the type it wants
cerr << endl;
return 0;
}
char**
あなたが取る関数に渡すことができる他の言及とは別にconst char **
、
const char**
はへの非定数ポインタconst char*
です。これを宣言して、型の値を自由に入れることができますconst char*
。
一方、const char * const *
またはとして宣言した場合は、それを行うことはできませんconst char * const * const
。
yourfunc(const char **p);
...
const char *array_str[10];
array_str[0] = "foo"; /* OK, literal is a const char[] */
yourfunc(array_str);
これがcdecl
言うことです:
cdecl> explain const char **table
declare table as pointer to pointer to const char
cdecl> explain const char * const *table
declare table as pointer to const pointer to const char
cdecl> explain const char * const * const table
declare table as const pointer to const pointer to const char
そのconst宣言は関数の検疫であり、それを埋める必要はありません。つまり、この関数は配列を変更せずに保持します(読み取りのみを行います)。したがって、constを期待する関数にnonconst変数を渡すことができます。
char **
-を取ると宣言された関数にを渡すことができます-MSDNのconstconst char **
のドキュメントを見る価値があるかもしれません
char **
に変換できるので、をパラメータとして受け取る関数を呼び出しconst char **
たい場合は、を指定するだけで暗黙的に変換されます。const char **
char **
パラメータとしてasを受け取り、それが参照するデータを変更する関数を記述したい場合は、キャストを介して機能させることができたとしても、コンパイラとの契約を破っています。const char **
char
私のコンパイラ(cygwinのgccバージョン3.4.4)を使用すると、ほとんどの回答が言っていることとは異なり、に渡すことはできますが、渡すchar *
ことはできconst char *
ませんでしchar **
た。const char **
これが、機能するものを構築するための1つの方法です。多分それはあなたを助けるでしょう。
void printstring( const char **s ) {
printf( "%s\n", *s );
}
int main( int argc, char** argv ) {
char *x = "foo"; // here you have a regular mutable string
const char *x2 = x; // you can convert that to a constant string
const char **y = &x2; // you can assign the address of the const char *
printstring(y);
}
キャスト演算子を使用してchar*の構成を解除できます:(char *)
void do_something(const char* s)
{
char* p=(char*)s;
p[0]='A';
}
配列char**で同じアイデアを使用します
const char **
基になる文字が定数であることを示します。したがって、次のようなことはできませんが:
const char **foo = ...;
**foo = 'a'; // not legal
しかし、ポインター自体の操作を妨げるものは何もありません。
// all the following is legal
const char **foo = 0;
foo = (const char **)calloc(10, sizeof(const char *));
foo[0] = strdup("foo");
foo[1] = strdup("baz");
つまり、実際の文字データを変更したい場合は、非 const ポインターを使用してキャストできます。
char **foo = ...;
func((const char **)foo);