数组地址算术 [英] Array address arithmetic

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

问题描述

检查下面的代码

void main()

{

char a [5] = {1,2,3, 4,5};

char * ptr;

ptr =(char *)(& a + 1);

printf(" ;%d",*(ptr-1));

}


该程序的输出为5


但我的预期是1.当我用表达式替换& a时,表达式为ptr =

(char *)(a + 1);我得到输出为1.在上面提到的表达式中,(& a + 1)

和(a + 1)之间的区别是什么。帮助人们混淆。

-

使用 http://www.talkaboutprogramming.com/group/comp.lang.c/

更多信息在 http://www.talkaboutprogramming.com/faq.html

解决方案

" vjay" < vi *********** @ gmail.comwrote in

news:3948456199d9a241db54852adf077d63

@ localhost.talkaboutprogramming.com:
< blockquote class =post_quotes>
检查下面的代码

void main()




int main (void)


{

char a [5] = {1,2,3,4,5};




不寻常将数字存储在字符中,但是如何以及如此。


char * ptr;

ptr =(char *)(& a + 1);



这里你取数组的地址a,然后加一个。这将

为您提供

数组中第五个元素之后的第一个字节的地址a。


printf("%d",*(ptr-1));



" ptr-1"给你留下a中第五个元素的地址。


该程序的输出是5


但我的预期是1.当我用表达式替换& a时表达

ptr =(char *)(a + 1);我得到输出为1.在上面提到的表达式中,

(& a + 1)和(a + 1)之间的区别是什么。帮助他们

令人困惑。




指针算术取决于指向的大小。类型。

大小的指向&输入& a是sizeof(char [5])。

-
$ b $bTomásóhéilidhe

On 12 ??9è?,????10ê±43·?,vjay < vijayanand ... @ gmail.comwrote:


检查下面的代码

void main()

{

char a [5] = {1,2,3,4,5};

char * ptr;

ptr =(char *)(& a + 1);

printf("%d",*(ptr-1));


}


程序的输出是5

但是我的预期是1.当我用表达式p中的a替换& a时=

(字符*)(a + 1);我得到输出为1.在上面提到的表达式中,(& a + 1)

和(a + 1)之间的区别是什么。帮助人们混淆。

-

消息发布时间:http://www.talkaboutprogramming.com/group/comp.lang.c/

更多信息:http:// www .talkaboutprogramming.com / faq.html



尝试以下方法:

int main()

{

char a [5] = {1,2,3,4,5};

char * ptr;

ptr =(char * )((char *)& a + 1);

printf("%d",*(ptr-1));

getchar();

}


vjayaécrit:


检查下面的代码/>
void main()

{

char a [5] = {1,2,3,4,5};

char * ptr;

ptr =(char *)(& a + 1);

printf("%d",*(ptr-1) ));

}


程序的输出是5

但我的预期是1。 WHE我用表达式替换& a,表达式为ptr =

(char *)(a + 1);我输出为1.在上面提到的表达式中,(& a + 1)

和(a + 1)之间的区别是什么。帮助他们混淆。



我会说这是因为& a是一个指向5个字符数组的指针,因此

(& a + 1)添加一个5个字符数组的大小。它给你带来一个

字符后的5.

然后ptr是一个指向char的指针,做(ptr-1)给你带来一个字符

回来,就在5.


我认为你的意思是(a + 1)。这样,它就会奏效。

数组的名称是指向其第一个元素的指针。所以a实际上是一个指向

char的指针,并且(a + 1)指向数组的第二个元素。


这都是由于指向char(a)的指针和指向chars数组的指针(& a")之间的区别。


希望我没错,
$ b $bNoé


Check the code below guys
void main()
{
char a[5] = {1,2,3,4,5};
char *ptr;
ptr = (char*)(&a + 1);
printf("%d",*(ptr-1));
}

The output of the program is 5

But what i expected was 1.When i replace &a with a in the expression ptr =
(char*)(a + 1); i get output as 1.what is the difference between (&a + 1)
and (a + 1) in the above mentioned expression.Help guys its confusing.

--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/
More information at http://www.talkaboutprogramming.com/faq.html

解决方案

"vjay" <vi***********@gmail.comwrote in
news:3948456199d9a241db54852adf077d63
@localhost.talkaboutprogramming.com:

Check the code below guys
void main()



int main(void)

{
char a[5] = {1,2,3,4,5};



Unusual to store numbers in a char, but how and ever.

char *ptr;
ptr = (char*)(&a + 1);


Here you take the address of the array "a", and add one to it. This will
give you the address of the first byte after the fifth element in the
array "a".

printf("%d",*(ptr-1));


"ptr-1" leaves you with the address of the fifth element in "a".

The output of the program is 5

But what i expected was 1.When i replace &a with a in the expression
ptr = (char*)(a + 1); i get output as 1.what is the difference between
(&a + 1) and (a + 1) in the above mentioned expression.Help guys its
confusing.



Pointer arithmetic depends on the size of the "pointed to" type. The
size of the "pointed to" type in &a is "sizeof(char[5])".
--
Tomás ó héilidhe


On 12??9è?, ????10ê±43·?, "vjay" <vijayanand...@gmail.comwrote:

Check the code below guys
void main()
{
char a[5] = {1,2,3,4,5};
char *ptr;
ptr = (char*)(&a + 1);
printf("%d",*(ptr-1));

}

The output of the program is 5

But what i expected was 1.When i replace &a with a in the expression ptr =
(char*)(a + 1); i get output as 1.what is the difference between (&a + 1)
and (a + 1) in the above mentioned expression.Help guys its confusing.

--
Message posted usinghttp://www.talkaboutprogramming.com/group/comp.lang.c/
More information athttp://www.talkaboutprogramming.com/faq.html

try the following:
int main()
{
char a[5] = {1,2,3,4,5};
char *ptr;
ptr = (char*)((char*)&a + 1);
printf("%d",*(ptr-1));
getchar();
}


vjay a écrit :

Check the code below guys
void main()
{
char a[5] = {1,2,3,4,5};
char *ptr;
ptr = (char*)(&a + 1);
printf("%d",*(ptr-1));
}

The output of the program is 5

But what i expected was 1.When i replace &a with a in the expression ptr =
(char*)(a + 1); i get output as 1.what is the difference between (&a + 1)
and (a + 1) in the above mentioned expression.Help guys its confusing.

I would say it is because &a is a pointer to an array of 5 chars, and
thus (&a + 1) add the size of one array of 5 chars. It brings you one
char after the 5.
Then ptr being a pointer to a char, doing (ptr-1) brings you one char
back, right on the 5.

I think you meant (a+1). This way, it would have worked. The name of the
array is a pointer to its first element. So "a" is really a pointer to a
char, and (a+1) points to the second element of your array.

It''s all due to the difference between a pointer to a char ("a"), and a
pointer to an array of chars ("&a").

Hope i''m not mistaken,
Noé


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

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