首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

指针挟制转化

2013-06-26 
指针强制转化glob.hstruct abc{int val1int val2char* p...} /* end glob.h */example1.c#includeglob

指针强制转化


glob.h

struct abc{
    int val1;
    int val2;
    char* p;
...

/* end glob.h */

example1.c
#include"glob.h"

extern void func(void *);
...
struct abc sa = {...};

/* call function define in example2.c */
func((void*)&sa);

/* end example1.c */

example2.c
#include"glob.h"
void func(void *);
func(void * p)
{
    struct abc *sa = (struct abc *)p;
    sa->xxx = ...
}
/* example2.c */



我在 example1.c 里调用函数func,把struct * 强制转换成void * 传进去,然后再在func里把void * 转过来用可以吗? 
谢谢。 C 指针
[解决办法]
可以
[解决办法]
参考MSDN里面的qsort:
qsort
Performs a quick sort.

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

Routine Required Header Compatibility 
qsort <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

None

Parameters

base

Start of target array

num

Array size in elements

width

Element size in bytes

compare

Comparison function

elem1

Pointer to the key for the search

elem2

Pointer to the array element to be compared with the key

Remarks

The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call:



compare( (void *) elem1, (void *) elem2 );

The routine must compare the elements, then return one of the following values:

Return Value Description 
< 0 elem1 less than elem2 
0 elem1 equivalent to elem2 
> 0 elem1 greater than elem2 


The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “less than” in the comparison function.

Example

/* QSORT.C: This program reads the command-line
 * parameters and uses qsort to sort them. It
 * then displays the sorted arguments.
 */

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

int compare( const void *arg1, const void *arg2 );

void main( int argc, char **argv )
{
   int i;
   /* Eliminate argv[0] from sort: */
   argv++;
   argc--;

   /* Sort remaining args using Quicksort algorithm: */
   qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare );

   /* Output sorted list: */
   for( i = 0; i < argc; ++i )
      printf( "%s ", argv[i] );
   printf( "\n" );
}

int compare( const void *arg1, const void *arg2 )
{
   /* Compare all of both strings: */
   return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
}


Output

[C:\code]qsort every good boy deserves favor
boy deserves every favor good


Searching and Sorting Routines

See Also   bsearch, _lsearch


[解决办法]
不是版主,回答你的问题你就不搭理是吧!指针挟制转化

你的代码没有平台依赖性,可以移到别的平台

引用:
Quote: 引用:

可以

谢谢版主大哥。
  其实func的调用跨越了几个文件,那几个文件里没有 struct abc的定义,我想这样转下类型传到最终的.c里,目前在自己的板子上跑了下,是没问题。

但是移植到另一个平台的时候会不会有问题呢?

热点排行