一个很容易糊涂的问题.

在函数的返回值中, void 是没有任何返回值, 而 void * 是返回任意类型的值的指针.

还是看代码吧:

#include <stdlib.h>
#include <stdio.h> void voidc(int a);
void* voidcp(int *a);
int main(){
int a=10;
int *ap; voidc(a);
ap = (int*)voidcp(&a);
printf("%d\n",*ap); return 0;
}
void voidc(int a){
printf("%d\n",a);
return; // 没有返回值
}
void* voidcp(int *a){
printf("%d\n", *a);
return a; // 返回 int *
}

二分查找的利用代码:

bsearch

Performs a binary search of a sorted array.



void *bsearch( const void *key, const void *base, size_t num, size_t width, int ( __cdecl *compare ) ( const void *elem1, const void *elem2 ) );



Routine Required Header Compatibility 

bsearch <stdlib.h> and <search.h> ANSI, Win 95, Win NT  





For additional compatibility information, see Compatibility in the Introduction.



Libraries



LIBC.LIB Single thread static library, retail version 

LIBCMT.LIB Multithread static library, retail version 

MSVCRT.LIB Import library for MSVCRT.DLL, retail version 





Return Value



bsearch returns a pointer to an occurrence of key in the array pointed to by base. If key is not found, the function returns NULL. If the array is not in ascending sort order or contains duplicate records with identical keys, the result is unpredictable.



Parameters



key



Object to search for



base



Pointer to base of search data



num



Number of elements



width



Width of elements



compare



Function that compares two elements: elem1 and elem2



elem1



Pointer to the key for the search



elem2



Pointer to the array element to be compared with the key



Remarks



The bsearch function performs a binary search of a sorted array of num elements, each of width bytes in size. The base value is a pointer to the base of the array to be searched, and key is the value being sought. The compare parameter is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. bsearch calls the compare routine one or more times during the search, passing pointers to two array elements on each call. The compare routine compares the elements, then returns one of the following values:



Value Returned by compare Routine Description 

< 0 elem1 less than elem2 

0 elem1 equal to elem2 

> 0 elem1 greater than elem2 





Example



/* BSEARCH.C: This program reads the command-line

 * parameters, sorting them with qsort, and then

 * uses bsearch to find the word "cat."

 */



#include <search.h>

#include <string.h>

#include <stdio.h>



int compare( char **arg1, char **arg2 ); /* Declare a function for compare */



void main( int argc, char **argv )

{

   char **result;

   char *key = "cat";

   int i;



   /* Sort using Quicksort algorithm: */

   qsort( (void *)argv, (size_t)argc, sizeof( char * ), (int (*)(const 

   void*, const void*))compare );



   for( i = 0; i < argc; ++i )    /* Output sorted list */

      printf( "%s ", argv[i] );



   /* Find the word "cat" using a binary search algorithm: */

   result = (char **)bsearch( (char *) &key, (char *)argv, argc,

                              sizeof( char * ), (int (*)(const void*, const void*))compare );

   if( result )

      printf( "\n%s found at %Fp\n", *result, result );

   else

      printf( "\nCat not found!\n" );

}



int compare( char **arg1, char **arg2 )

{

   /* Compare all of both strings: */

   return _strcmpi( *arg1, *arg2 );

}





Output



[C:\work]bsearch dog pig horse cat human rat cow goat

bsearch cat cow dog goat horse human pig rat

cat found at 002D0008





Searching and Sorting Routines



See Also   _lfind, _lsearch, qsort

纯真数据库的处理代码:

char c_text[1024], l_text[1024]; //返回用

void *result[] = {c_text, l_text};//返回用

void* __cdecl _GetAddress(const char *IPstr)

{

if( !loaded || !ptr )

return noload_result;





unsigned int ip = str2ip(IPstr);

get_ipwry(ip);





return result;

}

#include <iostream>
#include "ipsearcher.h"
using namespace std;
#pragma comment(lib, "ipsearcher.lib") int main(void)
{
typedef void *RESULT[2];//返回用
cout <<IPCount() << endl;
cout << DateTime() << endl;
char **pstr = (char**)_GetAddress("192.168.1.1");
cout << pstr[0] << pstr[1] << endl;
return 0;
}

