C Language Pointer Learning (reprint)

Source: Internet
Author: User
Tags value of pi

Http://blog.chinaunix.net/uid-22889411-id-59688.html

Pointer learning in C language:

Introductory Introduction:

int i=30;//defines an int variable i (two bytes of memory, different systems may not be the same, the two bytes of memory is named I, 30 is converted into binary into the two memory)

int *pi;//defines a pointer variable (unlike defining a normal variable, the difference is in what it deposits, and the pointer's content is an address that points to an int variable.) In a four -bit machine, it is 4 bytes in length)

pi=&i;//the address of I into PI, assuming I's address is 0x89770708

printf ("%d", *pi);//Print out the contents of the int that the content points to in pi. The content in pi is 0x89770708, which points to 30, so print out 30. That is to say *pi is i.

Exercises:

Char A,*pa

a=10

Pa=&a

*pa=20//can use *PI to change the value of a variable

printf ("%d", a)//output should still be 20

Pointers and Arrays:

~~int i,a[]={1,2,3,4,5};

for (i=0;i<=5;i++) {printf ("%d", A[i]);}

~ ~ int i,a[]={1,2,3,4,5};

for (i=0;i<=5;i++) {printf ("%d", * (A+i));}

~ ~ int i,*pa,a[]={1,2,3,4,5};

for (i=0;i<=5;i++) {printf ("%d", Pa[i]);}

~ ~ int i,*pa,a[]={1,2,3,4,5};

for (i=0;i<=5;i++) {printf ("%d", * (Pa+i));} Does it mean that pointers and array names are not bad?

~~int i,*pa,a[]={1,2,3,4,5};

Pa=a;

for (i=0;i<5;i++) {printf ("%d", *pa);p a++}//Here, the pointer value is modified. If you change to a++, a compile error occurs.

The pointer to a face is actually a pointer variable, and the array name is a pointer constant whose value cannot be modified.

Is there a pointer constant? Some!

int *const pa=a;//Here defines a pointer constant, which points to the data of type int, and assigns a to the PA, equivalent to:

int *const pa;pa=a;//because the array name a represents the address of this array, there is no need to use &.

Note that the format of the pointer constant defined here is not an int const *PA;

A const is a keyword that declares a constant. The int const *PA, consistent with the const int *PA meaning. This means that the order of const and int doesn't matter.

contrast int const *PA with int *const PA :

int const *PI:

Code Start *******************

int i1=30;
int i2=40;
The const int * pi=&i1;//is equivalent to the int const *pi=&i1;
pi=&i2; 4. Note Here, Pi can re-assign a new memory address at any time
i2=80; 5. Think about it: Can you use *pi=80 here instead? Of course not.
printf ("%d", *PI); 6. Output is 80

Code End *********************

The const int * pi=&i1;//equivalent to the int const *pi=&i1;const modifier is *PI, so the value of *PI cannot be changed. The pi itself is a variable (there is no const modifier in front of pi), so pi can be changed, and the int value pointed by Pi can also be changed. (The key is to see What parts of the const modifier are modified, and the parts that are modified by the const are not allowed to be changed.) )

int *CONSTPA :

Code Start ***************
int i1=30;
int i2=40;
int * Const pi=&i1;
pi=&i2; 4. Note Here, Pi can no longer be re-assigned so that it can no longer point to another new address.
The PI has a const modifier in front of it. So I've commented on it.
i1=80; 5. Think about it: Can you use *pi=80 here instead? Yes, the value of I1 can be modified here by *PI.
Please compare yourself with the previous example.
printf ("% d", *pi); 6. Output is 80
Code End *********************

The PI value cannot be modified. It always points to the memory address at the time of initialization. However, the value of I can be modified by *PI.

Three things to add:

begin*****************

const int I1=40;

int *pi;

pi=&i1; Is that OK? No, VC is a compilation error.

The address of a const int type i1 is not assignable to a pointer to an int type address. Otherwise pi will not be able to modify the value of I1!

Pi= (int*) &i1; Is that OK? Coercion of type conversions is supported by C.
VC compiles through, but still cannot modify the value of I1 through *pi=80. end***************

begin****************
const int I1=40;
const int * PI;
pi=&i1;//two types are the same, you can assign this value. It is clear that the value of I1 cannot be modified either through PI or i1.
end*****************

begin****************
int I
const INT * Const PI=&I;//Can you imagine what pi can do? The pi value cannot be changed, nor can the value of I be modified by PI. Because both the *PI and the pi are const.
end****************

C Explanation of function parameter passing in language:

Value passing, address passing, reference passing

*************************************

void Exchg1 (int x, int y)
{

X=a;y=b, which is implied by the function call.
int tmp;
Tmp=x;
X=y;
y=tmp;
printf ("x=%d,y=%d\n", X, y)
}
void Main ()
{
int a=4,b=6;
EXCHG1 (A, b);
printf ("a=%d,b=%d\n", A, B)
}

*************************************

The above is the value passing procedure. We found that the operation for X, Y does not have any effect on the value of A and B. It is also equivalent to:

int x; int a=5; X=a; X=x+4, get x=9, and a=5; just assign the value of a to the x,x do the operation, for a the value of no change!!

*************************************

EXCHG2 (int *px, int *py)
{
int tmp=*px;
*px=*py;
*py=tmp;
Print ("*px=%d,*py=%d\n", *px,*py);
}
Main ()
{
int a=4;
int b=6;
EXCHG2 (&A,&B);
Print ("a=%d,b=%d\n", A, b);
}

***************************************

*************************************

EXCHG2 (int&x, int &y)
{
int tmp=x;
X=y;
y=tmp;
Print ("x=%d,y=%d\n", X, y);
}
Main ()
{
int a=4;
int b=6;
Exchg2 (A, b);
Print ("a=%d,b=%d\n", A, b);
}

A supplement to pointers:

int A;

int B=exchange (&a,9);

The exchange functions are as follows:

Exchange (int *xp,int y) {

INTX=*XP;

*xp=y;

RETURNX;

}

Pointer to pointers:

short int i=50;

short int *pi;

pi=&i;

short int **ppi;

ppi=&pi;

Get:

What is the PPI? Address of Pi

What is *ppi? The value of pi (that is, the address of I)

What is **ppi? The value of I (i.e. *PI)

PS: There is also a pointer to the function to view the HTML document

C Language Pointer Learning (reprint)

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.