数组操作 [英] Array manipulation

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

问题描述

大家好,


我是刚刚开始用C编程的学生。我有以下

问题:如果我想转移数组的元素并插入一个新的

值作为最后一个元素,我使用这种语法(对于一个

元素大小的数组):


for(i = 0; i< size-1; i ++)

a [i] = a [i + 1]

a [size- 1] = newValue;


但事实证明这也有效:


int * p_i;

p_i =& a [1];

a [size-1] = newValue;


现在,指针方法对我来说似乎更有效率。但是因为我不是C的专家(刚开始上周),我想知道你对这个问题的想法 - 这是首选的方法吗?我是否正确

认为指针方法更有效率?非常欢迎任何想法,或者说
指针(没有双关语)。


感谢您的帮助

Angela

解决方案

2007年8月8日星期三12:09:05 +0000,perez.angela7写道:


大家好,


我是刚开始用C语言编程的学生。我有以下

问题:如果我想要移动数组的元素并插入一个新的

值作为最后一个元素,我使用这种语法(对于一个

元素大小的数组a):


for(i = 0; i< size-1; i ++)

a [i] = a [i + 1]

a [ size-1] = newValue;


但事实证明这也有效:


int * p_i;

p_i =& a [1];

a [size-1] = newValue;



是否缺少某些东西?分配给p_i并不神奇地

写给a。


[snip]

-

Army1987(将NOSPAM替换为电子邮件)

没有人通过辞职赢得比赛。 - S. Tartakower


8月8日下午1:21,Army1987< army1 ... @ NOSPAM.itwrote:
< blockquote class =post_quotes>
2007年8月8日星期三12:09:05 +0000,perez.angela7写道:


大家好,


我是刚开始用C语言编程的学生。我有以下

问题:如果我想转移数组的元素并插入一个新的

值作为最后一个元素,我使用这种语法(对于一个

元素大小的数组a):


for(i = 0; i< size-1; i ++)

a [i] = a [i + 1]

a [size-1] = newValue;


但事实证明这也适用:


int * p_i;

p_i =& a [1];

a [size-1] = newValue;



有什么遗漏?分配给p_i并不神奇地

写给a。


[snip]

-

Army1987(将NOSPAM替换为电子邮件)

没有人通过辞职赢得比赛。 - S. Tartakower



道歉,最后一行应该是p_i [size-1] = newValue;

p_i是转移版本a。

谢谢


pe *********** @ googlemail.com 写道:


大家好,


我是刚开始用C语言编程的学生。我有以下

问题:如果我想移动数组的元素并插入新的

值作为最后一个元素,我使用这种语法(对于一个

元素大小的数组):


for(i = 0; i< size-1; i ++)

a [i] = a [i + 1]

a [size-1] = newValue;


但事实证明这也有效:


int * p_i;

p_i =& a [1];

a [size-1] = newValue;


现在,指针方法对我来说似乎更有效率。但是因为我不是C的专家(刚开始上周),我想知道你对这个问题的想法 - 这是首选的方法吗?我是否正确

认为指针方法更有效率?任何想法,或者说b
指针(没有双关语)都非常欢迎。



我曾经认为,当我在大约1989年左右开始使用C并且从那以后开始和关闭时使用它是
。来自一个占主导地位的汇编程序

背景我曾经认为指针更有效率。我真的不知道b $ b不再相信它有任何东西 - 优化编译器

通常做得比我们人类能够接近的好。另外,

从不低估可读性的价值。


但是,任何不理解的人


虽然(* d ++ = * s ++);


应该可能不是真正的代码基础编程,因为对我来说,

几乎总结了大部分内容你需要知道关于

指针和指向的内容。


就你的代码而言,我会坚持使用数组符号。


a [i] = a [i + 1]


,IMO,比


更干净* p_a = *(p_a + 1)


因为第二个会带来各种各样的警钟,关于1是1还是
字节或其中一个元素p_a指向等等......不是每个人

程序员c知道这些东西就像常客一样。


Hi all,

I''m a student who just started programming in C. I have the following
question: if I want to shift the elements of an array and insert a new
value as the last element, I use this syntax (for an array a of
elements size):

for (i=0; i < size-1; i++)
a[i]=a[i+1]
a[size-1]=newValue;

But it turns out that this works too:

int *p_i;
p_i=&a[1];
a[size-1]=newValue;

Now, the pointer method seems more efficient to me. But because I''m
not an expert on C (just started last week), I would like to know your
thoughts on this - which is the preferred method? am i right in
thinking the pointer method is more efficient? Any thought, or
pointers (no pun) very welcome.

Thanks for your help
Angela

解决方案

On Wed, 08 Aug 2007 12:09:05 +0000, perez.angela7 wrote:

Hi all,

I''m a student who just started programming in C. I have the following
question: if I want to shift the elements of an array and insert a new
value as the last element, I use this syntax (for an array a of
elements size):

for (i=0; i < size-1; i++)
a[i]=a[i+1]
a[size-1]=newValue;

But it turns out that this works too:

int *p_i;
p_i=&a[1];
a[size-1]=newValue;

Is there something missing? Assigning to p_i doesn''t magically
write to a.

[snip]
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower


On Aug 8, 1:21 pm, Army1987 <army1...@NOSPAM.itwrote:

On Wed, 08 Aug 2007 12:09:05 +0000, perez.angela7 wrote:

Hi all,

I''m a student who just started programming in C. I have the following
question: if I want to shift the elements of an array and insert a new
value as the last element, I use this syntax (for an array a of
elements size):

for (i=0; i < size-1; i++)
a[i]=a[i+1]
a[size-1]=newValue;

But it turns out that this works too:

int *p_i;
p_i=&a[1];
a[size-1]=newValue;


Is there something missing? Assigning to p_i doesn''t magically
write to a.

[snip]
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

My apologies, last line should be p_i[size-1]=newValue;
p_i is the shifted version of a.
Thanks


pe***********@googlemail.com writes:

Hi all,

I''m a student who just started programming in C. I have the following
question: if I want to shift the elements of an array and insert a new
value as the last element, I use this syntax (for an array a of
elements size):

for (i=0; i < size-1; i++)
a[i]=a[i+1]
a[size-1]=newValue;

But it turns out that this works too:

int *p_i;
p_i=&a[1];
a[size-1]=newValue;

Now, the pointer method seems more efficient to me. But because I''m
not an expert on C (just started last week), I would like to know your
thoughts on this - which is the preferred method? am i right in
thinking the pointer method is more efficient? Any thought, or
pointers (no pun) very welcome.

I used to think that back when I started C in about 1989 or so and have
used it on and off since then. Coming from a predominantly assembler
background I used to assume that pointers were more efficient. I really
don''t believe there is anything in it anymore - optimizing compilers
often do a better job than us mere humans can ever come close to. Also,
never underestimate the value of readability.

However, anyone who doesn''t understand

while(*d++=*s++);

Should probably not be programming on real code bases since, for me, it
pretty much summarises most of what you need to know about regarding
pointers and contents pointed to.

As far as your code goes, I would stick with the array notation.

a[i]=a[i+1]

is, IMO, much cleaner than

*p_a=*(p_a+1)

since the second brings up all sorts of alarm bells about whether 1 is 1
byte or size of one of the elements p_a points to etc .... not everyone
who programs c knows this stuff as off pat as the regulars here.


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

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