zoukankan      html  css  js  c++  java
  • 不懂指针就不要说自己学过C语言!

    不懂指针就不要说自己学过C语言!

    1、掌握了指针,就掌握了C语言的精髓!计算机中绝大部分数据都放到内存中的,不同的数据放到不同的内存区域中。

    内存角度没有数据类型,只有二进制;数据以字节(8位二进制)为单位存取。

    不同数据类型占据不同的字节,32位系统中:int 为4个字节,short为2个字节。

    2、int i = 5;

    printf("%d ",&i);

    int j=5;

    printf("%d ",&j);

    3、&i表示:获得变量i所指向内存的地址,地址也是数字。

    4、既然&是取地址,那么就可以用int类型存储指针地址:

    int iAddr = &i;

    short、long、char等的地址都可以用int表示。不过如果用int表示各种指针,那么就不知道内存中到底放的是什么类型(其实内存中也不知道是什么类型,都是一堆字节数据而已)。

    一般用“类型指针”:int *iPtr = &i; printf("%d ",iPtr);结果是一样的。

    5、使用*取指针指向的内存数据。 int j=*iPtr;

    6、还可以通过指针修改变量的值:

    int i = 5;

    int *iPtr = &i;

    *iPtr = 6;//区别就在于是否是声明,和放在=的左边还是右边没关系

    printf("%d ",i);

    7、总结:

    *的两个用途:

    1)声明的时候用来声明指针变量: int *iPtr;

    2)除了声明变量的时候,其他时候*用来表示获取指针指向的数据。

    &用来获取变量的地址。

    本节代码:

     1 #include <stdio.h>
     2 
     3 int main(int argc, char *argv[])
     4 {
     5     int i = 5;
     6     printf("%d
    ", &i);
     7     int j = 10;
     8     printf("%d
    ", &j);
     9     /*
    10     int iAddr = &i;
    11     printf("%d
    ",iAddr);
    12 
    13     char c1 = 'a';
    14     int iC1 = &c1;
    15     printf("%d
    ",iC1);
    16     */
    17     int* iPtr = &i;//Pointer=ptr。星号和类型在一起就是声明指针
    18     printf("%d
    ", iPtr);
    19     int i1 = *iPtr;//取iPtr指针指向的内存中的数据
    20     //类型化的指针,知道是从内存中取几个字节
    21     //*放到指针变量前面代表“取指针地址指向的数据”
    22 
    23     printf("i1=%d
    ", i1);
    24 
    25     *iPtr = 100;//把100存到iPtr指向的内存中
    26     printf("i=%d
    ", i);
    27     /*
    28     char c1 ='a';
    29     char *c1p = &c1;
    30     char c2 = *c1p;
    31     */
    32     return 0;
    33 }
  • 相关阅读:
    Codeforces Round 546 (Div. 2)
    Codeforces Round 545 (Div. 2)
    Codeforces Round 544(Div. 3)
    牛客小白月赛12
    Codeforces Round 261(Div. 2)
    Codeforces Round 260(Div. 2)
    Codeforces Round 259(Div. 2)
    Codeforces Round 258(Div. 2)
    Codeforces Round 257 (Div. 2)
    《A First Course in Probability》-chaper5-连续型随机变量-随机变量函数的分布
  • 原文地址:https://www.cnblogs.com/zhubinglong/p/5982947.html
Copyright © 2011-2022 走看看