在C中使用char作为数组索引?
我有这样的代码:
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;
}
输出:
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
我不明白为什么会给出此错误消息。 我应该有一个像这样的向量:
vector['a'] = 0, vector['b'] = 1, .., vector['z'] = 26, vector['A'] = 27, vector['B'] = 28, .., vector['Z'] = 52
我知道我有这个向量,但错误随之而来。 如何解决这个问题?
I have this code:
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;
}
Output :
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
I don't understand why is giving this error message.
I should have a vector like this :
vector['a'] = 0, vector['b'] = 1, .., vector['z'] = 26, vector['A'] = 27, vector['B'] = 28, .., vector['Z'] = 52
I understand that I have this vector but the error came with it.
How solve this problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为“Z”不等于“z”。
“Z”等于 90,而您的向量只有 52 个元素。你的最高指数是51,所以你基本上已经出界了!
例如,当您这样做时,
这就是您的第一次迭代的样子:
Because 'Z' is not equal to 'z'.
'Z' is equal to 90 and your vector only has 52 elements. Your highest index is 51 so you are basically going out of bounds!
For example when you are doing this
This is what your first iteration looks like:
不做你认为它做的事。
'A'
表示字符A
的 ASCII 值是 65;您的索引立即超出范围。Doesn't do what you think it does.
'A'
means the ASCII value for the characterA
which is 65; your index is out of bounds immediately.因此,
vector
的可访问索引为 0 到 51。但是 -向量上没有这样的索引,并且结果越界异常。
So, the accessible indexes of
vector
are 0 to 51. But -There are no such indexes on the vector and result out of bounds exception.
'a' 是 97。你的数组只有 52 长。您实际上已经用第一个可执行语句破坏了数组。
我怀疑你想说的是类似的话
(虽然我不太确定“某事”可能是什么。)
'a' is 97. Your array is only 52 long. You've blown the array with virtually your first executable statement.
I suspect you meant to say something like
(Though I'm not quite sure what "something" might be.)
首先,附注:您的第一个 for 循环还需要包含“z”。因此:
在 C 中,数组不是从任何值到任何值的映射。它们只是一个数组(或一个连续的列表),从索引 0 开始。当你说的时候,
意味着你有一个大小为 52 的数组。数组的有效索引是 0 到 51。但是,当你写一个像
这样的字符时'a'
,它实际上只是一个数字,是字符a
的ascii代码(即0x61,即十六进制的61)。您使用的最高索引是'z'
,即 122。因此您的数组需要使索引 122 有效,因此大小必须至少 123。因此您的代码变成这样:
另外,你的问题存在矛盾。你首先说你有:
然后你说:
如果你想要从 0 而不是 1 开始的
vector['a']
,你应该将你的公式从vector[i] = i - ' a' + 1;
到向量[i] = i - 'a';
First, a side note: your first for loop needs to include 'z' also. Therefore:
In C, arrays are not maps from any value to any value. They are just an array (or a contiguous list), starting from index 0. When you say
means you have an array of size 52. Valid indices to the array are 0 to 51. However, when you write a character like
'a'
, it really is just a number which is the ascii code of charactera
(which is 0x61, that is 61 hex). The highest index you are using is'z'
which is 122. Therefore your array needs to have index 122 valid, therefore must have a size of at least 123.So your code becomes like this:
Also, there is a contradiction in your question. You say first that you have:
then you say:
if you want to have
vector['a']
starting from 0 rather than 1, you should change your formula fromvector[i] = i - 'a' + 1;
tovector[i] = i - 'a';