-1

I want to set return a const char* into char** I want the compiler to validate there is no change on that char*.

What the correct syntax should look like?

const char* str = "some str";
void test(char const **out_str) {
    *out_str = str;
}

EDIT:

Maybe something like that?

char* const str = "some str";
void test(char** const out_str) {
    *out_str = str;
}
Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
igor
  • 716
  • 1
  • 9
  • 27
  • Are you saying you intend to call this function, passing a `char**` to it ? And then expect the compiler to know that in *fact* it's a `const char**` ? Or am I misunderstanding your intent ? Can you clarify how you intend to call this, and where you would like this "compiler validation" to happen ? – Sander De Dycker Jul 23 '19 at 12:15
  • While literal strings in C are not constant (i.e. you can have a `char *` variable point to a string literal) they are still read-only. Therefore it's recommended to always use `const char *` for string literals. That also means your wanted use of `char *` for pointing to a string literal is suspect. – Some programmer dude Jul 23 '19 at 12:17

1 Answers1

0

Here is a demonstrative program that shows how the function can be called.

#include <stdio.h>

void test( char const **out_str ) 
{
    const char *str = "some str";
    *out_str = str;
}

int main( void )
{
    const char *p;

    test( &p );

    puts( p );
}

The program output is

some str

String literals have static storage duration. So the code is valid.

As for this declaration char** const then it denotes a constant pointer to a pointer to char.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335