mikebai.com

  • Home
  • dev
  • DotNET
  • M365
  • 搞笑
  • 杂七杂八
  • FocusDict
個人BLOG
it developer
  1. Main page
  2. dev
  3. Main content

C++ - void 与 void*

2016-04-20 106hotness 0likes 0comments

一、 Void 关键字的使用规则:

1. 如果函数没有返回值,那么应声明为 void 类型;
2. 如果函数无参数,那么应声明其参数为 void;
3. 如果函数的参数可以是任意类型指针,那么应声明其参数为 void * ;
4. void 不能代表一个真实的变量;
  • void 体现了一种抽象,这个世界上的变量都是“有类型”的

二、含义

  • void 的字面意思是“无类型“,void 则为 ”无类型指针“,void 可以指向任何类型
  • void 真正发挥的作用在于:
    • 对函数返回的限定
    • 对函数参数的限定
  • 例如:
    float *p1;
    int *p2;
    p1 = p2
    #warning 这样会报错:cannot convert from“int*”to “float*”,必须改为p1 = (float *)p2;
    void *p1;
    int *p2;
    p1 = p2;
  • 在 C 语言中,凡是不加返回值类型限定的函数,就会被编译器作为返回整形值处理,例如:
    add(int a,int b){
      return a+b;
    }
Tag: Nothing
Last updated:2016-04-20

mikebai

This person is a lazy dog and has left nothing

Like
< Last article
Next article >

COPYRIGHT © 2025 mikebai.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang