gpt4 book ai didi

c - 指向 CharArray 的 IntPointer 和 C 中的类型信息

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:50 27 4
gpt4 key购买 nike

我的程序

int main() {
int int_array[5] = {1, 2, 3, 4, 5};
char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
int* int_pointer = char_array;
char* char_pointer = int_array;

for(int i=0; i < 5; i++) {
printf("[Integer pointer] points to %p, which contains the char '%c'\n", int_pointer, *int_pointer);
int_pointer += 1;
}

for(int i=0; i < 5; i++) {
printf("[Char pointer] points to %p, which contains the integer %d\n", char_pointer, *char_pointer);
char_pointer += 1;
}
}

产生以下输出:

[integer pointer] points to 0xbffff810, which contains the char 'a'
[integer pointer] points to 0xbffff814, which contains the char 'e'
[integer pointer] points to 0xbffff818, which contains the char '8'
[integer pointer] points to 0xbffff81c, which contains the char '
[integer pointer] points to 0xbffff820, which contains the char '?'
[char pointer] points to 0xbffff7f0, which contains the char '1'
[char pointer] points to 0xbffff7f1, which contains the char '0'
[char pointer] points to 0xbffff7f2, which contains the char '0'
[char pointer] points to 0xbffff7f3, which contains the char '0'
[char pointer] points to 0xbffff7f4, which contains the char '2'

考虑到我是C语言和计算机科学的新手,我只是不明白为什么一方面int_pointer包含递增时向前移动四个字节的信息,但是另一方面另一方面,一次只读取一个字节,而 char_pointer 一次向前移动一个字节,一次只读取一个字节。那么int_pointer(char_pointer)到底包含了哪些信息呢?在我的逻辑中,所有信息都包含在指针中,为什么 int_pointer(整数是一个具有四个字节长度的数据结构)一次只读取一个字节,但一次向前移动四个字节没有任何意义时间。应该只读取一个字节而不是四个字节的信息存储在哪里(尽管 int_pointer 被声明为 int,这意味着它应该一次读取四个字节)?这个逻辑的矛盾在于,如果所有信息都存储在指针中,那么为什么 char_pointer(声明为一个字节)一次读取和跳转(如果递增)一个字节,而 int_pointer(声明为四个字节)字节)一次读取并跳转(如果递增)四个字节?

对于 int_pointer,我预期的输出如下:

[integer pointer] points to 0xbffff810, which contains the char 'abcd'
[integer pointer] points to 0xbffff814, which contains the char 'eUNDEFINEDSTUFF'
[integer pointer] points to 0xbffff818, which contains the char 'UNDEFINEDSTUFF'
[integer pointer] points to 0xbffff81c, which contains the char 'UNDEFINEDSTUFF'
[integer pointer] points to 0xbffff820, which contains the char 'UNDEFINEDSTUFF'

我希望我说清楚了哪一点我完全不明白,在此先感谢您的努力。

最佳答案

'abcd' 没有字符。该程序完全符合您的预期。在内存中,字符是这样存储的……

[   a   ][   b   ][   c   ][   d   ][   e   ]

你把整型指针指向a开头,说要读取一个字符(1字节),所以它读取一个字节,a。然后递增整数指针。

before: [   a   ][   b   ][   c   ][   d   ][   e   ] stuff.......
^ pointer
after: [ a ][ b ][ c ][ d ][ e ] stuff.......
^ pointer

现在指针移动了 4 个字节并指向 e。你说要读取 1 个字节,它会读取并输出 e。从这里开始,您会遇到意想不到的行为,如您在 8、空格和 ? 中看到的那样。

现在是整数数组。每个整数是 4 个字节,其中 1 是0000......00001。关键是,那个在最右边。

当你在内存中安排它时,它看起来像这样:

[00000000][00000000][00000000][00000001] [00000000][00000000][00000000][00000010] [00000000][00000000][00000000][00000011] [00000000][00000000][00000000][00000100] [00000000][00000000][00000000][00000101] and more stuff....

你这样放置你的指针:

[00000000][00000000][00000000][00000001] [00000000][00000000][00000000][00000010] [00000000][00000000][00000000][00000011] [00000000][00000000][00000000][00000100] [00000000][00000000][00000000][00000101] and more stuff....
^ pointer

并且您通过取消引用 char 指针告诉它读取 1 个字节,因此您将 00000001 提供给 printf 中的 %d。然后你递增

[00000000][00000000][00000000][00000001] [00000000][00000000][00000000][00000010] [00000000][00000000][00000000][00000011] [00000000][00000000][00000000][00000100] [00000000][00000000][00000000][00000101] and more stuff....
^ pointer

你告诉它通过你的解引用读取一个字节,它向你的 printf 提供一串 0,打印出一个 0。几个循环之后,你的指针到达 00000010 并打印出一个 2。如果你继续一些更多,你还有 3 个 0 和一个 3,还有 3 个 0 和一个 4,依此类推。

希望对您有所帮助。

关于c - 指向 CharArray 的 IntPointer 和 C 中的类型信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44844421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com