Thoughts on constant pointers

Source: Internet
Author: User

// It's too speechless. It's too speechless. Occasionally, you don't need to use live write to write things. The browser is almost finished --

 

// Rewrite it. It's just for cainiao. It's more than a month old.

 

I 've been learning VC for almost two months. During this time, I 've been entangled in various constants and pointers. * & what are the characters? = crash.

At present, there are still a lot to be confused, but some of them know. First, let's summarize some of them about constant pointers.

 

Environment: VC console application

First, read here

Const char * pcstr;

Const char castr [] = "Hello, VC ";

Pcstr = "Hello, Ms ";

Cout <"pcstr =" <pcstr <Endl <"castr =" <castr;

Through the above lines of code, we can perform a preliminary test. Why does one initialize (assign an initial value or define it?) during declaration ?), What about initialization? In fact, both can be initialized during declaration, but not all can be initialized later, because the const keyword indicates that the variable must be initialized during Declaration, that is, you will not be given a chance later. This is a feature of Const, you can change it

Const char castr [9]; // Note: The String constant (in the form of "skkhfsf") is automatically followed by a \ 0 as the ID of a string, it looks like the first terminator in the ASCII code

Castr = "Hello, VC ";

In this case, an error is returned.

Then we can see that pcstr is not a constant!

OK. What is it? It is called a pointer to a const char.

Note: Remember that the array name is the pointer to the array and the address of the first element of the array, right?

 

Okay. Let's continue. Add the code below.

Pcstr = "Hello, Sun ";

Cout again, OK, the output is normal

Add castr = "Hello, xx"

Cout again, what is it, right? The reason is as follows: Key const rules

 

Now that it is VC, let's talk about it here. We can record it like this: the LP is the pointer. This is a pointer type. W indicates the wchar_t type, the wide character set. C indicates the const, that is, the pointer to the constant. STR is a string, or you can call it an array of characters ending with the \ 0 mark.

{

I can skip it if I don't understand it at the moment, but I need to understand it later.

How can I handle a pcstr error?

* Pcstr = "Hello, eee ";

Then cout it. What is it like? It's wrong. Why? Because the address stores constants.

}

 

 

OK.

Char * const ccpstr = "Hello, qqq ";

So what is this? OK, think about it first, cout it first, um, normal,

Change it to char * const ccpstr;

Ccpstr = "Hello, qqq ";

Cout again, then? Wrong, why? Hehe, because it becomes a "constant Pointer" pointing to an array of ordinary characters. Right, this pointer itself is a constant. if the address is saved, the address cannot be changed, however, the address is not a constant and the content can be changed. This is to say that it always points to an address and should be initialized during Declaration. I have never used it, but I personally feel quite dangerous.

OK, continue to add the above Code. Do not change it. ccpstr = "Hello, www"; Haha, this is definitely wrong. Let's look at the above Code.

 

{

In another case, I should understand this.

* Ccpstr = "Hello, eee ";

Cout: That's right. Why? Because the point is not a constant.

}

 

There is a section in Sun Xin's video that specifically mentions this issue. It feels very good. We strongly recommend it !! Sun Xin has his own official website

It looks like sunxin.org.

 

These are not easy to remember. I found it easier to read from the right to the left. This is probably related to the syntax habits of foreigners --

 

So there is another thing-# const char * const cccstr;

I don't want to talk much about Khan. I should study it myself, but it should be safe. Haha, I just don't know how to use it.

 

 

In addition, I think of a wild pointer problem. Although I have little experience in the past two months, I have encountered two problems. again, this is the same as the case in the <memory> Article I transferred. It is also about the variable pointing to the function.

For example

I have a global variable, char * cpstr;

Write a method first

Void test ()

{

Char A [] = "hello, RRR ";

Cpstr =;

Cout <cpstr;

}

 

Then, in the main function, call this method.

 

Int main ()

{

Test ();

If you want to use the cpstr code after adding it, it is very dangerous. What do you mean by cpstr? Which character is it?

Nothing! It is a wild pointer, but it still points to the address of the Temporary Variable A in the test method, but the memory where the variable is executed is recycled, that is, it is a memory area that does not know what to save, however, cpstr still points to it. It is a very scary wild pointer. After listening to others' experience, it will return to the back space after the pointer is used up, that is, cpstr = NULL. There are also many other techniques I will not talk about, I can't remember much. I still have a problem using it myself.

It is said that the stack space for method expansion is recycled, which I have not understood in depth.

Speaking of this, I suddenly came up with a question about the design. I don't know if it is better to transmit the data required by the method based on parameters as much as possible? Do you need to consider coupling in the design of the method? Complicated...> _ <

 

I don't know whether the pointer is a value reference or an address reference when it is used as a parameter. If I know that * pname operations should be an address reference, the value will change, what if I directly assign values to pname? Is it a wild pointer or a value reference? >_<

Do I assign values using local variables or constants?

... Thinking, thinking

}

 

 

I just tested that it is indeed a value reference, but the * pname (that is, the parameter) value in the method cannot be assigned, that is, it cannot be converted from const char [4] To char-I am a little confused myself.

At the same time, when should I use char [] and char *?> <alas

However, if it is a constant string, the essence should be char --

 

I tested it with the int type and used * pname to assign values in the method. It is indeed an address reference. In fact, it's not surprising to think about it.

 

God, I am stupid. char * pname = "XXXX"; then, if pname points to a constant, the constant cannot be changed.

That is, the actual implicit conversion should be const char * = "ffedfsfgf ";

 

Array is not a constant, it is a value, sin, Amen

 

But I still feel that the processing of string pointers is different from that of various basic types.

At least the cout string pointer will not come out of the address, but the string. Why? Let me know.

But I understand why the string type in C # is of the address reference type.

 

Well, the confusion center is probably a char * string.

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.