Difference between const char * and char * in C ++ Parameters

Source: Internet
Author: User

When calling a function, we often see that the acceptance parameter of a function is (const char *);

For example, the strlen () function is defined:

  *

So what are the benefits of setting the form parameter to const? The answer on the network is: this will limit the form parameter to a constant so that we cannot modify it. I always feel that this statement is clear, but I think it is a little bit less.

In my opinion, there are two advantages:

First, by limiting the modification of real parameters, the runtime errors will be brought to the compiler in advance, so that the protection of non-error can be changed from human to machine, which is more reliable. This statement is more profound and fundamental than the statement "const limits parameters to constants, so that we cannot modify them", which is more impressive!
For example, in C ++, the heap memory is released into the destructor to achieve this guarantee. Let the machine implement it automatically, instead of letting a person swear: "I can ensure that all resources are released ". ---- Reference from Matrix_R

Second, it expands the parameter receiving range of the function, making the function more universal.

In the following example, we can see the two advantages: Write a function length () to implement the same functions as strlen () and then call the function. The Code is as follows:

#include <><stdio.h><tchar.h><iostream>  length( * count =  (;*s++ !=;count++ _tmain( argc, _TCHAR* str1[] = );      *s  =      

The above program can be correctly compiled and run, and three lines of "common length: 16" are output ".

However, what if I want to calculate the length of the Next string type? Because the accept parameter of length (char * s) is char *, we can use the c_str () method of stirng class to return the string character pointer (char *). Therefore, we perform the following operations:

 str2 = 

The above method does not seem to have any errors, but it cannot be compiled because str2.c _ str () returns const char *, while our length accepts char *, const char * cannot be converted to char.

Const char * s indicates that the content pointed to by the pointer is read-only and cannot be modified. The content pointed to by char * s can be modified, it is obviously not logical to convert a constant pointer to a common pointer. For the differences between constant pointers, pointer constants, and common pointers, see related documents.

Let's look back at the length requirement. The purpose of length is to calculate the length of a string. It does not modify the string itself. For functions with similar requirements (without modifying the real parameter ), we should and strongly recommend using const to modify the parameter, which is also a good coding habit.

Obviously, if void length (char * s) is defined like this, it will not accept the constant character pointer and constant character array. This is the 2nd benefit of using const to define parameters:Expands the receiving range of parameters of the function, making the function more universal.

Constant character pointer refers to the return value of str2.c _ str () or str2.data (), or defined by yourself.

Constant character array refers to: const char str1 [] = "you are a boy 1! ";

Finally, the above process illustrates the two benefits of using const to define parameters!

Reprinted Please note:

Name: difference between const char * and char * in C ++ Parameters

Author: Mr. heavy snow

Link: http://www.cnblogs.com/ishang/p/3309230.html

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.