指针初始化什么? [英] What does a pointer initialise to?

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

问题描述

一个总是使我困惑的东西,字符指针. 漫长的四年之后,我再次徘徊在c上.

One thing that always confused me , the character pointer. It's after long four years that I am again lingering into c.

以上述情况为例.为什么char指针以这种方式工作?当指针指向无内容时,或者当char指针存储除地址之外的其他东西时,如何直接寻址该指针的内容!

Take for example the mentioned case .Why does char pointer behave in this way ? How can we directly address the contents of the pointee when it points to nothing or is it like char pointer stores stuffs other than addresses !

#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;

}

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

Next the int pointer , How can it accept the value 60 and where does it get stored ?

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

leaving aside the char pointer and malloc ,I thought the basic idea of the pointer was to get an address to point to !

为什么会这样

 *intptr = 60 ; // should be setting the pointee's value to 60
  intptr = 60 ;  // sets the address

抛出编译错误

  int* intPtr=60;

偷偷溜进去,没有得到对方的地址(或者将60作为地址,如果是这样,为什么在前一种情况下不能接受)!

sneaked in without getting an address ( or is 60 taken as the address ,if so why is this not acceptable not in the former case)of the the pointee !

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

I guess I am missing something here but hey ! Guess what ? they told me to search in SO !

将char指针指向的地址赋予int指针也不会出错!

EDIT : Giving the address pointed to by the char pointer to an int pointer also throws in no error !

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

将其解引用将得到与字符串的第一个I相等的值.这是一个好习惯,还是对此有任何其他解释,省去了它们的字节位大小分配,例如int可以容纳一个char等. ..?

Dereferencing it will give a value equivalent to the first I of the string .Is this a good practise or there exists any other explanation to this leaving out thier byte bit size allocation such as an int can hold a char and so.. ?

正如hmjd指出的那样,初始化语法"就是问题所在!我编写自己的代码没有问题,但是在修改某人的代码时会遇到麻烦!

As hmjd pointed out the ' initialisation syntax ' is the problem ! I have no problem writing my own code but trouble arises when modifying someone's code !

推荐答案

在C语言中,诸如我听不懂为什么"之类的字符串文字存储为char数组,这样内存在程序的整个生命周期中都是可用的(所有地址都是凭空获取的,并不代表任何特定的平台或体系结构):

In C, a string literal like "I can't understand why" is stored as an array of char such that the memory is available over the lifetime of the program (all addresses are pulled out of thin air and are not meant to represent any specific platform or architecture):

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??

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

The string literal is also an array expression, and in most contexts an expression of type "N-element array of T" will be converted to type "pointer to T", and its value will be the address of the first element of the array (the exceptions are when the array expression is an operand of the sizeof or unary & operators, or is a string literal being used to initialize an array in a declaration).

所以当你写

char* charPtr = "I can't understand why";

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

you're copying the address of the string literal to charPtr:

Item        Address        0x00  0x01  0x02  0x03
----        -------        ----  ----  ----  ----
charPtr     0xffbe4000     0x00  0x08  0x00  0x00

请注意,如果声明已经

char str[] = "I can't understand why";

str应该被分配为足够长的char数组以容纳该字符串,并且该字符串的 contents 将被复制到其中:

str would have been allocated as an array of char long enough to hold the string, and the contents of the string would have been copied to it:

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??

写作时

int* intPtr = 60;

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

you're initializing the pointer value with 60, not setting it to point to an anonymous integer with the value 60:

Item        Address        0x00  0x01  0x02  0x03
----        -------        ----  ----  ----  ----
intPtr      0xffbe4004     0x00  0x00  0x00  0x3C

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

Address 60 is most likely not a valid address, so attempting to dereference intPtr would most likely lead to undefined behavior.

你写过类似的东西

int x = 60;
int *intPtr = &x;

然后您将遇到如下情况:

then you'd have a situation like this:

Item        Address        0x00  0x01  0x02  0x03
----        -------        ----  ----  ----  ----
x           0xffbe4004     0x00  0x00  0x00  0x3C
intPtr      0xffbe4008     0xff  0xbe  0x40  0x04

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

In this case, the value of intPtr is the address of x.

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

Finally, note that initialization and assignment are not the same thing.

T *x = value;

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

does not dereference x and assign value to the result; it assigns value directly to x. The type of value is treated as T *. Note that you should be getting warnings on

int *intPtr = 60;

沿从没有转换的整数中创建指针"的行.

along the lines of "making pointer from integer without cast".

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

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