C pointer programming path-first note

Source: Internet
Author: User
// ============================= C pointer programming path ==================== ================== //
/// // The first note ////////////////


* Indicates that the variable is a pointer variable, which is also a feature of the pointer variable.
Int * pstu; // defines the pointer variable pstu, And the pstu points to the int type variable.
Static int * pstu; // defines the pointer variable pstu, And the pstu points to the static integer variable.
Char * pstu; // pstu is a pointer variable.


Accessors and value operators *
Get the address of a variable through &
You can use * to obtain the data stored in a variable.


Char stuname, name; // defines the stuname, name
Char * pstu // defines the pointer variable pstu pointing to the character type variable
Char * pnew // defines the pointer variable pnew to the variable type.
Pstu = & stuname // get the address of the stuname variable and assign it to the pointer variable pstu
Pnew = pstu // assign the pointer variable pstu value to pnew
Name = * pnew // assign the value of the memory unit pointed to by pnew to name


Int num_temp; // defines the variable num_temp.
Int * p_num; // defines the pointer variable.
P_num = & num_temp; // point p_num to num_temp
* P_num = 5; // assign a value to the variable pointed to by p_num






Sample Code:
# Include <stdio. h>
Int main ()
{
Int num;
Int * p_num;
P_num = & num;
* P_num = 5;
Printf ("% d \ n", num );
Return 0;
}


The output value is 5;
It indicates that the value is assigned to the num pointed to by p_num;


A normal variable must be defined in use first, and a pointer variable must be defined first, and a value must be assigned before use.
Assign values to the corresponding values (initialization ).
Example:
Int X, * P;
Float y;
P = & X; // correct
P = & Y; // Error




Subtraction of integers in the pointer field:
Here the address is changed.
Example:
# Include <stdio. h>
Int main ()
{
Int num [4] = {12, 13, 14, 15 };
Int * p_new, * p_old;
P_new = num;
Printf ("% d \ n", p_new );
P_new ++;
Printf ("% d \ n", p_new );
P_new = p_new + 2;
Printf ("% d \ n", p_new );
Return 0;
}










Addition and subtraction of pointers and pointers:
Of course, the premise is that there are two pointers, the main operation in the array, the difference between the two elements is the distance, or can be said to evaluate the number of elements between the two pointers.

C pointer programming path-first note

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.