简体   繁体   English

在c中发现指针的混乱

[英]Finding confusion in pointer in c

#include<stdio.h>

int main(){
    int a = 320;
    char *ptr;
    ptr =( char*)&a;
    printf("%d",*ptr);
    return 0;
}

I just wanted to know what is the use (char*) ? 我只是想知道有什么用(char*) What if I used (int*) instead of that? 如果我使用(int*)而不是那个怎么办?

Your variable is of type integer ie int a = 320; 你的变量是整数类型,即int a = 320; Your pointer is of type character ie char *ptr; 你的指针是字符类型,即char *ptr;

character pointer can only points to character type variable, but you in your code want to point a integer variable so how would that be possible? 字符指针只能指向字符类型变量,但是你的代码中想要指向一个整数变量,那怎么可能呢? By this line of code: ptr =( char*)&a; 通过这行代码: ptr =( char*)&a; what it doing is type casting your pointer varble explicitly or forcefully (whatsoever you might want to call), by making it character type. 它做的是通过使其成为字符类型,显式地或强制地(无论你想要调用什么)类型转换指针varble。

Let's change the program a bit: 让我们稍微改变一下程序:

int main() {
    int a = 0x12345678;   // put hexadecimal value 12345678 into a
    char *charptr = (char*)&a;
    int *intptr = &a;                
    printf("%x\n", *charptr);   // %x prints in hexadecimal
    printf("%x\n", *intptr);
    return 0;
}

On a little endian system the output will be : 在一个小端系统上,输出将是:

78
12345678
  • &a is the memory address of the a variable. &a是的存储器地址a变量。
  • charptr is a pointer to char charptr是一个指向char的指针
  • intptr is a pointer to int intptr是一个指向int的指针

The second output 12345678 is obvious, but the first output 78 is not quite as obvious. 第二个输出12345678很明显,但第一个输出78并不那么明显。

On a little endian system the int a in memory looks like this: 在一个小端系统上,内存中的int a如下所示:

+----+
| 78 |  <---- both 'charptr' and 'intptr' point here
+----+
| 56 |
+----+
| 34 |
+----+
| 12 |
+----+

The char (or byte) at the address &a is 0x78, therefore if you dereference the charptr pointer you get 0x78 . 地址&a处的char(或字节)是0x78,因此如果取消引用charptr指针,则得到0x78

On a big endian system we have this: 大端系统上,我们有:

+----+
| 12 |  <---- both 'charptr' and 'intptr' point here
+----+
| 34 |
+----+
| 56 |
+----+
| 78 |
+----+

and the first output would be 12 instead of 78 . 第一个输出是12而不是78


In this line 在这一行

char *charptr = (char*)&a;

the (char*) is called a cast, and it tells the compiler to consider &a as a pointer to char . (char*)被称为强制转换,它告诉编译器将&a视为指向char的指针。

Without the cast: 没有演员:

char *charptr = &a;

you will get a compiler warning such as incompatible types - from 'int *' to 'char *' , because you are trying to assign a pointer to int to a pointer to char . 你将得到一个编译器警告,例如incompatible types - from 'int *' to 'char *' ,因为你试图将指向int的指针指向一个指向char的指针。

If you write this: 如果你这样写:

char *charptr = (int*)&a;

then you will also get a compiler warning incompatible types - from 'int *' to 'char *' . 那么你也会得到一个编译器警告incompatible types - from 'int *' to 'char *' Anyway in that case the (int*) cast is useless because &a is already a pointer to int . 反正在这种情况下, (int*)投是无用的,因为&a已经一个指针int

calling a variable ptr doesn't make it a pointer. 调用变量ptr不会使它成为指针。 if you want it to be a pointer it has to be declared with a * ie int *ptr; 如果你想让它成为一个指针,它必须用* ie int *ptr; or char *ptr; char *ptr;

Casting a pointer such as &a to type char is just silly as they're totally different things. 将类型为&a的指针转换为char类型只是愚蠢的,因为它们是完全不同的东西。 Using (int *) wouldn't change the type of &a so you'd not need to put it in, but then your compiler would show you that ptr is totally the wrong type for storing the value of &a in. 使用(int *)不会改变&a的类型,所以你不需要把它放进去,但是你的编译器会告诉你ptr完全是用于存储&a的值的错误类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM