练习5-9(K& R II) [英] Excercise 5-9 (K&R II)

查看:53
本文介绍了练习5-9(K& R II)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我解决一些困惑吗。

我试图了解指针和多维数组如何相互关联。

鉴于:


static char daytab [2] [13] = {

{0,31,28,31,30,31 ,30,31,31,30,31,30,31},

{0,31,29,31,30,31,30,31,31,30,31,30,31 }

};

和char * p;


如果p设置为指向第一行,(p = daytab [0])那么我的理解是p ++会指向daytab [0] [1]等等。我是什么?b $ b不确定是什么一旦p到达会发生什么行的结尾?

那么它是否会拾取第二行并执行相同的操作(即指向第2行中每个

列)?


并且假设有人希望指向ith。排在20

维数组中。如何做到这一点。


我相信答案会引发更多问题,但谢谢你们提前支付


Could someone help clear up some confusion for me.
I am trying to understand how pointers and multidimensional arrays
inter-relate.

Given:

static char daytab[2][13] = {
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
and char *p;

if p is set to point to the first row, ( p=daytab[0]) then my
understanding is that p++ will point to daytab[0][1] etc. What I am
uncertain about is what happens once p reaches the end of the row?
Does it then pick up the second row and do the same( ie point at each
column in row 2)?

And lets say that one wishes to point to the "ith" row in a 20
dimensional array. How does one do this.

I am sure the answers will spark more questions, but thank you in
advance.

推荐答案

mdh说:
mdh said:

如果p设置为指向第一行,(p = daytab [0])那么我的理解是p ++会指向daytab [0] [1]等等。我是什么?b $ b不确定是什么一旦p到达会发生什么排的结尾?
if p is set to point to the first row, ( p=daytab[0]) then my
understanding is that p++ will point to daytab[0][1] etc. What I am
uncertain about is what happens once p reaches the end of the row?



如果你有任何意义,你就停止递增它。

You stop incrementing it, if you have any sense.


然后它会接第二个行并执行相同的操作(即指向第2行中每个

列)?
Does it then pick up the second row and do the same( ie point at each
column in row 2)?



如果这就是你想要的,那就这样做:


p = daytab [1];

If that''s what you want, do this:

p = daytab[1];


并且假设有人希望指向ith。排在20

维数组中。怎么做到这一点。
And lets say that one wishes to point to the "ith" row in a 20
dimensional array. How does one do this.



通过'''ith'来定义你的意思排在20维数组'',因为

它不是很清楚。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。

Define what you mean by ''the "ith" row in a 20 dimensional array'', since
it isn''t very clear.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


4月10日上午9:51,Richard Heathfield写道:
On Apr 10, 9:51 am, Richard Heathfield wrote:

>
>



mdh说:

mdh said:


>
>

然后它是否接受第二行并执行相同操作(即指向第2行中每个

列)?
Does it then pick up the second row and do the same( ie point at each
column in row 2)?



如果这就是你想要的,那就这样做:


p = daytab [1];


If that''s what you want, do this:

p = daytab[1];



也许我可以通过询问这一点来澄清我的困惑(并且请

记住,我不像你那样精通C语言)。在这些情况下增加

指针会向下指向下一个

列指针。不是行。我试图得到一个概念图像,说明为什么这个

出现。显然很重要的是,最初需要建立指向特定行的

指针,然后才能增加

它的值以获得结果(但是一个人希望使用它们)

那一行。


Maybe I can clear up my confusion by asking this ( And please
remember, I am not as well versed in C as you are). Incrementing the
pointer under these circumstances advances down pointer to the next
"column" not "row". I am trying to get a conceptual image of why this
occurs. It is obviously significant that one needs to establish the
pointer to a particular row initially, and only then can one increment
it''s value to obtain the results(however one wishes to use them) for
that row.


>
>

并且假设有人希望指向ith。排在20

维数组中。怎么做到这一点。
And lets say that one wishes to point to the "ith" row in a 20
dimensional array. How does one do this.



用''ith'来定义你的意思排在20维数组''中,因为

它不是很清楚。


Define what you mean by ''the "ith" row in a 20 dimensional array'', since
it isn''t very clear.



说数组中的第15行arr [20] [20]。


不确定我是否清楚好吧。

Say the 15th row in an array arr[20][20].

Not quite sure if I am articulating this well.


mdh说:
mdh said:

也许我可以通过询问来澄清我的困惑这个(并且请

记住,我不像你那样精通C语言)。在这些情况下增加

指针会向下指向下一个

列指针。不是行。
Maybe I can clear up my confusion by asking this ( And please
remember, I am not as well versed in C as you are). Incrementing the
pointer under these circumstances advances down pointer to the next
"column" not "row".



这是一个二维数组,大小为5 x 2元素:


int N [2] [ 5] =

{

{6,17,42,39,22},

{37,26,90,21,14 }

};


这是一个指针:


int * p;


你现在可以这样做:


int i = 0;

p = N [0];

while(i ++< 5)

{

printf(" [%d]",* p ++);

}


这将打印:[6] [17] [42] [39] [22]


p现在点超出一个 N [0]数组。

指向它是合法的,主要是为了让这种循环方便你,

但取消引用是不合法的它或者进一步递增。


要到达阵列的第二行,你必须这样做:


p = N [1 ];

Here''s a two-dimensional array, 5 x 2 elements in size:

int N[2][5] =
{
{ 6, 17, 42, 39, 22 },
{ 37, 26, 90, 21, 14 }
};

Here''s a pointer:

int *p;

You can now do this:

int i = 0;
p = N[0];
while(i++ < 5)
{
printf(" [%d]", *p++);
}

This will print: [6] [17] [42] [39] [22]

p now points "one beyond" the N[0] array. It''s (just) legal for it to
point there, primarily to make this kind of looping convenient to you,
but it''s not legal to dereference it or increment it further.

To get to the second row of the array, you have to do this:

p = N[1];


我试图得到一个概念图像,说明为什么这个

出现。显然很重要的是,最初需要建立指向特定行的

指针,然后才能增加

它的值以获得结果(但是一个人希望使用它们来支付

那一行。
I am trying to get a conceptual image of why this
occurs. It is obviously significant that one needs to establish the
pointer to a particular row initially, and only then can one increment
it''s value to obtain the results(however one wishes to use them) for
that row.



对。具体来说,你对这种技术的困难是什么?

Right. What, specifically, is your difficulty with this technique?


并且假设有人希望指向ith排在20

维数组中。怎么做到这一点。
And lets say that one wishes to point to the "ith" row in a 20
dimensional array. How does one do this.


通过''the ith'来定义你的意思排成20维数组'',
因为它不是很清楚。


Define what you mean by ''the "ith" row in a 20 dimensional array'',
since it isn''t very clear.



说数组arr [20] [20]中的第15行。


Say the 15th row in an array arr[20][20].



嗯,这是一个二维数组,20 x 20,而不是20维
数组。但是,使用你的例子,你可以合法地做到这一点:


p = arr [i];


如果我在0范围内你可以像以前一样沿着arr [i]

移动p。当你到达行的末尾时,++ i然后再次绕回

,当然再次从p = arr [i]开始。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Well, that''s a two-dimensional array, 20 x 20, not a 20-dimensional
array. Using your example, though, you can legally do this:

p = arr[i];

provided that i is in the range 0 to 19. You can now move p along arr[i]
as before. When you get to the end of the row, ++i and then go round
again, starting with p = arr[i] again of course.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


这篇关于练习5-9(K&amp; R II)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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