C:锯齿状数组的问题

发布于 2024-08-08 10:07:20 字数 261 浏览 6 评论 0原文

我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小红帽 2024-08-15 10:07:20

由于 intchar 具有不同的大小,因此您应该尝试 char *edges[500]

Since an int and a char have different sizes, you should try char *edges[500]

时光是把杀猪刀 2024-08-15 10:07:20

类型不匹配。 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.

我偏爱纯白色 2024-08-15 10:07:20

嗯,这条

t.c:6: warning: assignment from incompatible pointer type

消息可能与此有关。

在 C 中,'c' 是一个(小)整数。您将它们存储为 arr 中的 char。但是,通过将 arr 作为 edges[0](即 int *)访问,您实际上正在检索(在大多数平台上)arr [0]arr[1]arr[2]arr[3] 作为整数。

Well, the

t.c:6: warning: assignment from incompatible pointer type

message might have something to do with it.

In C, 'c' is a (small) integer. You store those as chars in arr. However, by accessing arr as edges[0] which is an int *, you are actually retrieving (on most platforms) arr[0], arr[1], arr[2] and arr[3] as an integer.

×纯※雪 2024-08-15 10:07:20

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 of edges[0][1] (which happened to be 0).

change type of edges to char* edgeds[500].

绳情 2024-08-15 10:07:20

第 3

edges[0] = arr;

行赋值中的不兼容类型:arrchar *edges[0]int *

发生的情况是这样的

int *edges[500];
char arr[] = {'c','d'};

arr[0]是'c',arr[1]是'd'

edges[0] = arr;

忽略类型兼容性,edges[0]指向int “c”和“d”(可能还有两个未指定的字符)所在的地址。

printf("%c %c", edges[0][0],edges[0][1]);

Edges[0][0] 是 Edges[0] 中的第一个整数:它是 'c'、'd'(以及可能的另外两个未指定字符)的混合。该值被转换为无符号字符,产生打印的“c”。
Edges[0][1] 指向“c”、“d”(可能还有另外两个未指定字符)混合之后的整数。该内存位置尚未初始化,它很可能超出了您的进程可以访问的范围。

如果打印一个 int 而不是 2 个字符,

printf("%d\n", edges[0][0]);

您将看到“c”、“d”(可能还有两个未指定的字符)的混合。

最好的办法就是正确输入类型。

line 3

edges[0] = arr;

incompatible types in assignment: arr is a char * and edges[0] is a int *.

What happens is this:

int *edges[500];
char arr[] = {'c','d'};

arr[0] is 'c' and arr[1] is 'd'

edges[0] = arr;

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.

printf("%c %c", edges[0][0],edges[0][1]);

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

printf("%d\n", edges[0][0]);

you will see the mix of 'c', 'd' (and possibly two unspecified characters).

Best thing to do is get your types right.

四叶草在未来唯美盛开 2024-08-15 10:07:20

edges 是一个指向 int 的指针数组。表达式edges[0] 产生一个指向int 的指针,而不是指向char 的指针。将其视为 char 数组会导致计算出错误的字节偏移量,从而导致显示意外的内存块的值,而不是保存 char 值“d”的内存。

数组偏移量根据指向的类型的大小计算偏移量(以字节为单位):

foo_t * x;
ptrdiff_t difference = x[1] - x[0];

difference 的值将为 sizeof(foo_t) 字节。 sizeof(char) 定义为 1,sizeof(int) 通常为 4,这意味着您的代码将取消引用 char 数组中 4 个字节的值,而不是 1 byte 到 char 数组中。

edges is an array of pointers to int. The expression edges[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:

foo_t * x;
ptrdiff_t difference = x[1] - x[0];

The value of difference will be sizeof(foo_t) bytes. sizeof(char) is defined to be 1, and sizeof(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文