指针初始化为什么?

Bor*_*ito 6 c pointers function-pointers character

有一件事总让我困惑,角色指针.经过四年多的努力,我再次陷入困境.

以上面提到的情况为例.为什么char指针会以这种方式运行?当指针指向什么都没有时,我们怎么能直接解决指针的内容呢?或者它就像char指针存储除地址之外的东西!

#include <stdio.h>
#include <stdlib.h>

int main()
{ 
char* charPtr="I cant understand why";
int* intPtr=60;


printf("%d\n", intPtr); //displays 60
printf("%p\n", intPtr); // displays the hex value of 60

printf("%s\n", charPtr); // displays the wh0le string
printf("%p\n", charPtr); // displays the start address of the string
return 0;
Run Code Online (Sandbox Code Playgroud)

}

接下来是int指针,它如何接受值60以及它存储在何处?

抛开char指针和malloc,我认为指针的基本思想是得到一个指向的地址!

为什么这些案件

 *intptr = 60 ; // should be setting the pointee's value to 60
  intptr = 60 ;  // sets the address
Run Code Online (Sandbox Code Playgroud)

抛出编译错误

  int* intPtr=60;
Run Code Online (Sandbox Code Playgroud)

偷偷摸摸没有得到一个地址(或者60作为地址,如果是这样,为什么这在前一种情况下是不可接受的)指针!

我想我在这里错过了一些东西,但是嘿!你猜怎么着 ?他们告诉我在SO搜索!

编辑:给char指针指向的地址指向一个int指针也没有错误!

int8_t* intPtr= (int8_t*)0x80485c8 ; // works without casting too ! I guess addresses are acceptable.
Run Code Online (Sandbox Code Playgroud)

取消引用它将给出一个等于I字符串第一个的值.这是一个很好的做法,或者存在任何其他解释,这样就省去了字节位大小分配,比如int可以保存一个char等等.

正如hmjd指出'初始化语法'是问题所在!我编写自己的代码没有问题,但修改某人的代码时会出现问题!

hmj*_*mjd 6

当指针指向什么都没有时,我们怎么能直接解决指针的内容呢?或者它就像char指针存储除地址之外的东西!

认为混淆是初始化语法.这个:

char* charPtr="I cant understand why";
Run Code Online (Sandbox Code Playgroud)

没有解除引用charPtr.它相当于:

char* charPtr;
charPtr = "I cant understand why";
Run Code Online (Sandbox Code Playgroud)

两个代码片段都将字符串文字的地址存储"I cant understand why"charPtr.没有指向未指向任何内容的指针的解除引用.任何类型的指针变量只能存储地址.

这个:

int* intPtr=60;
Run Code Online (Sandbox Code Playgroud)

店的地址60intPtr:没有int转让或deferencing正在发生.此时不int存在变量.编译器应该在此行发出警告.任何尊重的尝试都很intPtr可能导致崩溃.


Joh*_*ode 4

在 C 中,像“我无法理解为什么”这样的字符串文字char被存储为一个数组,这样内存在程序的整个生命周期中都是可用的(所有地址都是凭空提取的,并不意味着代表任何特定的地址)平台或架构):

Item        Address        0x00  0x01  0x02  0x03
-----       -------        ----  ----  ----  ----
"I..."      0x00080000      'I'   ' '   'c'   'a'
            0x00008004      'n'   '''   't'   ' '
            0x00008008      'u'   'n'   'd'   'e'
            0x0000800C      'r'   's'   't'   'a' 
            0x00008010      'n'   'd'   ' '   'w' 
            0x00008014      'h'   'y'  0x00  0x??
Run Code Online (Sandbox Code Playgroud)

字符串文字也是一个数组表达式,在大多数情况下,“N 元素数组T”类型的表达式将转换为“指向的指针T”类型,其值将是数组第一个元素的地址(例外情况是数组表达式是sizeofor 一元运算&符的操作数,或者是用于在声明中初始化数组的字符串文字)。

所以当你写的时候

char* charPtr = "I can't understand why";
Run Code Online (Sandbox Code Playgroud)

您将字符串文字的地址charPtr复制到:

Item        Address        0x00  0x01  0x02  0x03
----        -------        ----  ----  ----  ----
charPtr     0xffbe4000     0x00  0x08  0x00  0x00
Run Code Online (Sandbox Code Playgroud)

请注意,如果声明已

char str[] = "I can't understand why";
Run Code Online (Sandbox Code Playgroud)

str将被分配为一个char足够长的数组来容纳字符串,并且字符串的内容将被复制到其中:

Item        Address        0x00  0x01  0x02  0x03
-----       -------        ----  ----  ----  ----
str         0xffbe4000      'I'   ' '   'c'   'a'
            0xffbe4004      'n'   '''   't'   ' '
            0xffbe4008      'u'   'n'   'd'   'e'
            0xffbe400C      'r'   's'   't'   'a' 
            0xffbe4010      'n'   'd'   ' '   'w' 
            0xffbe4014      'h'   'y'  0x00  0x??
Run Code Online (Sandbox Code Playgroud)

当你写的时候

int* intPtr = 60;
Run Code Online (Sandbox Code Playgroud)

您将指针值初始化为 60,而不是将其设置为指向值为 60 的匿名整数:

Item        Address        0x00  0x01  0x02  0x03
----        -------        ----  ----  ----  ----
intPtr      0xffbe4004     0x00  0x00  0x00  0x3C
Run Code Online (Sandbox Code Playgroud)

地址 60 很可能不是有效地址,因此尝试取消引用intPtr很可能会导致未定义的行为。

你写过类似的东西吗

int x = 60;
int *intPtr = &x;
Run Code Online (Sandbox Code Playgroud)

那么你就会遇到这样的情况:

Item        Address        0x00  0x01  0x02  0x03
----        -------        ----  ----  ----  ----
x           0xffbe4004     0x00  0x00  0x00  0x3C
intPtr      0xffbe4008     0xff  0xbe  0x40  0x04
Run Code Online (Sandbox Code Playgroud)

在这种情况下, 的值intPtr是 的地址x

最后,请注意初始化赋值不是一回事。

T *x = value;
Run Code Online (Sandbox Code Playgroud)

不取消引用x并分配value给结果;它value直接分配给x. 的类型value被视为T *。请注意,您应该收到警告

int *intPtr = 60;
Run Code Online (Sandbox Code Playgroud)

沿着“从整数创建指针而不进行转换”的思路。