关于指针的基本问题 [英] Basic question about pointers

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

问题描述

我对指针有一个非常基本的问题。请原谅我

我对C的无知

int main()

{

int * i;

* i = 1 //有时这可能会给我一个核心转储。

const char * str =" test" ;; //这似乎是一个有效的结构。

}


现在我明白当我们声明int * i时,我们只有一个指针

可能指向任何地方。如果我们尝试在其中存储内容,我们

可能会出现分段错误。但为什么const char * str =" test"

有效?我们不需要先为str分配内存吗?有人可以吗?b $ b请对此有所了解吗?


提前致谢

I have a very elementary question about pointers. Please pardon me
for my ignorance of C

int main()
{
int* i;
*i = 1 //at times this may give me a core dump.
const char* str = "test"; //This seems to be a valid construct.
}

Now I understand that when we declare int* i, we just have a pointer
that may point to any place. If we try to store something in it, we
may get a segmentation fault. But why does const char* str = "test"
work? Do we not need to allocate memory for str first? Can someone
please shed some light on this?

Thanks in advance

推荐答案

7月15日晚上10点14分,Aarti< aarti.sa ... @ gmail.comwrote:
On Jul 15, 10:14 pm, Aarti <aarti.sa...@gmail.comwrote:

我有一个非常关于指针的基本问题。请原谅我

我对C的无知

int main()

{

int * i;

* i = 1 //有时这可能会给我一个核心转储。

const char * str =" test" ;; //这似乎是一个有效的结构。

}


现在我明白当我们声明int * i时,我们只有一个指针

可能指向任何地方。如果我们尝试在其中存储内容,我们

可能会出现分段错误。但为什么const char * str =" test"

有效?我们不需要先为str分配内存吗?有人可以吗?b $ b请对此有所了解吗?


提前付款
I have a very elementary question about pointers. Please pardon me
for my ignorance of C

int main()
{
int* i;
*i = 1 //at times this may give me a core dump.
const char* str = "test"; //This seems to be a valid construct.
}

Now I understand that when we declare int* i, we just have a pointer
that may point to any place. If we try to store something in it, we
may get a segmentation fault. But why does const char* str = "test"
work? Do we not need to allocate memory for str first? Can someone
please shed some light on this?

Thanks in advance

http://c-faq.com/decl/strlitinit.html

http://c-faq.com/decl/strlitinit.html


On Sun,2007年7月15日20:14:33 -0700,Aarti< aa ********* @ gmail.com>

写道:
On Sun, 15 Jul 2007 20:14:33 -0700, Aarti <aa*********@gmail.com>
wrote:

>我有一个关于指针的非常基本的问题。请原谅我
因为我对C的无知


$

int * i;

* i = 1 //有时这可能会给我一个核心转储。

const char * str =" test" ;; //这似乎是一个有效的结构。
}
现在我明白当我们声明int * i时,我们只有一个可能指向任何地方的指针
。如果我们尝试在其中存储某些内容,我们可能会遇到分段错误。但为什么const char * str =" test"
>I have a very elementary question about pointers. Please pardon me
for my ignorance of C

int main()
{
int* i;
*i = 1 //at times this may give me a core dump.
const char* str = "test"; //This seems to be a valid construct.
}

Now I understand that when we declare int* i, we just have a pointer
that may point to any place. If we try to store something in it, we
may get a segmentation fault. But why does const char* str = "test"



无论是否出现分段错误,评估具有不确定值的任何变量的工作都会调用未定义的行为。

Whether you get a segmentation fault or not, the effort to evaluate
any variable with an indeterminate value invokes undefined behavior.


>工作?我们不需要先为str分配内存吗?有人可以对此有所了解吗?
>work? Do we not need to allocate memory for str first? Can someone
please shed some light on this?



" test"是一个字符串文字。当您的编译器构造您的

程序时,该文字是目标文件的一部分。指针str是

用该文字中第一个't'的地址初始化。


虽然技术上不精确,你可以想到它从概念上讲,

编译器为文字分配五个字节;使用值t,e,s,t和'\\'0'初始化那些
五个字节;并且

初始化str指向这个五字节区域的开头。


回答你的问题 - 用字符串初始化指针时

literal,你不需要为指针分配内存,因为

编译器为你做了。

删除del的电子邮件

"test" is a string literal. When your compiler constructs your
program, that literal is part of the object file. The pointer str is
initialized with the address of the first ''t'' in that literal.

While not technically precise, you can think of it conceptually as the
compiler allocating five bytes for the literal; initializing those
five bytes with the values ''t'', ''e'', ''s'', ''t'', and ''\0''; and
initializing str to point to the start of this five byte area.

To answer your question - when initializing a pointer with a string
literal, you do not need to allocate memory for the pointer because
the compiler did it for you.
Remove del for email


Aarti写道:
Aarti wrote:

我对指针有一个非常基本的问题。请原谅我

我对C的无知

int main()

{

int * i;

* i = 1 //有时这可能会给我一个核心转储。

const char * str =" test" ;; //这似乎是一个有效的结构。

}


现在我明白当我们声明int * i时,我们只有一个指针

可能指向任何地方。如果我们尝试在其中存储内容,我们

可能会出现分段错误。
I have a very elementary question about pointers. Please pardon me
for my ignorance of C

int main()
{
int* i;
*i = 1 //at times this may give me a core dump.
const char* str = "test"; //This seems to be a valid construct.
}

Now I understand that when we declare int* i, we just have a pointer
that may point to any place. If we try to store something in it, we
may get a segmentation fault.



不一定。分段错误确实发生在现代硬件和操作系统下,但不是在所有系统上。例如,在DOS下,不会发生
a分段错误。


但是在所有情况下,试图推测一个不确定的指针

根据C标准导致未定义的行为。

的结果是将一个任意数值存储到指针中(如上所述,上面是
),是实现定义的。在一个b $ b b的情况下,这可能是完全合理的,但在另一个情况下则是致命的。然而,它不是便携式的。

Not necessarily. Segmentation faults do occur under modern hardware
and operating systems, but not on all systems. Under DOS, for example,
a segmentation fault will not occur.

But in all cases, attempting to deference an indeterminate pointer
causes Undefined behaviour, as per the C Standard. Also the outcome of
storing an arbitrary numeric value into a pointer, (as you''ve done
above), is implementation defined. It may be perfectly sensible in one
situation, but fatal in another. It is however, not portable.


但为什么const char * str =" test"

工作?我们不需要先为str分配内存吗?有人可以给你这个吗?
But why does const char* str = "test"
work? Do we not need to allocate memory for str first? Can someone
please shed some light on this?



那是因为当实现编译你的程序时,它会自动为你的b
$ b字符串分配存储空间

并初始化str指向它的第一个元素。这是高级别的一部分。 C语言的本质。在汇编程序中,对于

例子,你必须手动为test指定存储空间。并且

使用此存储的起始

地址显式初始化指针(或寄存器)。


That''s because when the implementation compiled your program, it
automatically and anonymously allocated storage space for the string
and initialised str to point to the first element of it. This is a
part of the "high level" nature of languages like C. In assembler, for
example, you''d have to manually set aside storage for "test" and
explicitly initialise the pointer, (or register), with the start
address of this piece of storage.


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

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