char *的大小是否与int *的大小相同? [英] Is size of char * same as size of int *?

查看:258
本文介绍了char *的大小是否与int *的大小相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道: char *是char的指针. 和 int *是指向int的指针.

I know: char * is a pointer to char. and int * is a pointer to int.

所以,我想确认以下两件事:

So, i want to confirm following two things:

  1. 所以现在假设我在32位计算机上,那么这意味着内存地址为32位宽. 因此,这意味着char *和int *的大小均为32位(4个字节),对吗? char * *的大小也和int *的大小一样吗?

  1. So now suppose I am on 32 bit machine, then that means memory addresses are 32 bit wide. Thus that means size of char * and int * is both 32 bits ( 4 bytes), right ? Also is size of char * * also same as size of int * ?

假设我有: int * ptr;

suppose I have: int * ptr;

因此,现在做*((char * *)ptr)= 0x154与*((int *)ptr)= 0x514相同,对吧? (0x514只是任何随机存储器地址)

Thus now doing *((char * *) ptr) = 0x154 is same as *((int *) ptr) = 0x514 same, right ? ( 0x514 is just any random memory address)

平台:我在x86上.

P.S .:我知道类型转换不是建议的编码方式.但是我正在执行内核编码,因此我必须进行类型转换!

P.S.: I know type casting is not a suggested way to code. But I am doing Kernel coding, thus I HAVE TO do type casting !

推荐答案

在C中,指针不保证具有相同的大小.现在,实际上大多数实现指针将具有相同的大小,但这就是编译器的实现细节.

In C pointers are not guaranteed to have the same size. Now in reality most implementations pointers will be the same size, but that is an implementation detail of the compiler.

C常见问题解答:

旧的HP 3000系列对字节使用不同的寻址方案 地址而不是字地址;就像上面的几台机器一样 因此,它对char *和void *使用不同的表示形式 指针比其他指针

The old HP 3000 series uses a different addressing scheme for byte addresses than for word addresses; like several of the machines above it therefore uses different representations for char * and void * pointers than for other pointers

取决于使用的``内存模型'',8086系列处理器(PC 兼容)可以使用16位数据指针和32位函数 指针,反之亦然.

Depending on the ``memory model'' in use, 8086-family processors (PC compatibles) may use 16-bit data pointers and 32-bit function pointers, or vice versa.

*((char *)ptr) = 0x154*((int *)ptr) = 0x154不同.因为要取消引用指针,所以将char的大小和int的大小的数据写入到ptr指向的位置.假定为8位字符和32位int,*((char *)ptr) = 0x154会将0x154写入分配给ptr的内存地址,而*((int *)ptr) = 0x154会将0x0000000154写入从分配给ptr的地址开始的4个字节.

Also *((char *)ptr) = 0x154 is not the same as *((int *)ptr) = 0x154. Because you are dereferencing the pointer you will write data the size of a char and the size of an int into the location pointed to by ptr. Assuming an 8 bit char and a 32 bit int, *((char *)ptr) = 0x154 will write0x154 to the memory address assigned to ptr and *((int *)ptr) = 0x154 will write 0x0000000154 to the 4 bytes starting at the address assigned to ptr.

这篇关于char *的大小是否与int *的大小相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