解释指针引用? [英] Explain Pointer Dereferencing?

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

问题描述

我是在C初学者,我想知道什么是取消引用指针的意思,另外,如果我做了一个指针指向一个字符是一个字节。然后,我打印的字符值解引用指针那么如何指针知道它有阅读只有一个字节?

I Am a beginner in c and I want to know what is meant by dereferencing pointer and also if i made a pointer point to a character which is of one byte . Then i print the character value dereferencing pointer then how does the pointer know that it has to read only one byte ?

推荐答案

如果您有类似,

int x = 1234 ;
int *p = &x;

如果取消引用指针 P 那么它会读取正确的整数字节。因为你宣布它为指针 INT 。它会知道有多少字节由的sizeof()运营商阅读。一般 INT 的大小为4个字节,但它是依赖于机器的,这就是为什么它会使用的sizeof()运营商知道正确的尺寸,将读这样。如果你有这样的事情

If You Dereference Pointer p then it will correctly read integer bytes. Because You declared it to be pointer to int . It will know how many bytes to read by sizeof() operator. Generally size of int is 4 bytes but it is machine dependent that is why it will use sizeof() operator to know correct size and will read so .If you have something like this

 int y = 1234;
 char *p = &y;
 int *j  = &y;

现在指针p ,但我们已经声明它是指向一个字符所以它只能读取一个字节或任何字节字符是。
psented为 1234 二进制将重新$ P $ 00000000 00000000 00000100 11010010

Now pointer p points to y but we have declared it to be pointer to a char so it will only read one byte or whatever byte char is of . 1234 in binary would be represented as 00000000 00000000 00000100 11010010.

现在如果你的机器是Little Endian它将存储扭转他们的字节 11010010 00000100 00000000 00000000 11010010 0地址 假设地址 00000100 地址1 等。

Now if your machine is little endian it will store the bytes reversing them 11010010 00000100 00000000 00000000 . 11010010 is at address 0 Hypothetical address , 00000100 is at address 1 and so on.

所以,现在,如果你解引用指针p 将只读第一个字节,输出将被 -46 为字节阅读是 11010010 (因为我们指出符号字符,所以最显著位为符号位。第一个位 1 表示符号。 11010010 = -128 + 64 + 16 + 2 = -46 ),如果你解引用指针Ĵ将彻底读 INT 的所有字节我们宣布它是指向int的指针。和输出将 1234

So now if you dereference pointer p it will read only first byte and output will be -46 as byte read would be 11010010(Because we pointed signed char ,so the most-significant bit is the sign bit. First bit 1 denotes the sign. 11010010 = –128 + 64 + 16 + 2 = –46.) and if you dereference pointer j it will completely read all bytes of int as we declared it to be pointer to int . And output will be 1234

和另外如果你声明指针Ĵ为为int *Ĵ然后会读的sizeof(INT)通常为4个字节, *(J + 1)也将读取的字节数。同去与字符或指针所指向的任何其他数据类型将读取的字节数有规模的。一般字符 1个字节的。

And Also if you declare pointer j as int *j then *j will read sizeof(int) usually 4 bytes and *(j+1) will also read that many bytes . Same goes with char or any other data type the pointer pointed to them will read as many bytes there size is of. Generally char is of 1 byte.

这篇关于解释指针引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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