• c 各种例子


    1.

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <string.h>
    4. #include <unistd.h>
    5. #include <arpa/inet.h>
    6. #include <sys/socket.h>
    7. #include <signal.h>
    8. #include <sys/prctl.h>
    9. #include <sys/types.h>
    10. int main(void){
    11. char a[5]={'1','2','3','4','5'};
    12. char *g=a;
    13. char (*d)[5]=&a; //一维数组指针的指针, 注意:为指针数组赋值只需要赋一个首地址就行了,但指针必须指定数组范围大小
    14. printf("%c\n",*(g+3)); //4 g为指针
    15. printf("%c\n",*(*d+1)); //2 *d 为指针,单独的d不是指针
    16. char c[2][3][5]={ //5个char ,3排×5个char ,2组×3排×5个char 连乘的感觉 char c[a][b][c] a*b*c
    17. {
    18. {'a','b','c','d','e'},
    19. {'1','2','3','4','5'},
    20. {'q','w','e','r','t'}
    21. },
    22. {
    23. {'8','0','7','6','9'},
    24. {'p','o','l','m','n'}
    25. }
    26. };
    27. char (*w)[3][5]=c; //可以看成w 是c[a][b][c] 中 [a] 组
    28. printf("%c\n",***(w+1)); //82
    29. printf("%c\n",**(*(w+1)+1)); //u
    30. printf("%c\n",*(*(*(w+1)+1)+1)); //h 第2组中第2排的第2个元素
    31. char (*p)[2][3][5]=&c; //三维数组指针的指针
    32. printf("%c\n",**(*(*p+1)+1)); //p 查询顺序同上,p代表为组,组->横->
    33. printf("%c\n",*(***p+3)); //d
    34. return 0;
    35. }

         struct{

                  int code;

                   float cost;

        }item,*ptrst;

       ptrst=&item;

       prtst->code=3451

    //   ptrst->code==item.code==(*ptrst).code

    特别要学这种名字和指针一起定义的方法

    如:

          struct   sockaddr_in    addr,   *p;    //一起定义名字与指针

          p=&addr;                      //赋值

          p->sin_family=AF_INET;

    结构与union  的运算符相同,不同的是union 在同一时间内只能存储成员中的一种,其他的成员不真实。

    union  的作用:还没有确切得到数据前,把有可以的多种数据类型都作好存储预留,得到数句后再存入。而且存储只能多选一。

    2.  c的修饰符声明

             *       表示一个指针

            ( )  表示一个函数

              [ ]      表示一个数组

             可以使用多个修饰符,

             int       t[8][8]         //数组的数组

             int      **prt            //指向int 的指针的指针

             int      *prt[10]       //具有10元素的数组,每个元素是一个指向int的指针

             int     (*prt)[10]     //一个指针,指向具有10个元素的int数组

             int      *oof[3][4]      //一个3×4的数组,每个元素是一个指向int的指针

            int      (*oof)[3][4]    //一个指针,指向3×4的int数组

            int       (*  uof[3])[4]   //一个具有3个元素的数组,每个元素是一个指向具有4个元素的int 数组的指针

    从实际出发,其实只要理解了char *p ,char *p[ ], char  (*p)[3],  其他如int  *p[ ] ,是不被允许的

                 char  *p="hello"     允许

                 int   *p={1,2,3}       错误  ,除了char 类,任何类型的数组都不能用指针形式初始化数组。

                 char  *p[3]={"123","qqwe","asdsdsssf"}    允许

                 char  (*p)[3]={"123","11","qwe"}     错误,不能这样初始化二维数组,应先定义为

                                char  p[3][3]={"123","11","qwe"};

                          再赋指针: char  (*w)[3]=p;

    3.

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <unistd.h>
    4. #include <arpa/inet.h>
    5. #include <sys/socket.h>
    6. #include <string.h>
    7. #include <signal.h>
    8. void handler(int signum){
    9. printf("hello nanning",signum);
    10. //exit(0); //加上这句,程序退出,如不加,则只执行打印语句
    11. }
    12. int main(void){
    13. signal(2,handler); //SIGINT
    14. while(1);
    15. return 0;
    16. }
    17. // 键盘中断产生信号:1,ctrl+c 2,ctrl+\ 3,ctrl+z,键盘只有这三种情况产生signal信号
    18. //这三中情况只能对当前(运行中)的进程有效
    19. //如想利用键盘中断产生信号,随时打开帮助文件是行不通的。因为驻留运行的进程必须是当前进程。除非驻留文件和vi同时都在一个//当前运行进程中。

    4. 函数返回值是数组的情况:此返回值必须是static. 或者是全局数组或者是由main主函数传入的数组指针,再返回此指针,或者是存储在malloc() 指针的内存的字符串。          

    char *str(void);
    int main(int argc,char **argv){

                printf("%s\n",str());   //hello
                return 0;
    }
    char *str(void){

             char *c="hello";
             char  *p=malloc(sizeof(c)+1); //只是分配内存空间,还没有把c 存储到p指针的内存中
             strcpy(p,c);                //把c 转存到p指向的内存
             return p;              //返回p指向的内存指

              free(p);            //释放内存
    }

    5. 字符串拼接,像java相加

        int main(int argc,char **argv){
                 //c="hello"+argv[1]+"nanning+"wz";
                  char c[1000]={};    //数组定义后,一定要赋值
                  printf("%s\n",strcat(strcat(strcat(strcat(c,"hello"),argv[1]),"nanning"),"wz"));
                  return 0;
          }

    或者:

       char in[100]={};
        for(int t=0;t            strcat(in,argv[t]);
               strcat(in," ");             //如要拼接,不加这句
        }
        puts(in);

    6.c  中enum  的一个作用,为一堆离散代号编写整数序号。

                     enum school{wz,wh=100,wl,wq};         //默认:wz=0,wh=100,wl=101,wq=102
                     enum school bj;
                     int t=(bj=wq);
                     printf("%d\n",t);  //102      

    或者:  enum {wz,wh=100,wl,wq};//默认:wz=0,wh=100,wl=101,wq=102
                 printf("%d\n",wl);   //101 

    集合switch.     case  语句可以根据整数序号反查字母代号,话说这方法太复杂。

                         

  • 相关阅读:
    【元宇宙欧米说】从个人创作者的角度聊聊NFT
    大数据知识合集之预处理方法
    中国国债发行数据集(2002-2023)
    已解决Python配置环境变量失效问题
    深入理解计算机网络-9IP地址和子网2
    (六)算法基础——动态规划
    Android Splash实现
    gdb调试程序教程
    MongoDB---管理,备份恢复,应用场景
    Pytorch中 nn.Transformer的使用详解与Transformer的黑盒讲解
  • 原文地址:https://blog.csdn.net/m0_59802969/article/details/133198240