以更高的索引在C中启动数组

时间:2014-05-03 18:07:01

标签: c arrays

我的数组中包含的数字包含8x8 LED显示屏的二进制坐标。

现在我想拥有相同的角色。这很简单,因为索引是数字本身的字符。

因此索引1将显示1,索引2将显示2,依此类推......

我希望有一个从索引号65开始的数组。

这个想法是传递一个字符数组,并为每个字符获取带有atoi的等效ASCII数,然后遍历数组以选择正确的数据。

我怎么能从Index 65开始?

我认为每次从atoi获取值时减去65都可以,但不知何故感觉不对。

这样我可以说A等于0,B等于1,依此类推。

#define ZERO {0,1,1,0},{1,0,0,1},{1,0,0,1},{1,0,0,1},{1,0,0,1},{0,1,1,0}
#define ONE {0,0,1,0},{0,1,1,0},{0,0,1,0},{0,0,1,0},{0,0,1,0},{0,1,1,1}
#define TWO {0,1,1,0},{1,0,0,1},{0,0,0,1},{0,0,1,0},{0,1,0,0},{1,1,1,1}
#define THREE {1,1,1,1},{0,0,1,0},{0,1,0,0},{0,0,1,0},{1,0,0,1},{0,1,1,1}
#define FOUR {0,0,0,1},{0,0,1,1},{0,1,0,1},{1,1,1,1},{0,0,0,1},{0,0,0,1}
#define FIVE {1,1,1,1},{1,0,0,0},{1,1,1,0},{0,0,0,1},{1,0,0,1},{0,1,1,0}
#define SIX {0,0,1,1},{0,1,0,0},{1,0,0,0},{1,1,1,1},{1,0,0,1},{0,1,1,0}
#define SEVEN {1,1,1,1},{0,0,0,1},{0,0,1,0},{0,1,0,0},{1,0,0,0},{1,0,0,0}
#define EIGHT {0,1,1,0},{1,0,0,1},{0,1,1,0},{1,0,0,1},{1,0,0,1},{0,1,1,0}
#define NINE {0,1,1,0},{1,0,0,1},{0,1,1,1},{0,0,0,1},{0,0,1,0},{0,1,0,0}

byte numbers[10][6][4]={
  {ZERO},
  {ONE},
  {TWO},
  {THREE},
  {FOUR},
  {FIVE},
  {SIX},
  {SEVEN},
  {EIGHT},
  {NINE},
};

4 个答案:

答案 0 :(得分:3)

不,你不能用C改变起始数组索引。它总是为零。

如果在每次访问中手动减去太多麻烦,你可以制作一个宏或函数来自动执行操作。

例如:

byte** map(const char c)
{
    return numbers[c-'0'];
}

答案 1 :(得分:3)

您可以提供索引来跳过某些数组成员,如下所示:

byte numbersAndLetters[][6][4]={
  {ZERO},
  {ONE},
  {TWO},
  {THREE},
  {FOUR},
  {FIVE},
  {SIX},
  {SEVEN},
  {EIGHT},
  {NINE},
['A'] = {LETTER_A}, // Instead of hardcoding 65, use 'A'
  {LETTER_B}
  ...
};

这种形式的初始化程序允许您跳过一些元素,使它们默认初始化。

注意:将#define用于仅可用作较大语法结构一部分的项目通常不是一个好主意。例如,您定义的十个数字不能在数组初始值设定项之外使用,并且它们需要围绕它们的花括号。您应该内联定义它们,并为清晰起见添加注释:

byte numbers[][6][4]={
  {{0,1,1,0},{1,0,0,1},{1,0,0,1},{1,0,0,1},{1,0,0,1},{0,1,1,0}}, // Zero
  {{0,0,1,0},{0,1,1,0},{0,0,1,0},{0,0,1,0},{0,0,1,0},{0,1,1,1}}, // One
  ..
}

答案 2 :(得分:0)

不要这样做。 (这是对您的请求的常见错误解决方案。)

您可能会参考一些建议您可以执行类似

的操作
    int  a[10]={5,6,7,8,9,10,11,12,13,14};
    int *pA=a-5;

现在pA[5]将引用数组的第一个元素。

不要这样做。

该标准允许实现pA的生成会因创建无效指针而创建陷阱。指针不需要在对象外有效 指向该对象末尾的一个元素。

在许多情况下,这种类型的代码都可以使用,但并非如此。应该避免。

参考C99 6.5.6关于加法运算的加法运算符,其中加法运算符之一是指针:“如果指针操作数和结果都指向同一数组对象的元素,或者指向最后一个数组对象的元素,评估不应产生溢出,否则,行为是未定义的。“

答案 3 :(得分:0)

#define NUL  {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}
#define ZERO {{0,1,1,0},{1,0,0,1},{1,0,0,1},{1,0,0,1},{1,0,0,1},{0,1,1,0}}
#define ONE {{0,0,1,0},{0,1,1,0},{0,0,1,0},{0,0,1,0},{0,0,1,0},{0,1,1,1}}
#define TWO {{0,1,1,0},{1,0,0,1},{0,0,0,1},{0,0,1,0},{0,1,0,0},{1,1,1,1}}
#define THREE {{1,1,1,1},{0,0,1,0},{0,1,0,0},{0,0,1,0},{1,0,0,1},{0,1,1,1}}
#define FOUR {{0,0,0,1},{0,0,1,1},{0,1,0,1},{1,1,1,1},{0,0,0,1},{0,0,0,1}}
#define FIVE {{1,1,1,1},{1,0,0,0},{1,1,1,0},{0,0,0,1},{1,0,0,1},{0,1,1,0}}
#define SIX {{0,0,1,1},{0,1,0,0},{1,0,0,0},{1,1,1,1},{1,0,0,1},{0,1,1,0}}
#define SEVEN {{1,1,1,1},{0,0,0,1},{0,0,1,0},{0,1,0,0},{1,0,0,0},{1,0,0,0}}
#define EIGHT {{0,1,1,0},{1,0,0,1},{0,1,1,0},{1,0,0,1},{1,0,0,1},{0,1,1,0}}
#define NINE {{0,1,1,0},{1,0,0,1},{0,1,1,1},{0,0,0,1},{0,0,1,0},{0,1,0,0}}
#define __A  {{0,1,1,0},{1,0,0,1},{1,1,1,1},{1,0,0,1},{1,0,0,0},{1,0,0,1}}
/* ...finish out the entire character set if you wish */

byte numbers[256][6][4]=
  {
  NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, //0-15
  NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, //16-31
  NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, //32-47
  ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, NULL, NULL, NULL, NULL, NULL, NULL, //48-63
  __A, __B, __C, __D, __E, __F, __G, __H, __I, __J, __K, __L, __M, __N, __O, //64-79
  /* ...finish out the entire character set if you wish */
  };