String pointer and character array

zhaozj2021-02-16  61

Many people just don't understand from C , this code is in C.

Char * pchar = "Hello!"; / / Define the character pointer PCHAR, point to the first element of the number of characters, H

* pchar = 'h'; // problem

How can I not do it in C ? You are all over the reference book, you will say that PCHAR points to constants, how can I allow changing? You asked again, how do I run in C? No one answers you. So you had to comfort yourself, this is the C protection mechanism.

Let me have a summary, find this problem, if you don't study it, it is always a cloud, just like me. So, I compiled this code with BC 3.1 and run, whether it is a DOS program or a Windows program running, the result is also the same as you expect. no problem. But VC 6 will be wrong when running, error You want to be quite familiar (XX memory cannot be written). But is this just the problem of a compiler?

You can get the program you compiled in VC to run under pure DOS, even if he is a console program, he will also say "Can Not Run in DOS" because he is 32-bit. BC 3.1 even is a built-in Windows program, is just 16 bits. So the key to the problem is the management of memory. 32-bit systems define operational permissions for the memory used by the application, can you read or write, or what else. The 16-bit system lacks this security mechanism, even if you say this data can only be read, but there is no relationship.

The reason why this code can run in C is because the compiler of C is 16 bits; now in C , it is because the C compiler you use is 32-bit - energy Finding BC 3.1 This antique is really not easy, relatively TC2.0 is better to find. I have disassembled the two statement char * p = "hellow"; char p = "hellow"; discovery that their implementation is not the same, relatively, char * p = "hellow"; generate The code is more concise. Although c also wants to define a constant string, this idea is misunderstood due to 16-bit lack of protection; almost all people who have learned C have consumed their efforts to understand the code, and then use. Not, just because this code is efficient, it can be seen from the disassembly results. So use char p [] = "hellow"; this grammar is laughing, but in fact, this grammar is intentional, that kind of grammar is just drilling an empty space, so he is deprived in 32 programs The right to survive.

This reminds me of a lot of weird writing, although they make people feel very puzzled, but their efficiency is much higher than that is easy to understand, of course, I don't know if this is an example of drilling an empty space; from C The idea of ​​design, they have the meaning of existence, but from the current program design trend, the readability of the code exceeds his efficiency. I don't know how these syntax can be considered a foreign language in programmers - want to write an efficient program, master me, even if it is hard to die.

PS: 32-bit system Run 16-bit programs use analog simulation method, that is, the same as running in the 16-bit system.

转载请注明原文地址:https://www.9cbs.com/read-22998.html

New Post(0)