Introduction to C Programming--pointers (top)

Source: Internet
Author: User

Pointer, is the soul of C language, is also the C language and other high-level programming language different place, it can operate the memory flexibly, also can bring a lot of problems, need to use pointers safely.

A pointer variable is a variable that holds the address of a variable, which may be difficult to learn at first.


Pointer common Error 1:

# include <stdio.h>int main (void) {int * p;  Defines the int type variable pointer pint i = 3;*p = i; This statement is incorrect, *p is uninitialized, p holds an unknown address, points to an unknown variable, and if modified by I, it causes an error. printf ("%d\n", *p); return 0;}


Pointer Common error 2:

# include <stdio.h>int main (void) {long * p;double * q;int i = 3;double j = 5.5;long m = 5.5;//p = &j;//error. p = &m;//error. Q = &i;//error. p = &i;//error. Note: In pointer variables, it is not possible to assign values to each other, even if there is no loss of precision between the different types. printf ("%d\n", *p);p rintf ("%lf\n", *q); return 0;}

classic pointer program-swap two numbers:

/*2013 January 22 17:31:51 This procedure may indicate that the pointer causes the function to return several differences between value passing, pointer passing, and reference passing: value passing is the copy of the value of I, j to the Swap function, and pointer passing is the i,j of the swap function that points to the main function. Reference passing is the same number of i,j in the SWAP function as the main function, which can be considered as the alias of the I,j. This is not mentioned here, and will be understood later in the study. Although pointer passing is similar to a reference pass, you cannot confuse a pointer with a reference, that is, you cannot think of a pointer as a reference, a reference is a pointer. If you do not use a pointer, the output is: a = 5, b = 3//indicates that the value has been swapped inside the function. i = 3, j = 5//And the value inside the main function is not swapped. */# include <stdio.h>//This function is passed as a value and cannot complete the interchange requirement. void Swap_1 (int a, int b) {int t;t = A;a = B;b = t;printf ("A =%d, B =%d\n", A, b); return;} This function is passed by the pointer, but the internal writing error, the pointer variable (address) interchange, point to interchange, that is, the original pointer variable p pointing to A,q B, after interchange, p points to b,q point A. void swap_2 (int * p, int * q) {int * t;//The same type can be assigned to each other, so the variable t is defined as an int * type.  t = p;p = Q;q = t; That is, p, Q is a pointer to i,j variable, change the value of P, Q does not change the value of I,j, P,Q,I,J is four different variables. return;} This function is a pointer pass, which can accomplish the mutual requirements.  void Swap_3 (int * p, int * q) {int t;t = *p;*p = *q;*q = t; *p is the a,*q is B, here is the main function, a, a, and interchange. In other words: *p and A are the same variable, *q and B are the same variables. return;}  int main (void) {int i = 3;int j = 5;//swap_1 (i, j);//swap_2 (&i, &j); Swap_3 (&i, &j); The pointer is passed, and the argument is passed in the same form as the parameter type, that is, the delivery address, &i, &j. printf ("I =%d, j =%d\n", I, j); return 0;}


Pointer warm Up Program:

# include <stdio.h>int main (void) {int * p;  Define the Shaping pointer variable, p is the name of the pointer variable, and int * indicates that the P variable holds the address of the INT type variable. The     //p variable is an int * type, which is the type that holds the address of the INT variable. int i = 3;p = &i;/*1. P holds the address of I, so p points to I, and I can be found by P. 2. P is not i,i or p,p and I are two different variables. More prepared to say: Modify the value of P does not affect the value of I, modify the value of I does not affect the value of P. 3. If a pointer variable points to an ordinary variable, the * pointer variable is  exactly the same as the  normal variable example: if P is a pointer variable and p is stored in the address of the normal variable I, p points to the normal variable i*p is  exactly equal to I, *p = = I (* is the address The inverse of & is considered to be the normal variable that the pointer points to. Or, in all occurrences of the *p can be replaced by I (except for the definition of the declaration) in all occurrences of I can be replaced by *p*p to represent the contents of P as the address of the variable so, *p is I, modify the value of *p is to modify the value of I. */printf ("i =%d\n", i);p rintf ("i =%d\n", *p); *p = 99; That is, the value of I is modified. printf ("i =%d\n", i);p rintf ("i =%d\n", *p); return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Introduction to C Programming--pointers (top)

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.