void和void* 的用法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yun_xy/article/details/503937

#include <stdio.h>
#include <stdlib.h>

int f()
{
    return 0;
}
int main(int argc, char *argv[])
{
  f(10);    //如果函数为int f(void),则报错
  int *pi;
  void *pv;
  pi=pv;
  pv=pi;
  //*pv=8;   error
 // pv[0]=4;  error
  int *p =(int*)malloc(100*sizeof(int));
  free(p);
  system("PAUSE"); 
  return 0;
}
在C编译器下,C对f()不会做参数检查,所以主函数中写f(10),编译器不报错,但如果写成

int f(void)

{

  return 0;

}

这时主函数中f(10),就要报错,因为void表明告诉编译器,这里没有参数。

注意:C++编译器中默认都是没有参数,不管你写int f()还是int f(void),主函数中f(10)都要报错

void* 表指向的对象类型不确定。void * 可以和任何指针直接做变换,除了函数指针外。

如:

      int *pi;
       void *pv;
      pi=pv;   //注意在C++编译器中必须转换  pi=(int*)pv;
      pv=pi;

由于void * 可以和任何指针直接做变换,除了函数指针外,在C编译器下,我们知道malloc函数的返回类型是void*,所以下2句是等价的

  int *p =(int*)malloc(100*sizeof(int));

int *p =malloc(100*sizeof(int))

但是void* 不能做取值和小表操作

 //*pv=8;   error
 // pv[0]=4;  error

展开阅读全文

void *和void**

05-15

[code=C/C++]rn#includern#includern#includernrn#define DEBUGrn#define BOOL intrn#define MAX_STACK_SIZE 100rn#define TRUE 1rn#define FALSE 0rnrntypedef structrnrn void *value;rnsnode_t;rnrntypedef structrnrn snode_t base[MAX_STACK_SIZE];rn int top;rnstack_t;rnrnBOOL InitStack(void **pstack)rnrn stack_t *stack;rnrn if (NULL == pstack)rn return FALSE;rnrn stack = (stack_t*)malloc(sizeof(stack_t));rn if(NULL == stack)rn return FALSE;rn memset(stack,0,sizeof(stack_t));rn *pstack = stack;rn return TRUE;rnrnrnvoid DestroyStack(void **pstack, void(*snode_free)())rnrn stack_t *stack;rn int i;rn if(NULL == pstack || NULL == *pstack)rn return;rn stack = (stack_t*)*pstack;rn for(i = 0; i < MAX_STACK_SIZE; ++i)rn rn if(stack->base[i].value)rn rn (*snode_free)(stack->base[i].value);rn stack->base[i].value = NULL;rn rn rn free(stack);rn *pstack = NULL;rnrnrnvoid ClearStack(void **pstack, void (*snode_free)())rnrn stack_t *stack = (stack_t*)pstack;rn int i;rn if(NULL == stack)rn return;rn for(i = 0; i < MAX_STACK_SIZE; ++i)rn rn if(stack->base[i].value)rn rn (*snode_free)(stack->base[i].value);rn stack->base[i].value = NULL;rn rn rn stack->top = 0;rnrnrnBOOL StackEmpty(void *pstack)rnrn stack_t *stack = (stack_t*)pstack;rnrn if(NULL == stack)return TRUE;rn if(0 == stack->top)return TRUE;rnrn return FALSE;rnrnrnint StackLength(void *pstack)rnrn stack_t *stack = (stack_t*)pstack;rn if(NULL == stack) return -1;rnrn return stack->top;rnrnrnBOOL PushStack(void *pstack, void *value)rnrn stack_t *stack = (stack_t*)pstack;rn if(NULL == stack || NULL == value)return FALSE;rn if(MAX_STACK_SIZE == stack->top) return FALSE;rn stack->base[stack->top++].value = value;rn return TRUE;rnrnrnBOOL PopStack(void *pstack, void *pvalue)rnrn stack_t *stack = (stack_t*)pstack;rnrn if(NULL == stack || NULL == pvalue)return FALSE;rn if(0 == stack->top) return FALSE;rn *((void **)pvalue) = stack->base[--(stack->top)].value;rn stack->base[stack->top].value = NULL;rn return TRUE;rnrnrn#ifdef DEBUGrnrnint main(int argc,char *argv[])rnrn int i, *p, err = 0;rn void *s;rn if(FALSE == InitStack(&s))rn rn printf("InitStack Failed\n");rn return -1;rn rn for(i = 0; i < 10; ++i)rn rn p = (int *)malloc(sizeof(int));rn *p = i;rn if(FALSE == PushStack(s,(void*)p))rn rn printf("PushStack Failed\n");rn err++;rn break;rn rn rn if(err == 0)rn rn while(TRUE == PopStack(s,(void*)&p))rn rn printf("get a ele from stack:%d\n", *p);rn free(p);rn rn rn DestroyStack(&s,free);rn return 0;rnrn#endifrn[/code]rn这是一个堆栈的数组实现。rn初始化的时候为什么不是传void *,而是传了void **rnvoid*和void **到底怎么用?rnvoid ClearStack(void **pstack, void (*snode_free)());这里也不是很懂。rn虚心求教。 论坛

没有更多推荐了,返回首页