short放进short*
时间: 2023-07-05 18:21:00 浏览: 48
将short类型的变量放进short指针类型的变量中,可以通过取地址符&获取short变量的地址,然后将该地址赋值给short指针类型的变量,例如:
```
short x = 10;
short* ptr = &x;
```
这样,指针变量ptr就指向了short类型变量x的内存地址。
相关问题
short *和int *
short *和int *都是指针类型,但是它们指向的数据类型不同。short *指向的是short类型的数据,而int *指向的是int类型的数据。
在C语言中,short类型通常占用2个字节,而int类型通常占用4个字节。因此,使用short *可以节省内存空间,但是可能会导致精度损失。而使用int *可以保证数据的精度,但是会占用更多的内存空间。
需要注意的是,在进行指针运算时,short *和int *的行为也是不同的。因为short类型占用的内存空间比int类型小,所以short *在进行指针运算时会按照short类型的大小进行移动,而int *则会按照int类型的大小进行移动。
char* 转short*
char*转short*需要进行类型转换。可以使用reinterpret_cast操作符将char*转换为short*。例如,如果存在一个char*指针变量char_ptr,我们可以使用以下代码将其转换为short*指针变量short_ptr:
short* short_ptr = reinterpret_cast<short*>(char_ptr);
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)