About the difference between const char*, Char const* and Char *const

Source: Internet
Author: User
Tags modifier

Note: The const and typedef are very special when used together

Trap One:

Remember, a typedef is a new alias that defines a type, and unlike macro, it is not a simple string replacement. Like what:
First define:
typedef char* PSTR;
And then:
int mystrcmp (const PSTR, const PSTR);

is the const PSTR actually equivalent to the const char*? No, it's actually equivalent to char* Const.
The reason is that const gives the whole pointer itself a constant, that is, a constant pointer char* const.
In short, remember that when the const and the typedef appear together, the typedef is not a simple string replacement.

Trap Two:

A typedef is syntactically a storage class keyword (like auto, extern, mutable, static, register, etc.), although it does not really affect the storage characteristics of the object, such as:
typedef static INT INT2; Not feasible
Compilation will fail, prompting "more than one storage class specified."

1 differences on const char*, Char const* and Char *const
2
3 Mnemonic methods:
4 read a declaration from right to left
5 For example:
6 char * const CP;
7//CP is a const pointer to Char
8
9 const char * CP;
Ten//CP is a pointer to const char;
11 rule:
12/Right to left read:
13//* read as pointer to
14//* * read into (a) pointer to (a) pointer to
A//* const read as const pointer to
16 Again for example:
char * * CPP; CPP is a pointer to (a pointer to char)
18
const char * * CPP; CPP is a pointer to (a pointer to const char)
20
char * const * CPP; CPP is a pointer to const pointer to Char
22
const char * const * CPP; CPP is pointer to const pointer to const CHAR
24
char * * Const CPP; CPP is const pointer to pointer to Char
26
char * CONST * Const CPP;//CPP is const pointer to const pointer to Char
28
const char * const * Const CPP; CPP is const pointer to const pointer to C
Onst Char
30
31
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
33//Key:
34//Just remember:
35//(1) Read the declaration from right to left
36//(2) * read as pointer to
Panax//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

We've all decorated a variable with the const modifier, and we know that after using the const modifier, this variable is equivalent to a constant, and his value can no longer be modified in other parts of the code, which is important for the function call to prevent errors from modifying the variables that should not have been modified, for example, Add a function to call B function for string processing, a the address of the string passed to B,b in the process mistakenly modified the address of the string, causing the program error, sometimes this error is difficult to find, at this time we can use the Const keyword.

But in the case of pointers, we do not want to modify the address that the pointer points to, but allow you to modify the value in the address pointed to by the pointer, so that you can understand this paragraph, and if char * a = PTR, then a cannot point to the other address, but the assignment to *a is OK. So if we write const char * or char const * OR

char * const, which one is what we need. Look at a piece of code.

1 #include <stdio.h>

2 int main (void)

3 {

4 char buf[] = "Hello World";

5 char buf2[] = "World Hello";

6 const char* A = BUF;

7 char const* b = buf;

8 char* Const C = buf;

9//*a = ' x ';

Ten//*b = ' t ';

One *c = ' t ';

A = BUF2;

b = buf2;

//c = Buf2;

printf ("A is%s/nb are%s/nc is%s/n", A, B, c);

0;

17}

If I remove the comment from line 9th of Line 8th, the following error will occur when compiling: (compilation environment GCC)

test.c:in function ' main ':

Test.c:9: Error:assignment of Read-only location

Test.c:10:error:assignment of Read-only Location

If 14 lines of comments are removed, the following error occurs:

test.c:in function ' main ':

Test.c:14:error:assignment of read-only variable ' C '

All right, let me sum up, the const char *, like the char const* effect, is not allowed to modify the value of the address space that the pointer points to, that is, the value as a constant, while char * const is not allowed to modify the pointer itself, can no longer point to other places, and uses the pointer Note that you must remember to assign an initial value when using the char * const to set a constant pointer, otherwise you won't be able to assign a value anywhere else.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.