指针声明/数组定义 [英] Pointer Declaration/Array definition

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

问题描述



为什么这个声明会给出不确定的结果:


file1:extern char * p;

file2:char p [10];


假设p已经初始化,现在正在访问p [i] ......


Why does this declaration give undefined result:

file1: extern char * p;
file2: char p[10];

Let''s assume p has been initialized, now accessing p[i]...

推荐答案

<ur**@ur8x.com>在消息中写道

新闻:cg ********** @ news-int2.gatech.edu ...

|

|为什么这个声明会给出不确定的结果:

|

| file1:extern char * p;

分配内存以存储指针,稍后可以更改

以引用任何内存位置。

| file2:char p [10];

在固定地址为10个字符分配内存。

当使用变量p时,数组是隐式的

转换为指向数组第一个元素的指针。

|

|我们假设p已初始化,现在正在访问p [i] ...


如果包含

file1的代码包含该怎么办?声明如下:

p = NULL;

hth,

Ivan

-
< a rel =nofollowhref =http://ivan.vecerina.com/contacttarget =_ blank> http://ivan.vecerina.com/contact
<ur**@ur8x.com> wrote in message
news:cg**********@news-int2.gatech.edu...
|
| Why does this declaration give undefined result:
|
| file1: extern char * p;
Allocates memory to store a pointer, which may later be changed
to refer to any memory location.
| file2: char p[10];
Allocates memory for 10 characters at a fixed address.
When the variable p is used, the array is implicitly
converted to a pointer to the first element of the array.
|
| Let''s assume p has been initialized, now accessing p[i]...

What is supposed to happen if code that includes
file1 contains a statement such as:
p = NULL;
hth,
Ivan
--
http://ivan.vecerina.com/contact


ur**@ur8x.com 写道:

为什么这个声明给出了不确定的结果:

file1:extern char * p;
file2:char p [10];

我们假设p已经初始化,现在访问p [i] ...

Why does this declaration give undefined result:

file1: extern char * p;
file2: char p[10];

Let''s assume p has been initialized, now accessing p[i]...




file1认为p是指向char的指针。 file2认为p

是一个包含10个字符的数组。这就是extern char * p;的原因。应该

在file1和file2中包含的头文件中,然后编译器会抱怨。这遵循简单的原则,即头文件用于导出其他模块需要知道的其他内容。


-

fix(vb。):1。纸张覆盖,模糊,隐藏在公众视野之外; 2.

以一种产生意想不到的后果的方式来解决,比原来的问题更糟糕。用法:Windows ME

修复了Windows 98 SE的许多缺点。 - Hutchison



file1 thinks that p is a pointer to a char. file2 thinks that p
is an array of 10 chars. This is why the "extern char *p;" should
be in a header file that is included in both file1 and file2, and
then the compiler would complain. This follows the simple
principle that header files are used to export things other
modules need to know about.

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison


ur**@ur8x.com 写道:
ur**@ur8x.com wrote:
为什么这个声明会给出未定义的结果:
file1:extern char * p;
file2:char p [10];
Why does this declaration give undefined result: file1: extern char * p;
file2: char p[10];



其他人已经解释了为什么这不起作用,即因为

a char数组和一个char指针是非常不同的东西,

不多共同。我猜你的困惑来自

这个事实,在某些条件下,数组的名称被处理为

,好像它是一个指针(第一个元素) )

数组,例如在


char p [] =" hello word" ;;

char * pp = p;


但这只发生在数组用于值上下文时,即
,即如果它被用作具有值的话。然后,只有这样,它才会意味着(通常称为它衰变成)地址

数组的第一个元素。


但是在


extern char * p;


''p''没用过在价值背景中(编译器甚至不知道

在其他地方定义了一个名为''p'的字符数组,因为

'在不同的源文件中),所以腐烂到指针,规则

不参与。

问候,Jens

-

\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de

\ __________________________ http://www.toerring.de



Other people already explained why this won''t work, i.e. because
a char array and a char pointer are very different things, having
not much in common. I guess your confusion is coming from the
fact that under certain conditions the name of an array is dealt
with as if it would be a pointer to (the first element of) the
array, e.g. in

char p[ ] = "hello word";
char *pp = p;

But this only happens when the array is used in "value context",
i.e. if it is used as if it had a value. Then, and only then, it
is taken to mean (often called "it decays into") the address of
the first element of the array.

But in

extern char *p;

''p'' isn''t used in "value context" (the compiler even doesn''t know
that somewhere else an array of chars named ''p'' was defined since
that''s in a different source file), so the "decay to pointer" rule
doesn''t get involved.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


这篇关于指针声明/数组定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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