void * 和 void 在函数返回值中的区别的更多相关文章

  1. js遍历ajax回调函数返回值中的object对象

    function printObject(obj) {      //obj = {"cid":"C0","ctext":"区县& ...

  2. C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。

    //将3*4矩阵中找出行最大,列最小的那个元素. #include <stdio.h> #define M 3 #define N 4 void fun(int (*a)[N]) { ,j ...

  3. 关于QT中evaluateJavaScript()函数返回值的处理问题

    关于QT中evaluateJavaScript()函数返回值的处理问题 - 寒风问雪的专栏 - 博客频道 - CSDN.NET 关于QT中evaluateJavaScript()函数返回值的处理问题 ...

  4. 获得函数返回值类型、参数tuple、成员函数指针中的对象类型

    //function_traits.h,获得函数返回值类型.参数tuple.成员函数指针中的对象类型 //参考https://github.com/qicosmos/cosmos/blob/maste ...

  5. coalesce函数-返回参数中第一个非null值

    coalesce函数-返回参数中第一个非null值 学习了:http://www.cnblogs.com/zc_0101/archive/2009/08/11/1543650.html 这个要复杂一些 ...

  6. c语言main函数返回值、参数详解(返回值是必须的,0表示正常退出)

    C语言Main函数返回值 main函数的返回值,用于说明程序的退出状态.如果返回0,则代表程序正常退出:返回其它数字的含义则由系统决定.通常,返回非零代表程序异常退出. 很多人甚至市面上的一些书籍,都 ...

  7. C++ const修饰函数、函数参数、函数返回值

    const修饰函数 在类中将成员函数修饰为const表明在该函数体内,不能修改对象的数据成员而且不能调用非const函数.为什么不能调用非const函数?因为非const函数可能修改数据成员,cons ...

  8. C语言中malloc函数返回值是否需要类型强制转换问题

    1. 在C语言中, 如果调用的函数没有函数原型, 则其返回值将默认为 int 型. 考虑调用malloc函数时忘记了 #include <stdlib.h>的情况 此时malloc函数返回 ...

  9. 【C/C++】引用&的含义/语法/作为函数参数/函数返回值/本质/常量引用

    含义 引用不产生副本,只是给原变量起了别名. 对引用变量的操作就是对原变量的操作. 基本语法 数据类型 &别名 = 原名 e.g. int a = 10; int &b = a; // ...

随机推荐

  1. [quack] A browser extension to Clean website pages ADs

    1. youdao.com result page Before: After: Later we will add more and more. Or you can submit issues t ...

  2. java实现单向循环链表

    链表图解 带头结点的链表: 不带头结点的链表: 区别 带头结点的链表容易代码实现 不带头结点的容易实现循环链表和双向链表 代码的实现 (增减 删除) 节点实现: public class node { ...

  3. 洛谷训练新手村之“BOSS战-入门综合练习1”题解

    P1478 陶陶摘苹果(升级版) 题目链接:https://www.luogu.com.cn/problem/P1478 题目大意:陶陶有s点体力值,每个苹果消耗体力值,问s体力值最多能摘多少苹果. ...

  4. Python 处理Excel内的数据(案例介绍*2)

    (一)案例一介绍 现在有一匹电商产品跟当日销量的数据,如下,总共有上万笔的数据,现在需要统计每个品牌当日的销售量,比如美宝莲今天总共卖出了多少的商品,另外需要统计每个品牌下面的每个子品类当日销售量(品 ...

  5. window 下配置wamp 环境

    PHP下载 下载php压缩包,几点注意:这里我安装的事apache 所以在 php官方下载页时不是随便下载的,不然可能配置不了apache! 注意我以下图片标注

  6. 开源API网关,你选对了么?

    开源API网关,你选对了么? api网关的本质 不用扯那么多,也不用画图,一句话说清楚 api网关:流量总入口,得以集中控制! 就这么简单 api网关协议上最基本要支持HTTP 和 WebSocket ...

  7. Java对象头与锁

    对象由多部分构成的,对象头,属性字段.补齐区域等.所谓补齐区域是指如果对象总大小不是4字节的整数倍,会填充上一段内存地址使之成为整数倍. 后面两个很好理解,今天我主要想总结一下对象头: 对象头这部分在 ...

  8. Java并发-Java内存模型(JMM)

    先来说说什么是内存模型吧 在硬件中,由于CPU的速度高于内存,所以对于数据读写来说会出现瓶颈,无法充分利用CPU的速度,因此在二者之间加入了一个缓冲设备,高速缓冲寄存器,通过它来实现内存与CPU的数据 ...

  9. MongoDB高级用法

    MongoDB高级查询用法大全 转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册:http://ww ...

  10. 自学 JAVA 的几点建议

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 许久不见,最近公众号多了很多在校的师弟师妹们.有很多同学都加了我微信问了一些诸如 [如何自学 Java ]的问题, ...