1,872 views
3 votes
3 votes

What is the answer?

How char *const  and const char* differ 

2 Answers

Best answer
4 votes
4 votes

Copying a portion of C grammar for declarations:

declaration
  : declaration_specifiers ';'
  | declaration_specifiers init_declarator_list ';'
  ;
declaration_specifiers
  : storage_class_specifier
  | storage_class_specifier declaration_specifiers
  | type_specifier
  | type_specifier declaration_specifiers
  | type_qualifier
  | type_qualifier declaration_specifiers
  | function_specifier
  | function_specifier declaration_specifiers
  ;
storage_class_specifier
  : TYPEDEF
  | EXTERN
  | STATIC
  | AUTO
  | REGISTER
  ;
type_specifier
  : VOID
  | CHAR
  | SHORT
  | INT
  | LONG
  | FLOAT
  | DOUBLE
  | SIGNED
  | UNSIGNED
  | BOOL
  | COMPLEX
  | IMAGINARY
  | struct_or_union_specifier
  | enum_specifier
  | TYPE_NAME
  ;
type_qualifier
  : CONST
  | RESTRICT
  | VOLATILE
  ;
 
declarator
  : pointer direct_declarator
  | direct_declarator
  ;
direct_declarator
  : IDENTIFIER
  | '(' declarator ')'
  | direct_declarator '[' type_qualifier_list assignment_expression ']'
  | direct_declarator '[' type_qualifier_list ']'
  | direct_declarator '[' assignment_expression ']'
  | direct_declarator '[' STATIC type_qualifier_list assignment_expression ']'
  | direct_declarator '[' type_qualifier_list STATIC assignment_expression ']'
  | direct_declarator '[' type_qualifier_list '*' ']'
  | direct_declarator '[' '*' ']'
  | direct_declarator '[' ']'
  | direct_declarator '(' parameter_type_list ')'
  | direct_declarator '(' identifier_list ')'
  | direct_declarator '(' ')'
  ;
 
init_declarator_list
  : init_declarator
  | init_declarator_list ',' init_declarator
 

;

init_declarator
  : declarator
  | declarator '=' initializer
  ;
pointer
  : '*'
  | '*' type_qualifier_list
  | '*' pointer
  | '*' type_qualifier_list pointer
  ;
type_qualifier_list
  : type_qualifier
  | type_qualifier_list type_qualifier
  ;

Full list:

So, "const" is a type qualifier and in a declaration a type qualifier can come alone or followed by declaration specifier. ( "const; " is a valid C statement as per C grammar).  

Now see the "declarator". After a pointer we can have any number of type quantifiers. i.e.,

char * p, char * const p, char * const const p;

etc.  

const char *p1 = "hello";
const * char p1 = "hello";
char const * p1 = "hello";
char * const p1 = "hello"

There are 4 ways we can write "const" and "char" involving a pointer. The thing to identify is whether "pointer" is constant or if it is a normal pointer to a "const char". 

If pointer is "const", it cannot point to anything else and must be initialized (assigned a value along with declaration). 

If pointer is to a character constant, then the location pointed to by the pointer cannot be modified. (If modified result is undefined meaning anything can happen). 

So, lets see the 4 options I gave:

  1. Constant character pointer, p1 - yes, p1 is a pointer to a constant char.
  2. After "*" we cannot have a type specifier - hence invalid. 
  3. Character constant pointer, p1 - p1 is a pointer to a character constant - same as 1.
  4. Constant pointer to character, p1 - p1 is a constant pointer to a character. 

So, now coming to question:

p1 is a pointer to a character constant - (*p1) is READ ONLY

p2 is a constant pointer - p2 is READ ONLY.

So, statements 4 and 5 are not allowed. 

selected by
4 votes
4 votes

Answer is A).

const char* p1 ---> This means that the string is constant . so it is stored in a read area in memory . so we cannot change its contents or edit it . Hence , if we dare to change the contents of this string , it will throw compiler error , but pointer can point to any other string .

char* const p2 ---> This means that the pointer is constant so it cannot point to any other string but if we want to change the contents of the string , we can as it is not constant .

Now, stm 3 , P1 wants to point to other string which is allowed and hence no error.

In stm 4 , P2 wants to point to other string , which results in an error .

Stm 5 is an error bcoz, P1 cannot edit the contents of constant string .

Stm 6 has no error , as P2 can change the contents of the string .

Related questions

2 votes
2 votes
3 answers
1
0 votes
0 votes
1 answer
2
0 votes
0 votes
0 answers
3
0 votes
0 votes
0 answers
4
Hira Thakur asked Nov 18, 2017
343 views
The output of below code is_______________. int main() { int i = 120; int *a = &i; foo(&a); printf("%d ", *a); printf("%d ", *a); } void foo(int const a) { int j = 210; ...