汎用ポインタ

void *変数名


#include <stdio.h>

typedef struct tagTest {
 char c;
 int i;
} _TEST;

void Disp( void *t )
{
 printf( "%d\n", sizeof( *(_TEST*)t ) );
}

void main()
{
 _TEST test;
 Disp( &test );
}


汎用ポインタは任意のポインタを代入する事が可能であり、
同様に型キャストによって型を復元させる事ができます。


#include <stdio.h>

void Disp( void *t )
{
 char *pstr = (char*)t;
 puts( pstr );
 putchar( (int)*pstr );

 int *pd = (int*)t;
 putchar( *pd );
}

void main()
{
 char *str = "abcdefg";
 Disp( str );
}


あらゆる型のポインタをキャストできます。
C > ポインタ | comments (0) | trackbacks (0)

Comments

Comment Form

icons:

Trackbacks