5 void 分析

1 函数定义中 void 的意义

  • 如果一个函数定义没有显式地指明返回值和参数,那么默认的返回值类型是 int,参数可以是任意个

    #include <stdio.h>
    
    f()
    {
        printf("f()
    ");
    }
    
    int main()
    {
        int i = f();
        int j = f(1, 2);
        int k = f(1, 2, 3);
    
        return 0;
    }
    
    //输出结果
    f()
    f()
    f()
    
  • void 修饰函数的返回值和参数:void 修饰函数返回值和参数是为了表示“无”

    • 如果函数没有返回值,那么应该将其声明为 void

      • Demo

        #include <stdio.h>
        
        void f()
        {
            printf("f()
        ");
        }
        
        int main()
        {
            int i = f();
            int j = f(1, 2);
            int k = f(1, 2, 3);
        
            return 0;
        }
        
      • 编译

        test.c: In function 'main':
        test.c: error:10: void value not ignored as it ought to be
        test.c: error:11: void value not ignored as it ought to be
        test.c: error:12: void value not ignored as it ought to be
        
    • 如果函数没有参数,应该声明其参数为 void

      • Demo

        #include <stdio.h>
        
        void f(void)
        {
            printf("f()
        ");
        }
        
        int main()
        {
            f();
            f(1, 2);
            f(1, 2, 3);
        
            return 0;
        }
        
      • 编译

        test.c: In function 'main':
        test.c: error:10: too many arguments to function 'f'
        test.c: error:11: too many arguments to function 'f'
        test.c: error:12: too many arguments to function 'f'
        

2 void 指针的意义

  • C 语言规定只有相同类型的指针才可以相互赋值

  • void* 指针作为左值用于“接收”任意类型的指针

  • void* 指针作为右值使用时需要进行强制类型转换

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int* pI = (int*)malloc(sizeof(int));
        char* pC = (char*)malloc(sizeof(char));
        void* p = NULL;
        int* pni = NULL;
        char* pnc = NULL;
    
        p = pI;   // void*指针作为左值使用
        pni = p;  // error:void*指针作为右值使用需要进行强制类型转换
    
        p = pC;   // void*指针作为左值使用
        pnc = p;  // error:void*指针作为右值使用需要进行强制类型转换
    
        return 0;
    }
    
  • 通过 void* 实现 MemSet 函数

    • 函数作用:将一段内存中的每个字节都设置为一个固定的数

    • Demo

      #include <stdio.h>
      #include <stdlib.h>
      
      // void*:可以接受任意类型的指针
      void MemSet(void* src, int length, unsigned char n)
      {
          // 定义一个字节长度的指针
          unsigned char* p = (unsigned char*)src;
      
          int i = 0;
      
          for (i = 0; i < length; i++) {
              p[i] = n;
          }
      }
      
      int main()
      {
          int a[5];
          int i = 0;
      
          // 每个字节都设置为0
          MemSet(a, sizeof(a), 0);
      
          for (i = 0; i < 5; i++) {
              printf("%d
      ", a[i]);
          }
      
          
          int c[6];
          // 每个字节都设置为1
          MemSet(c, sizeof(c), 1);
      
          for (i = 0; i < 6; i++) {
              printf("%d
      ", c[i]);  // a[i] = (00000001 00000001 00000001 00000001)2 = (16843009)10
          }
      
          return 0;
      }
      
    • 编译运行

      0
      0
      0
      0
      16843009
      16843009
      16843009
      16843009
      16843009
      16843009
      
原文地址:https://www.cnblogs.com/bky-hbq/p/13582840.html