C Language----------Pointers

Source: Internet
Author: User

Address

Note: Compare the pointer to the house number, the envelope mailing address, and now I see the pointer reminds me of the house number, envelope address

#include "stdio.h" int main () {    int a = ten;    int *p = &a;    printf ("%p\n", p);    return 0;  } Output
0x7fff5fbff81c

Analysis: is a specific location of the system RAM, usually in 16 binary digits, the system through this address, you can find the corresponding content, when using 80386, we must distinguish between the following three different addresses: logical address, linear address, physical address, in the C language pointer programming, Can read the pointer variable itself value (& operation), the value is actually the logical address , it is relative to your current process data segment address (offset address), not with the absolute physical address coherence, such as the above " 0x7fff8b6a378c "is the logical address. Instead of being sent directly to the memory bus, the logical address is sent to the Memory management Unit (MMU). The MMU consists of a chip or a group of chips, whose function is to map the logical address to a physical address, i.e. address translation. Here is the transformation diagram:

Pointer

First, the definition of the pointer variable

Pointer: The pointer to a variable is the address of the variable (the address is the pointer)

Pointer variable: A variable that holds the address of a variable, which is used to point to another variable

1. Format: Variable type * pointer variable name;

2. Example: int *p; Char *p2;

3. Note: When defining a variable, the * is only a symbol of the pointer variable

Second, using pointer variables to simply modify the values of other variables

1. Point to a variable

int A;

int *p;

p = &a;

Or

int *p = &a;

2. Modify the value of the pointing variable

*p = 10;

3. Modify the value of the outer variable inside the function

int a = 10;change (&a); void change (int *n) {    *n = 20;}

4. Pointers Use Note:

int main () {

/* Not recommended notation, int *p can only point to data int *p of type int ; Double d = 10.0; p = &d;*// * Pointer variable can only store address int *p; p = $; */ * Pointer variable is not initialized, do not take to indirectly access other storage space int *p; printf ("%d\n", *p); */ int a = ten; /* int A; A = ten; */ * int *p; p = &a; *///define the variable when * is only a symbol, no other special meaning int *p = &a; Incorrect wording //*p = &a; p = &a; This time the function of the *: Access to the variable p pointing to the storage space *p =; char c = ' A '; Char *CP = &c; *CP = ' D '; printf ("%c\n", c);
return 0;
}
Output
D

Practice Summary:

#include <stdio.h>void swap (int *v1, int *v2); int main () {/    *    int a = ten;    int b = one;        Swap (&a, &b);    */            int a2 =;    int b2 =;        Swap (&A2, &b2);        printf ("a2=%d, b2=%d\n", A2, B2);    return 0;} /* Cannot exchange the value of the outer argument, only the pointer to void swap (int *v1, int *v2) {    int *temp;    temp = v1;    V1 = v2;    v2 = temp;} *///to complete the interchange of two integer variable values (int *v1, int *v2) {    int temp = *V1;    *V1 = *v2;    *v2 = temp;} /* Swaps only internal v1, v2 values void swap (int v1, int v2) {    int temp = v1;    V1 = v2;    v2 = temp;} */

Pointer question:/*

%d int%f float\double%ld long%lld Long long%c char%s string%zd  unsigned long/#include <stdio.h>/* 0000 0001 0000 0010 0000 0000 0000 0000  0000 0000 0000 0000 0000 0010 0000 0001 */int Main () {    //0000 0000 0000 0000 0000 0 0000 0010    int i = 2;    0000 0001    Char c = 1;        char *p;    p = &c;        *p = ten;        printf ("Value of C is%d\n", *p);        return 0;} void Test () {    char C;//1    int A;//4    long B;//8        //Any pointer consumes 8 bytes of storage space    char *CP;    int *ap;    Long *bp;        printf ("Cp=%zd, Ap=%zd, bp=%zd\n",           sizeof (CP),           sizeof (AP),           sizeof (BP));
}

Third, pointers and arrays

1. When an array is passed in as a function parameter, it is automatically converted to a pointer

/* 1. How array elements are accessed int ages[5]; int *p; p = ages; 1> array name [subscript] ages[i] 2> pointer variable name [subscript] p[i] 3> * (P + i) 2. Pointer variable +1, exactly how much the address value is added, depending on the type of pointer int * 4 char * 1 DOUBLE * 8 *        /void change (int array[]); int main () {//20 bytes int Ages[5] = {10, 11, 19, 78, 67};        Change (ages); return 0;}    Using a pointer to receive an array, the pointer variable array points to the first element of the array void change (int *array) {printf ("%d\n", array[2]); printf ("%d\n", * (array+2));}        /*void change (int array[]) {int s = sizeof (array); printf ("%d\n", s);}    */void Test () {double d = 10.8;    Double *DP;        DP = &d;    printf ("DP =%p\n", DP);        printf ("DP + 1 =%p\n", DP + 1);        int ages[5] = {10, 9, 8, 67, 56};    int *p;    Pointer variable p points to the first element of the array p = &ages[0];        The array name is the address of the array and the address of the first element of the array//p = ages;      /* p---> &ages[0] p + 1---> &ages[1] p + 2---> &ages[2] p + I---> &ages[i]        *///printf ("%d\n", * (p+2));        printf ("%d\n", p[2]); /* for (int i =0; i<5;     i++) {printf ("ages[%d] =%d\n", I, * (p+i));    }*///printf ("%p\n", p);    printf ("%p\n", p + 1); printf ("%p\n", p + 2);}

Four. Pointers and strings

1. Constant area

Storing some constant strings

2. Heap

Object

3. Stack

Store local Variables

Master:

2 ways to define a string

1> using arrays

Char name[] = "Itcast";

* Features: Characters inside the string can be modified

* Application: The contents of the string need to be changed frequently

2> using pointers

Char *name = "Itcast";

* Features: string is actually a constant string, inside the character is cannot be modified

* Usage: The contents of the string do not need to be modified, and this string is often used

int main () {    char name[20];        printf ("Please enter name: \ n");        scanf ("%s", name);        ' J ' A ' a ' C ' K '        //printf ("%c\n", Name[3]);        printf ("The string just entered is:%s\n", name);        return 0;} Defines the string array void Test2 () {    char *name = "Jack";        int ages[5];        Pointer array (string array)    char *names[5] = {"Jack", "Rose", "Jake"};        Two-dimensional character array (string array)    char names2[2][10] = {"Jack", "Rose"};} Defines the string void Test () {    //string variable    char name[] = "it";    Name[0] = ' T ';        printf ("%s\n", name);            "it" = = ' I ' + ' t ' + ' + '    //pointer variable name2 points to the first character of the string        //string constant    char *name2 = "it";        Char *name3 = "it";        *name2 = ' T ';        printf ("%c\n", *name2);        printf ("%p\n%p\n", name2, Name3);        printf ("%s\n", name2);}

C Language----------Pointers

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.