的C指针运算片段 [英] C pointer arithmetic snippet

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

问题描述

我有我试图去code程序。它是从另一种语言翻译成C(他的名字是不是这里口语),和我想要了解它是如何工作,我慢慢地改写了code和简化其使用的所有漂亮的逻辑构造C有提供。

I have a program that I'm trying to decode. It is translated to C from another language (whose name is not spoken here), and as I want to understand how it works, I am slowly rewriting the code and simplifying it to use all the nice logical constructs C has to offer.

下面有点保留在我的code雨后春笋般冒出来,用不同的 X 的值

The following little bit keeps popping up in my code, with varying values of X and Y:

ptr[X]--;
while(ptr[X])
  {
    ptr[X]--;
    ptr += Y;
  }

PTR 的类型为的char * ,我真的不能对数组状态的假设在任何时候,因为它深深地植根于循环和依赖于输入和输出pretty。我可以成功地简化,为:

ptr is of type char *, and I can't really make assumptions about the state of the array at any point because it's pretty deeply embedded in loops and dependent on input and output. I can successfully "simplify" that to:

for(ptr[X]--; ptr[X]; ptr[X]--, ptr += Y);

但是,这很可怕。曾经如此稍好的是:

But that's just awful. Ever so slightly better is:

for(ptr[X]--; ptr[X]; ptr += Y) ptr[X]--;

我想知道是否有人能想出上述code,我将不胜AP preciate它更好的简化。这发生在不少于五个地方,并且损害我的简化和理解流量控制能力,因此,如果任何人都可以提供更consise /可读的版本,这将是真棒。如果任何人都可以只提供任何形式花哨的洞察code,那将是真棒太,虽然我基本上明白它做什么。

I want to know if anyone can come up with a better simplification of the above code, I would greatly appreciate it. This occurs in no less than five places, and is impairing my ability to simplify and understand the flow control, so if anyone can provide a more consise/readable version, that would be awesome. If anyone can just offer any sort of fancy insight into that code, that would be awesome too, although I basically understand what it does.

洞察code为特定的 X 和/或也有帮助。 往往是-2和2, X 之间通常为1,什么它的价值。

Insight into the code for a specific X and/or Y can also help. Y tends to be between -2 and 2, and X is usually 1, for what its worth.

推荐答案

PTR [X] 等同于 *(PTR + X),因此,我们可以按照如下重写:

ptr[X] is equivalent to *(ptr + X), so we can rewrite it as follows:

for((*(ptr + X))--; *(ptr + X); (*(ptr + X))--, ptr += Y);

现在有很多冗余的在这里,所以我们可以简化这:

Now there's a lot of redundancy here, so we can simplify this to:

char *ptr_plus_x = ptr + X;
for((*ptr_plus_x)--; *ptr_plus_x; (*ptr_plus_x)--, ptr_plus_x += Y);

然后我们就可以摆脱 ptr_plus_x 完全

ptr += X;
for((*ptr)--; *ptr; (*ptr)--, ptr += Y);

在英国,我们请访问偏移X,X + Y,X + 2Y,X + 3Y,...,递减每个存储单元,直到我们找到一个存储位置是0.但是,测试的内存位置0递减后总是发生,所以我们非常期待在与1的值序列中的第一个内存位置。一旦我们发现,我们把它减为0,并退出。

In English, we visit the memory locations at offsets X, X+Y, X+2Y, X+3Y, ..., decrementing each memory location, until we find a memory location that is 0. But, the test for 0 always occurs after the decrement, so we're really looking for the first memory location in that sequence with a value of 1. Once we find that, we decrement it to 0 and quit.

如果Y是1,则减小连续内存位置的字符串去向前,直至并包括第1,如果Y是-1,同样的事情发生,但是从偏移十向后搜索如果Y为0 ,发生一个无限循环。如果Y是任何其他值,搜索模式跳过各个条目。

If Y is 1, then we decrement a string of consecutive memory locations going forwards, up to and including the first 1. If Y is -1, the same thing happens, but searching backwards from offset X. If Y is 0, an infinite loop occurs. If Y is any other value, the search pattern skips various entries.

这不是一个很直观的功能,这样我就可以明白为什么你困惑。

It's not a very intuitive function, so I can see why you're confused.

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

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