1、前言
今天查看代码时,我发现指针被指定给了intptr_t类型的变量。 因为到现在为止都没有见过intptr_t这样的数据类型,所以在感觉上intptr_t应该是int型的指针。 我很奇怪为什么要这样做指针。 我下决心在网上查了一下,发现我的感觉不对。 所以,任何事情都要弄清经过,而不是感觉。 首先总结intptr_t型,然后介绍指针和intptr_t型的变换,最后给出测试程序。
2、intptr_t型
我接触的第一个处理器是32位,现在64位处理器发展很快。 数据类型(特别是与int相关的类型)在不同位数的计算机平台上的长度不同。 C99标准没有规定特定数据类型的长度大小。
位数
查尔斯
短整型
资讯科技
龙格
指针
16
1字节8位
2字节16位
2字节16位
4字节32位
2字节16位
32
1字节8位
2字节16位
4字节32位
4字节32位
4字节32位
64
1字节8位
2字节16位
4字节32位
8字节64位
8字节64位
为了保证平台的通用性,程序中请勿使用长整型。 可以使用必须引用stdint.h头文件的固定大小的数据类型宏定义。
/* thereissomeamountofoverlapwithasknownbyinetcode * /
#ifndef __int8_t_defined
# define __int8_t_defined
类型签名char int8_ t;
typedef short int int16_t;
类型def int int 32 _ t;
# if __WORDSIZE==
typedef long int int64_t;
# else
__extension__
typedef long long int int64_t;
# endif
#endif
/*未注册. * /
类型def unsigned char uint8_ t;
typedefunsignedshortintuint 16 _ t;
#ifndef __uint32_t_defined
类型def unsigned int uint 32 _ t;
# define __uint32_t_defined
#endif
#if __WORDSIZE==64
类型def unsigned long int uint 64 _ t;
#else
__extension__
typedefunsignedlonglongintuint 64 _ t;
#endif
intptr_t的类型定义如下:
//intptr_t型是为指针准备的
/* Types for `void *' pointers. */
#if __WORDSIZE==64
# ifndef __intptr_t_defined
typedef long int intptr_t;
# define __intptr_t_defined
# endif
typedefunsignedlongintuintptr _ t;
#else
# ifndef __intptr_t_defined
typedef int intptr_t;
# define __intptr_t_defined
# endif
typedef unsigned int uintptr_t;
#endif
从定义中可以看到,intptr_t用于存储地址(即地址),因为intptr_t因平台而异,并且始终与地址位数相同。
3、指针和intptr_t
c语言指针是用于保存变量或常量的地址,地址由处理器的位数决定。 windows程序中经常使用句柄,但实际上是地址,具有通用性,封装了基础。 首先这个理解不深刻,什么时候需要将指针转换为intptr_t类型?
4、测试程序
#包含
#包含
#包含
#包含
#包含
#包含
#define ID_STR_LE
N 12#define NAME_STR_LEN 10
typedef struct student
{
char id[ID_STR_LEN];
char name[NAME_STR_LEN];
uint8_t age;
}student;
student * create_student()
{
student *stu = (student *)malloc(sizeof(student));
if (stu == NULL)
return NULL;
memset(stu, , sizeof(student));
return stu;
}
void *free_student(student *stu)
{
if (stu)
free(stu);
}
static void init_student(student * stu)
{
assert(stu);
const char *id = "";
const char *name = "Anker";
uint8_t age = ;
memcpy(stu->id, id, strlen(id));
memcpy(stu->name, name, strlen(name));
stu->age = age;
}
static int handle_student(intptr_t handle)
{
if (handle == )
{
return -;
}
student *stu = (student*)handle;
printf("id: %sn", stu->id);
printf("name: %sn", stu->name);
printf("age: %un", stu->age);
return ;
}
int main()
{
student *stu;
stu = create_student();
init_student(stu);
//将指针转换为intptr_t类型
intptr_t handle = (intptr_t)stu;
handle_student(handle);
free_student(stu);
return ;
}
5、参考网址
C语言指针的长度和类型
本文地址:http://www.cnblogs.com/archimedes/p/point-length-type.html,转载请注明源地址. 如果考虑应用程序的兼容性和可移植性,指针的长度就是一 ...
C语言指针类型 强制转换
关于C语言指针类型 强制转换 引用一篇文章: C语言中,任何一个变量都必须占有一个地址,而这个地址空间内的0-1代码就是这个变量的值.不同的数据类型占有的空间大小不一,但是他们都必须有个地址,而这个 ...
cocos2d-x getParent() 获得一个父类的一个node型指针,转换为父类类型
void CenterLayer::zhanzheng(CCObject* pSender){ ((GameScene*)this->getParent())->showLayer(Gam ...
C语言指针5分钟教程
指针.引用和取值 什么是指针?什么是内存地址?什么叫做指针的取值?指针是一个存储计算机内存地址的变量.在这份教程里“引用”表示计算机内存地址.从指针指向的内 存读取数据称作指针的取值.指针可以指向某些 ...
C语言指针的初始化和赋值
1.指针的初始化 指针初始化时,"="的右操作数必须为内存中数据的地址,不能够是变量,也不能够直接用整型地址值(可是int*p=0;除外,该语句表示指针为空).此时,*p仅仅是表示 ...
C语言指针操作