C language pointer initialization and assignment

Source: Internet
Author: User

1. pointer Initialization

At first glance, it seems that the initialization and assignment of pointers are very confusing, and *, and an array is generated from time to time. In fact, the summary is very simple:

Int * P;

Int A = 25;

Int B [10];

Int * m = &;

Int * n = B;

Int * r = & B [0];

The pointer definition is as shown above. A variable with the * Header represents this variable as a pointer variable.

During pointer initialization, the right operand of "=" must be the address of the data in the memory. It cannot be a variable or an integer address value (but int * p = 0; except, this statement indicates that the pointer is null ). In this case, * P only indicates that a pointer variable is defined and has no indirect value.

Int * s = 15;

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

Int * s =;

The above three initialization methods are all incorrect.


2. pointer assignment

P = m;

P = &;

P = B;

* P = 25;

* P =;

* P = B [4];

Pointer assignment. The left operand of "=" can be * P or P.

When the left operand of "=" is * P, it changes the data stored by the address pointed to by P. When the left operand of "=" is P, the address pointed to by P is changed.

The array variable name B indicates the first address of the array, so P = B; is also correct.


3. "Special Circumstances"

As mentioned above, the variable address must be used for pointer initialization, rather than directly using the variable.

So how can we explain the following:

Char * CP = "ABCD ";

In fact, this initialization process points the pointer CP to the first address of the string, rather than passing the string value. Because, in C language, there is no overall processing mechanism for a string.

Therefore, we add a quotation mark to the title "special case", because it is actually a pointer initialized with the variable address, and "special case" is not special.

As a result, how to assign values to pointers using strings? The following method is used:

CP = "mnop ";

Statements such as * CP = "mnop" are incorrect. The reason is that the String constant transmits its first address.

In addition, this initialization process has another implicit meaning: "ABCD" is a String constant, and no string replication occurs during the initialization process, instead, it simply points the pointer to the String constant. Therefore, you cannot use * CP to modify the value of the string because the string is a constant. Of course, we can use "cp =" to modify the string to which the Pointer Points. the pointer itself is not a constant.

If you try to modify the string through the * CP pointer, what will happen? The answer is undefined, depending on the compiler. At least one thing is certain. In the compilation phase, the compiler will not report an error because * CP is not a constant, so assigning a value to * CP has no syntax error. However, some compilers, such as Vc, will throw an exception at runtime: an access conflict occurs when the write location is 0x00415768 (the address pointed by CP!

This is different from charca [] = "ABCD". The data in the string can be modified through Ca [X.

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.