Explanation of C language core array and pointer I

Source: Internet
Author: User

 Pointer:

I believe you are familiar with the following code:

Int
I = 2;

Int * P;

P = & I;

This is the simplest pointer application and the most basic usage. Next, let's get familiar with what is a pointer: first, a pointer is a variable, which saves not ordinary data, but the address of the variable. In the above Code, the pointer p Stores the address information of the integer variable I.

Next, let's take a look at how to define a pointer. Since a pointer is also a variable, its definition is also the same as that of other variables: int * P; * is an indirect addressing or indirect reference operator. In the above example, we also see a special operator &, which is an address-taking operator (in other cases, it is also a bitwise operator, & for the intersection operator ).

In the pointer definition above, we can see that the definition is an integer pointer. Do pointers still have types? The answer is yes, the pointer can only point to a specific type of object, that is, each pointer must point to a specific data type (the only exception: A pointer to the void type can store a pointer to any type, but it cannot indirectly reference itself .). For example, an int type pointer cannot point to a char type variable.

The following is a complete example to illustrate the simple application of pointers:

# Include <stdio. h>

Void main ()

{

Int
A, B, C, * P;

A = 1;

B = 3;

P = &;

B = * p + 1;

C = * (p + 1 );

Printf ("% d
% D/N ", a, B, c, * P + 3 );

 

}

Running result: 1 2-858993460 4

This is a complete example. You can debug it on your machine. Many people now use Microsoft's Visual Studio development environment, some people do not know how to write C Programs and debug C Programs in this development environment. For details, refer to the appendix.

In the above example, we can see two such expressions: B = * p + 1; and c = * (p + 1 ); the former means adding 1 to the content in the address referred to by P to B, which is equivalent to B = a + 1 ;, the latter is the address referred to by P plus 1 and then the address referred to by (p + 1) is assigned to C. Of course, we do not know what is put in the next address of P, therefore, a random value is output (this operation is very dangerous. Remember not to use an uncertain memory address ).

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.