指针 - 示例问题 [英] Pointers - sample question

查看:83
本文介绍了指针 - 示例问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main( ) {
 char str[] = { 48, 49, 50, 51, 52, 53, 54};
 int *p = (int *) (str + 1);
 char *pStr = (char *)p;
 printf("%c\n", *pStr);
}







什么是o / p?

在* p中存储什么,* pStr存储什么?




what is the o/p ?
in *p what gets stored and *pStr what gets stored?

推荐答案

试试吧!

编译它,运行它然后看看。



输出为1

p包含一个指向字符数组中第二个字节的指针

所以pStr。



如果你尝试实际使用p作为指向int的指针并从中检索整数值,可能会有所不同 - 某些系统会失败,因为int指针(可能)不是字对齐的。但char值不必具有任何特定的对齐方式,因此工作正常。
Try it!
Compile it, run it and see.

Output is "1"
p contains a pointer to the second byte in the array of characters
So does pStr.

It might be different if you try to actually use p as a pointer to an int and retrieve an integer value from it - some systems will fail, because the int pointer is (probably) not word aligned. But char values don''t have to have any particular alignment, so that works fine.


这篇关于指针 - 示例问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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