Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- yourfunc(const char **p);
- ...
- const char *array_str[10];
- array_str[0] = "foo"; /* OK, literal is a const char[] */
- yourfunc(array_str);
- 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
- #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;
- }
- void printstring( const char **s ) {
- printf( "%sn", *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);
- }
- void do_something(const char* s)
- {
- char* p=(char*)s;
- p[0]='A';
- }
- 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");
- char **foo = ...;
- func((const char **)foo);
Add Comment
Please, Sign In to add comment