Data Structure Notes 1 (c ++) _ Pointer

I. Overview of Data Structure

       1. Definitions:

    How do we put a lot of real and complex problem, save to specific data types and specific storage structure into the main memory (RAM), and on this basis to achieve a certain function (such as finding an element, remove a elements, sort all of the elements) and the corresponding operations performed. The corresponding operation is also called algorithms.

         + Data Structures = relation individual subject

         Algorithm (narrow) = stored data operation

  Algorithm 2: solving methods and procedures

          2.1 algorithm to measure the quality criteria:

      : 2.1.1 time complexity. Program about time the number of executions, not executed

      2.1.2 space complexity: during the execution of the algorithm is probably the biggest memory occupied

      2.1.3. The degree of difficulty (ed algorithm most important)

      2.1.4. Robustness

       3. Status of data structures: data structure is in the core curriculum software

         Operation language storing program data = data + + can be executed by a computer

 Second, prior knowledge

  1. Pointer

    1.1 The importance of pointers: Pointer is the soul of c language

    1.2 For the definition of the concept of pointers:

      Address is the address pointer is a pointer, pointers, and the address is a concept

      Pointer variable is a variable to store the memory address of the cell, a so-called memory cell is the memory address unit number, a concept both

      The nature of the pointer is a non-negative integer limited operation / non-negative integer from zero          

        Range: 0 - FFFFFFFF [4G-1]

        1.2.1 The relationship between memory and cpu:

          CPU can directly access only memory, the memory can be accessed by the CPU unique mass storage, the basic unit of memory is divided byte, each byte has eight bits, each one storing a 0 or 1. 1, CPU and dealing with memory: address, control lines, data lines

          Memory can be seen a lot of the small lattice, if it is 32 bits, numbered from 0 to 4G-1;

          . 1.2.1.1 address lines can be determined which of the operating unit number, not the address number, the address is numbered; focus on an address line, because the address lines are 32 bits, so the maximum only 0 to 32-1; number memory It can not be repeated, but the content can be repeated;

            Definition: The address is the number of memory cells, cell number is dead, can not be changed, but the contents inside can be changed; when a program finishes running, the memory will be recycled (recycling and destruction is not a concept, the operating system will allocate memory to the program, the program tells operating system, the operating system will memory space is released, the original data (digital legacy waste) does not destroy, but can not use it (this problem c, but not java, java will automatic release), where the variables will be cleared, but the memory cell number still exists;

          . 1.2.1.2 control lines are used to determine: read, write, read-only, write-only;

          1.2.1.3. Data line for data transmission

  2. pointers classification:

    2.1 Basic types of indicators

      2.1.1 Basic Concepts

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int * p;    //该p变量只能存储int类型的地址,不能存放一个整数
 6     int i = 10;
 7     int j;
 8 
 9     // char ch = 'A';
10     // p = &ch;    //p只能存放int类型,不能存放char类型的'A',类型不一致,会报错
11     // p = 10; //error p是个变量名字,表示只能存放int型变量地址,10是个整数,不是个地址;
12     // *p = i;        //可以存放i,一定要如下理解才可以
13                 // 1)将i的地址发送给p,意味着p是指向i的;
14                 // 2)修改p、i的值,不会影响另外一个值,相互不会影响;
15                 // 3)*p即为i变量本身,i跟*p可以在任何地方进行互换,i的值改了,*p的值也改了,i原来等于100,*p也就等于100,但是p不是i,i不是p;
16     p = &i;     // 如果这一行被省略掉,会报错;
17                 // 第一步int * p;只是说p可以保存整型变量地址了,但p中并没有保存真正的整型变量地址,所以我们不知道*p真正指向的是谁了
18                 // 虽然p中没有保存真正有效整型数字地址,但是p中还是可以有垃圾数字的,垃圾数字也有可能是某一个变量的地址,所以*p最终指向的是一个不确定的单元,*p不知道指向了哪里,造成了混乱,c语言中不允许这样去写
19                 // 不能将一个不确定单元的值,赋给另外一个变量,这样不合适,所以会报错
20     *p = i;        // 等价于i=i,不会出错,但是没有什么实际意义
21     j = *p;    // 等价于j = i; 若注释掉这一行,则j没有赋值,c会自动赋予一个垃圾数字-2341343;
22     printf("i =  %d, j= %d, *p = %d\n",i, j, *p);
23 
24     //printf();
25     return 0;
26 }
View Code

 

