在C中使用char作为数组索引?

Val*_*lva 0 c c++ arrays vector

我有这个代码:

int main(){

  char vector[52];
  char i;

  /* initialize the vector */
  for (i ='a'; i < 'z'; i++){
    vector[i] = i - 'a' + 1;
  } 
  // vector is like vector['a'] = 1, vector['b'] = 2 .. vector['z'] = 26


  for (i ='A'; i <= 'Z'; i++){
    vector[i] = i - 'A' + 27;
  }
  // vector is like vector['A'] = 27, vector['B'] = 28 .. vector['z'] = 52

  for (i ='a'; i <= 'z'; i++){
    printf("letter %c : %d \n", i, vector[i]);
  } 

  for (i ='A'; i <= 'Z'; i++){
    printf("letter %c : %d \n", i, vector[i]);
  }

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

letter a : 1 
letter b : 2 
letter c : 3 
letter d : 4 
letter e : 5 
letter f : 6 
letter g : 7 
letter h : 8 
letter i : 9 
letter j : 10 
letter k : 11 
letter l : 12 
letter m : 13 
letter n : 14 
letter o : 15 
letter p : 16 
letter q : 17 
letter r : 18 
letter s : 19 
letter t : 20 
letter u : 21 
letter v : 22 
letter w : 23 
letter x : 24 
letter y : 25 
letter z : 0 
letter A : 27 
letter B : 28 
letter C : 29 
letter D : 30 
letter E : 31 
letter F : 32 
letter G : 33 
letter H : 34 
letter I : 35 
letter J : 36 
letter K : 37 
letter L : 38 
letter M : 39 
letter N : 40 
letter O : 41 
letter P : 42 
letter Q : 43 
letter R : 44 
letter S : 45 
letter T : 46 
letter U : 47 
letter V : 48 
letter W : 49 
letter X : 50 
letter Y : 51 
letter Z : 52 
*** stack smashing detected ***: ./a.out terminated
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(__fortify_fail+0x50)[0xc25df0]
/lib/i386-linux-gnu/libc.so.6(+0xe5d9a)[0xc25d9a]
./a.out[0x8048547]
[0x343332]
======= Memory map: ========
00110000-0012a000 r-xp 00000000 08:01 131939     /lib/i386-linux-gnu/libgcc_s.so.1
0012a000-0012b000 r--p 00019000 08:01 131939     /lib/i386-linux-gnu/libgcc_s.so.1
0012b000-0012c000 rw-p 0001a000 08:01 131939     /lib/i386-linux-gnu/libgcc_s.so.1
00a19000-00a1a000 r-xp 00000000 00:00 0          [vdso]
00aa4000-00ac0000 r-xp 00000000 08:01 131898     /lib/i386-linux-gnu/ld-2.13.so
00ac0000-00ac1000 r--p 0001b000 08:01 131898     /lib/i386-linux-gnu/ld-2.13.so
00ac1000-00ac2000 rw-p 0001c000 08:01 131898     /lib/i386-linux-gnu/ld-2.13.so
00b40000-00c9a000 r-xp 00000000 08:01 131911     /lib/i386-linux-gnu/libc-2.13.so
00c9a000-00c9b000 ---p 0015a000 08:01 131911     /lib/i386-linux-gnu/libc-2.13.so
00c9b000-00c9d000 r--p 0015a000 08:01 131911     /lib/i386-linux-gnu/libc-2.13.so
00c9d000-00c9e000 rw-p 0015c000 08:01 131911     /lib/i386-linux-gnu/libc-2.13.so
00c9e000-00ca1000 rw-p 00000000 00:00 0 
08048000-08049000 r-xp 00000000 08:01 40062      /home/valter/Documents/Complexidade/recursivo/a.out
08049000-0804a000 r--p 00000000 08:01 40062      /home/valter/Documents/Complexidade/recursivo/a.out
0804a000-0804b000 rw-p 00001000 08:01 40062      /home/valter/Documents/Complexidade/recursivo/a.out
0846f000-08490000 rw-p 00000000 00:00 0          [heap]
b7772000-b7773000 rw-p 00000000 00:00 0 
b7782000-b7785000 rw-p 00000000 00:00 0 
bfa50000-bfa71000 rw-p 00000000 00:00 0          [stack]
Aborted
Run Code Online (Sandbox Code Playgroud)

我不明白为什么要给出这个错误信息.我应该有这样的矢量:

vector['a'] = 0, vector['b'] = 1,  .., vector['z'] = 26, vector['A'] = 27, vector['B'] = 28, .., vector['Z'] = 52
Run Code Online (Sandbox Code Playgroud)

我知道我有这个向量,但错误随之而来.怎么解决这个问题?

Pep*_*epe 6

因为'Z'不等于'z'.

'Z'等于90,你的矢量只有52个元素.你的最高指数是51,所以你基本上超出了界限!

例如,当你这样做

  for (i ='A'; i <= 'Z'; i++)
  {
    vector[i] = i - 'A' + 27;
  }
Run Code Online (Sandbox Code Playgroud)

这是您的第一次迭代:

vector[65] = 65 - 65 + 27; // <-- Wrong index !
Run Code Online (Sandbox Code Playgroud)