为什么C支持负数组索引? [英] Why does C support negative array indices?

查看:130
本文介绍了为什么C支持负数组索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这个后,很显然,C支持负指数。

From this post in SO, it is clear that C supports negative indices.

1)为什么要支持这样一个潜在的内存违反程序?

1.) Why support such a potential memory violation in a program?

2)不应该编译器至少抛出一个负折射率警告? (使用GCC AM)

2.) Shouldn't the compiler throw a Negative Index warning at least? (am using GCC)

3。)或者是这个计算中运行时完成?

3.) Or is this calculation done in runtime?

编辑1:结果
任何人都可以暗示其用途?结果
修改2:结果
对于3)在 []数组/指针使用循环的计数器显示指数运行时间的计算。

EDIT 1:
Can anybody hint at its uses?
EDIT 2:
for 3.) Using counters of loops in [] of arrays/pointers indicates Run-time Calculation of Indices.

推荐答案

下面是使用的例子。

这是无限冲激响应滤波器,从最近的previous输出值部分地计算的。通常情况下,将有输入值的某些阵列和阵列,其中输出值要被放置。如果当前输出元素为y I ,则y I 为y I 可以计算= A 0 •点¯x I + A 1 运算•X I-1 + A 2 •是<子> I-1 + A 3 •是<子> I-2 。

An Infinite Impulse Response filter is calculated partially from recent previous output values. Typically, there will be some array of input values and an array where output values are to be placed. If the current output element is yi, then yi may be calculated as yi = a0•xi + a1•xi–1 +a2•yi–1 +a3•yi–2.

写code这种自然的方式是这样的:

A natural way to write code for this is something like:

void IIR(float *x, float *y, size_t n)
{
    for (i = 0; i < n; ++i)
        y[i] = a0*x[i] + a1*x[i-1] + a2*y[i-1] + a3*y[i-2];
}

注意,当 I 为零, Y [I-1] ÿ [I-2] 有负指数。在这种情况下,主叫方负责创建的阵列,设置初始两个要素为起动值为输出(通常为零或从previous缓冲器中保持过的值),并通过一个指针,其中第一个新值将被写入。因此,这个程序中, IRR ,通常收到一个指针到一个数组的中间,并使用负指数,解决一些元素。

Observe that when i is zero, y[i-1] and y[i-2] have negative indices. In this case, the caller is responsible for creating an array, setting the initial two elements to "starter values" for the output (often either zero or values held over from a previous buffer), and passing a pointer to where the first new value is to be written. Thus, this routine, IRR, normally receives a pointer into the middle of an array and uses negative indices to address some elements.

这篇关于为什么C支持负数组索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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