指针和数组 [英] pointer and array

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

问题描述




可以请任何人澄清C中的指针和数组吗?


如果我有:


int arr [10];


以下两个命令是相同的:arr和& arr [0]。


& arr怎么样?是不是一回事?

解决方案

Leon Brodskiy< iv ** @ rogers.com>潦草地写道:


可以请任何人澄清C中的指针和数组吗?
如果我有:
int arr [10];
以下两个命令是相同的:arr和& arr [0]。


他们是表达,而不是命令。但是,是的,它们是相同的,
价值背景下的


& arr怎么样?是不是一回事?




根本不是一回事。这是整个阵列的地址,而不是它的第一个元素。最实际的意思是这样的

地址以10 * sizeof(int)的形式递增,而不是以
sizeof(int)的形式递增。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \

\ ----------------------------- ---------------------------规则! -------- /


感谢您的回答。

我问,因为我在程序中找到了以下电话:


void f1(char inp [10])

{...}


无效main()

{...

char str [10];

....

f1 (& str);

....

}


我认为这是一个错误,这不是假设工作但它确实有效。

函数接收指向char的指针。我向函数发送一个指向char的指针,但仍然看起来像数组中的指针一样。那么,

就是这个案例str和& str一样吗?


谢谢。


" Joona我Palaste < PA ***** @ cc.helsinki.fi>在消息中写道

news:cq ********** @ oravannahka.helsinki.fi ...

Leon Brodskiy< iv ** @ rogers .COM>潦草地写道:


可以请任何人澄清C中的指针和数组吗?


< blockquote class =post_quotes>如果我有:


int arr [10];


以下两个命令将是相同的:arr和& arr [0]。



它们是表达式,而不是命令。但是,是的,在价值背景下,它们是同一个东西。

& arr怎么样?是不是一回事?



根本不是一回事。这是整个阵列的地址,而不是它的第一个元素。最实际的意思是这样的地址以10 * sizeof(int)的形式递增,而不是以sizeof(int)的形式递增。

-
/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \
\\ \\ ------------------------------------------------- -------规则! -------- /



Leon Brodskiy< iv ** @ rogers.com>潦草地写道:

感谢您的回答。
我问,因为我在程序中发现了以下调用:
void f1(char inp [10])
{...}
void main()


将此更改为int main(void)。

{...
char str [10];
...
f1(& str);
...
}
我认为这是一个bug这不应该工作,但它确实有效。
函数接收指向char的指针。我向函数发送一个指向char的指针,但仍然看起来像在数组中它是同一个指针。那么,
是这个案例str和& str相同吗?




该程序*不正确。你应该调用f1(str),而不是

(char *),它可能在您的实现下工作。但是试图访问
char数组指针(char(*)[])就像它是一个char指针一样会产生错误的结果,主要是因为我说的原因。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------- -----芬兰-------- \

\ ------------------------ --------------------------------规则! -------- /

冰淇淋销售不知何故导致溺水:两者都发生在夏季。

- Antti Voipio& Arto Wikla


Hi,

Could please anyone clarify about pointer and array in C?

If I have:

int arr[10];

The following two commands will be the same: arr and &arr[0].

What about &arr? Is it not the same thing?

解决方案

Leon Brodskiy <iv**@rogers.com> scribbled the following:

Hi, Could please anyone clarify about pointer and array in C? If I have: int arr[10]; The following two commands will be the same: arr and &arr[0].
They''re expressions, not commands. But yes, they''re the same thing,
in a value context.
What about &arr? Is it not the same thing?



Not the same thing at all. It''s the address of the entire array, not
its first element. The most practical meaning of this is that such
addresses increment in terms of 10*sizeof(int), not in terms of
sizeof(int).

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/


Thanks for your answer.
I''m asking because I have found in a program the following call:

void f1(char inp[10])
{...}

void main()
{...
char str[10];
....
f1(&str);
....
}

I tought this is a bug and this is not supposed to work but it does work.
Function receives pointer to char. I send to the function a pointer to a
pointer to char but still seems like in arrays it is the same pointer. So,
is this case str and &str the same?

Thanks.

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cq**********@oravannahka.helsinki.fi...

Leon Brodskiy <iv**@rogers.com> scribbled the following:

Hi,


Could please anyone clarify about pointer and array in C?


If I have:


int arr[10];


The following two commands will be the same: arr and &arr[0].



They''re expressions, not commands. But yes, they''re the same thing,
in a value context.

What about &arr? Is it not the same thing?



Not the same thing at all. It''s the address of the entire array, not
its first element. The most practical meaning of this is that such
addresses increment in terms of 10*sizeof(int), not in terms of
sizeof(int).

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/



Leon Brodskiy <iv**@rogers.com> scribbled the following:

Thanks for your answer.
I''m asking because I have found in a program the following call: void f1(char inp[10])
{...} void main()
Change this to int main(void).
{...
char str[10];
...
f1(&str);
...
} I tought this is a bug and this is not supposed to work but it does work.
Function receives pointer to char. I send to the function a pointer to a
pointer to char but still seems like in arrays it is the same pointer. So,
is this case str and &str the same?



That program is *NOT* correct. You are supposed to call f1(str), not
f1(&str). If the function f1 assigns its parameter to a char pointer
(char*), it might work under your implementation. But trying to access
the char array pointer (char(*)[]) as if it were a char pointer will
yield wrong results, mainly for the reasons I stated.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla


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

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