0
int const *ptr;
int *const *ptr;
int **const ptr;
int const **ptr;

I know that whatever is followed by the keyword const cannot be re-written (here I cannot re-write *ptr, *ptr, ptr, **ptr respectively). But I am not sure about the behaviors of the pointers before the keyword const and also unable to find which and all are undefined behavior. It would be helpful if someone can explain me this.

Thanks

Rndp13
  • 1,094
  • 1
  • 21
  • 35
  • 3
    What do you mean by *undefined behaviour* here? They are all valid declarations of different types. – P.P Jun 09 '15 at 10:21
  • 2
    Why are you expecting anyone of them as undefined behavior example? – Dayal rai Jun 09 '15 at 10:26
  • 3
    There isn't even "behaviour" in the code you posted, since it does not do anything (except declaring variables). – urzeit Jun 09 '15 at 10:26
  • In a site, they had mentioned **const ptr & const *ptr as undefined. So I wanted to know which are all valid and invalid declarations. –  Jun 09 '15 at 10:30
  • 1
    Please supply a link to the site where you read this - it may be incorrect or it may just be that you mis-interpreted it. – Paul R Jun 09 '15 at 10:40
  • 2
    @Denise "Undefined behaviour" and "invalid declaration" are completely different concepts. An invalid declaration won't compile, undefined behaviour occurs when a program is run. – molbdnilo Jun 09 '15 at 10:40
  • Yeah thank you, I wanted to know which and all are undefined behaviors hence posted the question @molbdnilo –  Jun 09 '15 at 10:47
  • site : http://www.sanfoundry.com/multiple-choice-questions-c-pointers-function-arguments/ --------qn no: 7 (for **const ptr) @Paul R. For const *ptr, I will search and post it. Please give me some time.. –  Jun 09 '15 at 10:49
  • 1
    @Denise: the material on that site looks to be of questionable benefit - I recommend getting a decent book and work through that instead - here's [a useful list](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Paul R Jun 09 '15 at 11:01
  • Sure, thank you @Paul R –  Jun 09 '15 at 11:09
  • 2
    @Denise I counted three errors in those nine questions. You probably want to stay away from that site. (The answer to 7 is "the program is undefined", which isn't even one of the choices.) – molbdnilo Jun 09 '15 at 11:41
  • @molbdnilo why the program is undefined –  Jun 16 '15 at 06:43

1 Answers1

4

None of them are "undefined behavior". They are just different pointer declarations:

int const *ptr;    // pointer to const int
int *const *ptr;   // pointer to const pointer to int
int **const ptr;   // const pointer to pointer to int
int const **ptr;   // pointer to pointer to const int

If something is const then it can't be modified. For pointers this means that the pointer itself can not change (i.e. it can not be modified to point at something else), however whatever the pointer points at may still be modifiable, unless it too is const.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Thank you @Paul R. Could you please tell me the usage of *const *ptr and ** const ptr –  Jun 09 '15 at 10:34
  • The comments above explain what each declaration means. I'll add some further explanation though. – Paul R Jun 09 '15 at 10:37
  • Yeah, but I find it difficult to differentiate and understand the usage of *const & **const from const (when followed by the same variables). Is there any signiificant usage of "constant pointers to int" ? –  Jun 09 '15 at 11:04
  • Yes of course - there are really two different and unrelated concepts here - pointers which are `const` (and therefore unmodifiable, such that they can't point at anything else) and data that is `const` (and therefore unmodifiable) and which may be accessed via a pointer (either `const` or non-`const`). Try separating out the two ideas and make you sure you understand each in isolation. Also, as mentioned above, you might want to get a good book on C rather than trying to learn from interview questions on dubious web sites. – Paul R Jun 09 '15 at 11:14