C:锯齿状数组的问题
我有以下代码:
int *edges[500];
char arr[] = {'c','d'};
edges[0] = arr;
printf("%c - %c", edges[0][0],edges[0][1]);
我想要显示的是 c - d
但实际显示的是 c -
正如您在上面看到的,第一个元素正在显示,但第二个元素没有显示。
为什么不显示数组的第二个元素?
I have the following code :
int *edges[500];
char arr[] = {'c','d'};
edges[0] = arr;
printf("%c - %c", edges[0][0],edges[0][1]);
What I want displayed is c - d
but what is actually being displayed is c -
So as you can see above, the first element is being displayed but not the second one.
Why isn't the second element of the array not being displayed ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
由于
int
和char
具有不同的大小,因此您应该尝试char *edges[500]
Since an
int
and achar
have different sizes, you should trychar *edges[500]
类型不匹配。
edges
是一个包含 500 个 int 指针的数组,您将一个指向两个字符的指针分配给它的第一个元素。Type mismatch.
edges
is an array of 500 pointers to int and you are assigning a pointer to two characters to its first element.嗯,这条
消息可能与此有关。
在 C 中,
'c'
是一个(小)整数。您将它们存储为arr
中的char
。但是,通过将arr
作为edges[0]
(即int *
)访问,您实际上正在检索(在大多数平台上)arr [0]
、arr[1]
、arr[2]
和arr[3]
作为整数。Well, the
message might have something to do with it.
In C,
'c'
is a (small) integer. You store those aschar
s inarr
. However, by accessingarr
asedges[0]
which is anint *
, you are actually retrieving (on most platforms)arr[0]
,arr[1]
,arr[2]
andarr[3]
as an integer.edges[0]
被解释为包含元素 {(int)'cd' , 0} 的 int 数组(实际上,第二个元素可以包含任何垃圾)。带有说明符 %c 的 printf 获取
edges[0][0]
的第一个字节(即“c”)和edges[0][1]
的第一个字节(发生了为 0)。将
edges
的类型更改为char* Edgeds[500]
。edges[0]
is interpreted as int array with elements {(int)'cd' , 0} (actually, the second element can contain any junk).printf with specifier %c takes first byte of
edges[0][0]
(i.e. 'c') and first byte ofedges[0][1]
(which happened to be 0).change type of
edges
tochar* edgeds[500]
.第 3
行赋值中的不兼容类型:
arr
是char *
,edges[0]
是int *
。发生的情况是这样的:
arr[0]是'c',arr[1]是'd'
忽略类型兼容性,edges[0]指向
int
“c”和“d”(可能还有两个未指定的字符)所在的地址。Edges[0][0] 是 Edges[0] 中的第一个整数:它是 'c'、'd'(以及可能的另外两个未指定字符)的混合。该值被转换为无符号字符,产生打印的“c”。
Edges[0][1] 指向“c”、“d”(可能还有另外两个未指定字符)混合之后的整数。该内存位置尚未初始化,它很可能超出了您的进程可以访问的范围。
如果打印一个 int 而不是 2 个字符,
您将看到“c”、“d”(可能还有两个未指定的字符)的混合。
最好的办法就是正确输入类型。
line 3
incompatible types in assignment:
arr
is achar *
andedges[0]
is aint *
.What happens is this:
arr[0] is 'c' and arr[1] is 'd'
Ignoring the type compatibility, edges[0] point to the
int
at the address where the 'c' and 'd' (and possibly two more unspecified characters) are.edges[0][0] is the first integer in edges[0]: that's the mix of 'c', 'd' (and possible two more unspecified characters). That value is converted to unsigned char, yielding 'c' which gets printed.
edges[0][1] points to the integer right after the mix of 'c', 'd' (and possibly two more unspecified characters). That memory location has not been initialized and it may well be outside the range your process can access.
if you print an int instead of 2 chars
you will see the mix of 'c', 'd' (and possibly two unspecified characters).
Best thing to do is get your types right.
edges
是一个指向 int 的指针数组。表达式edges[0] 产生一个指向int 的指针,而不是指向char 的指针。将其视为 char 数组会导致计算出错误的字节偏移量,从而导致显示意外的内存块的值,而不是保存 char 值“d”的内存。数组偏移量根据指向的类型的大小计算偏移量(以字节为单位):
difference
的值将为sizeof(foo_t)
字节。sizeof(char)
定义为 1,sizeof(int)
通常为 4,这意味着您的代码将取消引用 char 数组中 4 个字节的值,而不是 1 byte 到 char 数组中。edges
is an array of pointers to int. The expressionedges[0]
yields a pointer to int, not pointer to char. Treating it as a char array causes the wrong byte offset to be calculated, causing the value of an unexpected piece of memory to be displayed rather than the memory holding the char value 'd'.Array offsets calculate the offset in bytes based on the size of the type pointed to:
The value of
difference
will besizeof(foo_t)
bytes.sizeof(char)
is defined to be 1, andsizeof(int)
is typically 4, meaning that your code will dereference a value 4 bytes into the char array, not 1 byte into the char array.