void * 和 void 在函数返回值中的区别
2013-11-16 23:41
309 查看
一个很容易糊涂的问题.
在函数的返回值中, 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 * 是返回任意类型的值的指针.
还是看代码吧:
#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 在函数返回值中的区别
- c语言创建函数时void *与 void做修饰符的区别
- 函数参数返回值 void 和 void *
- 函数参数返回值 void 和 void *
- void定义函数与int、double的区别
- c 和 c++ 中的 函数void形参和空形参的区别
- C++函数中返回引用和返回值的区别
- u8 temp; (void *) temp 和 (void *) & temp 有什么区别,具体运算过程是啥? u8 data; u8 k=1;
- C语言中函数的参数列表为空和void的区别
- C语言学习6 :指针的定义,指针类型要合法,指针要初始化,指针做函数参数,数组和指针的通用性,指针+1所代表的空间,void * 指针,交换函数中的指针,数组和字符型指针区别,字符型指针的应用,使用指针完成字符操作函数
- typedef int(init_fnc_t) (void) 和 typedef int(*init_fnc_t) (void)函数类型和函数指针的区别
- 函数返回值为自定义的类型与引用类型的区别
- 函数指针和函数返回值为引用的区别
- void func()函数和void func(void)函数的区别
- 函数参数列表为空和void的区别
- 函数参数为void和没有参数的区别
- python中函数返回值为func 和func() 的区别
- 指针函数(函数返回值是某一类型指针)与函数指针的区别
- C中-void类型函数同样可以让函数有返回值
- 【从C到C++学习笔记】引用/const引用/引用传递/引用作为函数返回值/引用和指针的区别