1

Bellow is my C code:

const char *l = "some text";
const char * const *m = {l};

When I try to compile that code, I get this warning:

warning: initialization from incompatible pointer type

Can anybody explain me why that warning and how should I initialize the second variable (m)?

1
  • 1
    Why are you using { }? Commented Mar 8, 2013 at 13:01

2 Answers 2

3

Actually, you are not using the const keyword in the right way. const applies to the first left token it meets or, if there is not, to the first right token.

So, in your case :

const char * const *m;

The first const applies to char, just like for l. The second one applies to your first *, which means that m is a pointer to a constant pointer to a constant content ("some text"). Since you had not written

const char * const l;

There is a conflict with the const-ness of your two pointers hence the warning.

I think what you want is to guarantee that the address stored in l won't be altered by the program. If it is that so, then you must change the declaration of l to this one :

const char * const l = "some text";
Sign up to request clarification or add additional context in comments.

3 Comments

can you provide the documentation for: const applies to the first left token it meets or, if there is not, to the first right token. ? Thanks for the right answer!
in your case m is a pointer to a const pointer to a char constant(just as @Rerito said). according to clockwise spiral rule which you can find right here: c-faq.com/decl/spiral.anderson.html
@artaxerxe You can check Wikipedia's page about const-correctness : en.wikipedia.org/wiki/Const-correctness. It also can be inferred from the examples you will find on publications.gbdirect.co.uk/c_book/chapter8/… I just expressed the rule in an easier to remember fashion.
2

Why not use

const char* m[] = {l};

I think that should work.

I imagine you actually intend m to be more than one element long, otherwise you would not do something so convoluted, right?

1 Comment

because I need a that type: const char * const *

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.