Pointer assignment problem "GO"

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/snowq/archive/2008/01/17/2050129.aspx

Recently wrote a small program, encountered a little problem, is about the pointer to a string. With this, Curiosity drove me to decide on the initialization and assignment rules for pointers. After a little gain, write down, for later use.

1, the initialization of the pointer

At first glance, the initialization and assignment of pointers seems to be confusing, again *, and &, and occasionally comes out an array. In fact, summed up is very simple:

int *p;

int a=25;

int b[10];

int *m=&a;

int *n=b;

int *r=&b[0];

The pointer is defined as shown above, and a variable preceded by a * represents the variable as a pointer variable.

When the pointer is initialized, the right operand of "=" must be the address of the in-memory data, cannot be a variable, and cannot be directly used with an integer address value (except for an int *p=0; The statement indicates that the pointer is empty). At this point, *p simply means that a pointer variable is defined, and there is no indirect value.

Int *s=15;

Int *s={2,3,5};

Int *s=a;

All three of these initialization methods are wrong.

2. Assignment of pointers

P=m;

p=&a;

P=b;

*p=25;

*p=a;

*P=B[4];

Pointer-related assignments, the left operand of "=" can be *p, or p.

When the left operand of "=" is *p, the data that the address of P points to is changed, and when the left operand of "=" is P, the address pointed to by P is changed.

The variable name B of the array represents the first address of the array, so the p=b is correct.

3. "Special Circumstances"

As mentioned earlier, the initialization of a pointer must use the variable address, not the variable directly.

So, how do you explain the following:

Char *cp= "ABCD";

In fact, this initialization process is to point the pointer CP to the first address of the string, not to pass the value of the string. Because, in the C language, there is no mechanism to process a string as a whole.

So, our headline "Special case" adds a quotation mark, because it is actually a pointer to the variable address initialization, "special case" is not special.

From this, how do you use a string to assign a value to a pointer? Only in the following way:

cp= "Mnop";

Type such as *cp= "MNOP"; Such a statement is wrong. The reason for this is that the string constant is passing its first address as described above.

In addition, this initialization process has another layer of implied meaning: "ABCD" is a string constant, in the initialization process does not occur the copy of the string, but simply pointer to the string constant, therefore, you can not modify the value of the string by *CP, because the string is a constant. Of course, we can use "cp=" to modify the string that the pointer points to, and the pointer itself is not a constant.

If you try to modify the string by using the pointer *CP, the answer is undefined, depending on the compiler. At least one thing is certain that in the compile phase, the compiler will not give an error because *CP is not a constant, so there is no syntax error in assigning a value to *CP. However, some compilers, such as VCs, throw exceptions at run time: An access violation occurs when writing to the location 0x00415768 (CP points to the address)!

This point with char ca[]= "ABCD"; is different, by ca[x] You can modify the data in the string.

4. Classic of C language

Finally, by the way, the classic C language good book. There are mainly the following:

"The C Programming Language" (C programming language), the father of the C language, a short span of more than 200 pages can be the C language of the various details of the description is clear, must be called classics. Compared to the domestic four hundred or five hundred pages, but do not know the cloud of C language programming, it is really different.

"C Expert Programming", the first edition of the book was introduced in 2002 by the People's post and telecommunications publishing house, then once out of print. The recent announcement of a reprint is a source of excitement. At the same time, this book is also I want to collect recently.

Pointer assignment problem "GO"

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.