1

When I compile the given code it doesn't produce any error or warnings. My question here is shouldn't the compiler produce error when compiling the following line *err = "Error message"; because we are dereferencing a pointer to pointer to constant char and assigning a string to it.

Is it allowable to assign anything inside a pointer anything other than address and exactly what is happening in this given scenario?

#include <stdio.h>

void set_error(const char**);

int main(int argc, const char* argv[])
{
    const char* err;
    set_error(&err);
    printf("%s",err);
    return 0;
}


void set_error(const char** err1) 
{
     *err1 = "Error message";
}
4
  • It's a const char**, you can modify the individual pointers (strings in the string array, conceptually), just not the characters of individual strings. You are thinking of char * const * (or something). Commented Dec 5, 2013 at 14:34
  • err is a non-const pointer to a non-const pointer to a const char. So yes, you can modify it. Commented Dec 5, 2013 at 14:34
  • Frist question I see with complaining that there is no warning nor errors and everything works fine ;) (+1) Commented Dec 5, 2013 at 14:34
  • C and C++ are different programming languages. Pick one. Don't attempt to program in two different languages at the same time. A C program compiled on a C++ compiler will be crap, and a C++ program that only uses C programming practices will also be crap (from a C++ programmer's point of view). Commented Dec 5, 2013 at 15:02

3 Answers 3

9
const char** err1

That's a pointer to a non-constant pointer to a constant object. Dereferencing it gives a non-constant pointer (to a constant object), which can be assigned to.

To prevent assigning to the const char*, that would also have to be const:

const char * const * err1
Sign up to request clarification or add additional context in comments.

1 Comment

Wow really subtle! I will always prefer STL containers any day.
4

"Error message" is not a std::string. It's a const char[]. All string literals in C++ are const char[]. In C, they're char[].

2 Comments

But in C, you should still treat them as if they have const char[] type. Everything else is poor programming practice.
@Lundin I would say that in C you must still treat them as if they have const char[] type, because the implementation is allowed to put string literals into read-only memory.
1

Is it allowable to assign anything inside a pointer anything other than address and exactly what is happening in this given scenario?

You can assign pointer to a pointer. You think about pointer as an address, that's fine to understand concept, but do not mix it with data type. Data type is a pointer, not address. For example to assign address in memory to a pointer you need to cast it to a pointer:

char *pointer = reinterpret_cast<char *>( 0xA000000 );

You may ask how this would compile?

int array[10];
int *ptr = array;

That comes from C - array can be implicitly converted to a pointer to the first element. So it is pointer to pointer assignment again. Now about string literal with double quotes. It is an array as well:

const char str[] = "str";
const char str[] = { 's', 't', 'r', '\0' }; 

These 2 statements are pretty much the same. And as array can be implicitly converted to pointer to the first element it is fine to assign it to const char *

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.