- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的程序
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/
有人能给我解释一下我下面程序中的一点吗我编写的以下程序是为了理解 int * const * var 的含义; #include "iostream" using namespace std ; in
我正在摆弄 C 中的指针,但仍然不确定一些非常基础的知识。我想出了以下示例代码: #include int main(void) { int num = 42; /
以下代码产生警告: const char * mystr = "\r\nHello"; void send_str(char * str); void main(void){ send_str
我正在尝试在我的 Linux 计算机上升级 cmake。这是我使用的命令。 删除旧的 cmake: sudo apt purge --auto-remove cmake 从 https://cmake
我试图借助 char 指针来追踪 union 第一个字节的值。我假设 byte1 的输出应该是 ff,但实际上是 ffffffff。如果我错了请纠正我。 #include #include #in
我正在使用微 Controller 进行一些 ADC 测量。当我尝试使用 -O2 优化编译以下代码时遇到问题,当代码中存在 PrintVal() 函数时,MCU 卡住。我做了一些调试,结果发现,当我添
#include #include using namespace std; int main () { vector qwerty; qwerty.push_back(5);
我有我的 woking setup.py 文件;其中包含以下行: home = os.environ["HOME"] # home = /home/dr in my machine; distu
这段代码 #include #include static_assert(std::is_same_v::value_type, volatile int>); 在最新的 GCC 和 clang
我对 C 中的前向声明有疑问。 代码 typedef struct yhash_s t_yhash;// forward declaration struct yhash_s { size_t
我想提取成员指针指向的成员的类型。 template void demo(myClass& instance, void* ptr) { instance.*member = *reinter
我正在尝试使用指针将一段 C 代码转换为 Swift 3。这是 C 代码中的相关部分。 Float32 sampleArray[256] = { // Array is 256 Float value
您可能知道,VLA's haves pros and cons 和它们在 C11 中是可选的。 我想使 VLA 成为可选项的主要原因是:“堆栈可能会爆炸”: int arr[n]; /* where
这段代码有什么错误?为什么我不能按照我尝试的方式取消引用该元素。 #include typedef struct { int value; struct node * left;
有什么方法可以在 JavaScript 中创建\返回指向变量的指针吗? 比如,在 PHP 中: function func() { ..... return &$result; } 我
如果您想使用方法的指针作为参数,则需要将该方法键入作为对象的函数,就像这样好 : type TAcceptor = function(filename:string):boolean of objec
很简单的问题: 我对 C++ 中的智能指针有点陌生。我想我得到了所有权的东西,但我不知道如何访问他们实际指向的内容。当我尝试使用对象的成员函数/变量时,我只是得到了 unique_ptr 类的函数,这
我得到了一个点的方位 Angular 、指南针方向和一个可以将箭头设置到某个方向的 api(0 是顶部,90 是右侧,180 是底部,360 是顶部) 如果我希望箭头指向我采用方位 Angular 形
我正在尝试找到一种方法,从单元格中获取位于当前工作表左侧(托盘下方)的工作表单元格中的数据。 我知道如何通过调用其他工作表 =Sheet1!A1 但现在我需要一些最好的解释 =Sheet[-1]!A1
所以我在 MATLAB 中有一个 for 循环,其中向量 x 将通过一个函数,比如 cos(x).^2,或者不同的选择,比如 sin(x).^2 + 9.*x。用户将在 for 循环之前选择他想使用的
我是一名优秀的程序员,十分优秀!