Dark Horse Programmer -08-Pointer

Source: Internet
Author: User

First, the pointer Prelude 1. The importance of pointers

Pointers are a very important type of data in C, and if you say that you have a good study in c except for pointers, you simply say you have not learned C language.

2. Small needs

l Voidchange (int n) after the function call is complete, change the value of the argument

L Analysis: Change the value of the argument, find storage space, address

Second, the pointer variable definition 1. The format of the definition

L class name identifier * pointer variable name;

L int*p;

2. Define the Post assignment first

L Simple Value

int a = 10;

int *p;

p = &a;

3. Assigning values at the same time as defined

int a = 10;

int *p = &a;

4. Attention Points

The pointer variable is used to hold the address of the variable, do not give it arbitrary assignment of a constant. The following wording is wrong.

l int *p;

l p = 200; This is wrong.

L%p The address value stored in the output pointer

L Other pointer type description, such as float *p; Char *p;

L can not use the type, such as int a = 10; Float *p =&a;

5. Clear the pointer

L p = 0;

L P =null;

Third, pointer instances

L Assign values to the variables pointed to by the pointer

1 char a = 10;

2 printf ("Before modification, value of a:%d\n", a);

3

4//pointer variable p points to variable a

5 Char *p = &a;

6

7//Modify the value of variable a indirectly by pointer variable p

8 *p = 9;

9

printf ("Modified, value of a:%d", a);

A value is the 10,P value is the address of variable a FFC3.

Notice that the 5th and 8th lines have a "*" and their meanings are different:

(1) the "*" in line 5th is used to indicate that p is a pointer variable

(2) The 8th line of "*" is a pointer operator, where the *p represents a P-value ffc3 this address to access the corresponding storage space, that is, the storage space of variable A, and then the right value 9 is written to this storage space, equivalent to a = 9;

The output is:, it can be found that we indirectly modified the value of variable A through the variable p.

l Example!!! This is the case that Miss MJ gave me a very deep impression of!!!!!!!!!!!!!!!!!!!

Iv. the research of pointers

1. A pointer variable consumes 8 bytes of storage space

V. Pointers and Arrays 1. Pointer to a one-dimensional array element

1//define an array of type int 2 int a[2]; 3 4//Define a pointer of type int 5 int *p; 6 7//Let the pointer point to the NO. 0 element of the array 8 p = &a[0]; 9 10//Modify the value of the pointed element one by one *p = 10;12 13//Print the value of the first element in printf ("a[0] =%d", a[0]);

2. Traverse one-dimensional array elements with the pointer

1//define an array of type int 2 int a[4] = {1, 2, 3, 4}; 3 4//Defines a pointer of type int and points to the No. 0 element of the array 5 int *p = A; 6 7 int i; 8for (i = 0; i < 4; i++) {9//Take the pointer operator * To remove the value of the array element int value = * (p+i); printf ("a[%d] =%d \ n", I, value); 13}

P is a pointer, a is an array

Vi. Pointers and strings

Char *s;

s = "MJ";

The point above is also correct: Define the pointer variable first, and then point to the string. If a character array is not allowed to do this, the following is an error:

1 Char s[10];

2 s = "MJ";

The compiler is sure to report the error of line 2nd, because S is a constant, representing the first address of the array, and cannot be assigned a value operation.

It is also important to note that the following practices are also incorrect:

1 char *s = "MJ";

2

3 *s = "like";

The 3rd line of code made 2 errors:

· The 3rd line of code is equivalent to the string "like" into the memory space of S point, as the 1th line of code can be seen, S points to "MJ" the first character ' M ', that is, a piece of char type S point of storage space, only 1 bytes, to "like" in 1 bytes of space, Positive memory Overflow

· As you can see from the 1th line of code, the pointer s is pointing to the string constant "MJ"! So it is no longer possible to modify the string contents by pointers! Even *s = ' A ' seems to be the wrong way of writing, because s points to a constant string that does not allow the modification of characters inside it.

Now want to change the string "LMJ" the first character ' l ' to ' l ', the solution is a variety of

1 char *p2 = "LMJ";

2 *p2 = ' L ';

3

4 printf ("%s", p2);

It seems to be possible, but this is the error code, which is wrong on line 2nd. First look at line 1th, pointer variable p2 point to a string constant, because it is a constant, so its internal characters are not allowed to modify.

Someone might have been blindfolded, here's the 1th line of code char *P2 = "LMJ"; the 2nd line of code in the first scenario char a[] = "LMJ"; not the same? It's not the same.

· Char a[] = "LMJ"; define a String variable!

· Char *p2 = "LMJ"; defines a string constant!

Dark Horse Programmer -08-Pointer

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.