小结:

    1、如果一个指针变量(假定为p)存放了某个普通变量,那我们就可以说:“p指向了i”,但p与i是两个变量;修改p的值不影响i的值,修改i的值不影响p的值

    2、*p等价于I 或者说*p可以与i在任何地方互换

    3、如果一个指针变量指向了某个普通变量,则*指针变量就完全等价于该普通变量

注意:

       指针变量也是变量,只不过它存放的不能是内存单元的内容,只能存放内存单元的地址

       普通变量前不能加*

       常量和表达式前不能加&

如何通过被调函数,修改主调函数中普通变量的值

1.实参为相关变量的地址:&i

2.形参为以该变量的类型为类型的指针变量:*p

3.在被调用函数中通过*形参变量名的方式修改主函数:*p=100


    2.2.指针和数组的关系(一维数组)

//Array_point_1.cpp
# include <stdio.h>

int main(void)
{
    int a[5] = {1,2,3,4,5};    // 1)a中存放的不是1~5这5个数字,这5个数字是在a0到a4中存放的。
                            // 2)数组名a存放的是数组的第一个元素的地址
                            // 3)它的值不能被改变
                            // 4)字母a即为一维数组名,指向的是数组的第一个元素,即a指向的是a0
                            // 5)a[3]和*(a+3)的关系:a[i] <<==>> *(a+i) 即数组a[i]的写法,等价于*(a+i)    a[3] == *(3+a), 3[a] == *(a+3), 因a指向第一个元素a[0],故a+3指向第四个元素a[3],则*(a+3)==a[3];
                            // 理论上指针比下标的速度快,但是可以忽略不计
    //a[3] == *(3+a);
    
    printf("%p\n", a+1);
    printf("%p\n", a+2);
    printf("%p\n", a+3);

//[Out]:
//0019FF30
//0019FF34
//0019FF38
    
    printf("%d\n", *a+3);  // *a+3等价于 a[0]+3 = 4
//[Out]:
// 4
    return 0;
}
View Code

      2.2.1.数组名:a[*]中的a

        一维数组名 是个指针常量,它存放的是一维数组第一个元素的地址a[0],它的值不能被改变

        一维数组名指向的是 数组的第一个元素

      2.2.2.下标和指针的关系:a[i]等价于*(a+i)

        a[i] <<==>> *(a+i)

        假设指针变量的名字为p,则p+i的值是p+i*(p所指向的变量所占的字节数)

      2.2.3.指针变量的运算:

        指针变量不能相加,不能相乘, 不能相除,如果两指针变量属于同一数组,则可以相减

        指针变量可以加减一整数,前提是最终结果不能超过指针

          p+i 的值是p+i*(p所指向的变量所占的字节数)  

          p- i 的值是p-i*(p所指向的变量所占的字节数)

          p++ <==> p+1  // 如果是int型,就是4字节,double型,就是8字节,但是都是指向的后一个元素

          p-- <==> p-1

      2.2.4.举例:

        如何通过被调函数修改主调函数中一维数组的内容[如何界定]

          两个参数:1)存放数组首元素的指针变量;2)存放数组元素长度的整型变量

//Array_point_2.cpp
# include <stdio.h>

void Show_Array(int * p, int len)    // a发送给了*p
{
//    p[0] = -1;    //p[0] == *p
//    p[2] = -1;    //p[2] == *(p+2) == *(a+2)  p[i]就是主函数的a[i]
    int i = 0;

    for (i=0; i<len; ++i)
        printf("%d\n", p[i]);
}

int main(void)
{
    int a[5] = {1,2,3,4,5};

    Show_Array(a, 5);    // a等价于&a[0], &a[0]本身就是i
                        // 通过写一个数组名,数组长度,就可以确定一个数组, 
                        // 要想通过一个函数,访问另一个函数中的一个数组,只需要知道这个数组的首地址(数组第一个元素地址)和长度,
                        // 在另一个函数中就可以任意访问、修改主函数a[5]的值
//[Out]:1 2 3 4 5    

//    printf("%d\n", a[2]);
//[Out]: -1
//    printf("%d\n", a[0]);
//[Out]: -1
    
    return 0;

}
View Code

 

Guess you like

Origin www.cnblogs.com/wangxue533/p/11745395.html