Char pointers vs Int pointers passed to functions(传递给函数的 Char 指针与 Int 指针)
问题描述
我以为我理解了指针,但我认为它们被区别对待的方式存在细微差别,我并没有完全理解.当我将整数指针或整数地址传递给 showInt 时,它将打印出与函数外部相同的内存地址.然而.当我将以下指针传递给 showChar 时;
I thought I understood pointers, but I think there's a nuance by how they're treated differently that I'm not quite following. When I pass an integer pointer or the address of an integer to showInt, it will print out the same memory address as it would outside the function. However. When I pass the following pointer to showChar;
char* value = "One";
showChar(value);
函数内部的第一个元素的地址与函数外部的地址不同.我知道这是与按值传递一致的行为,并且指针的副本是在函数内制作的,但是我的印象是指针的副本仍然具有相同的地址.为什么在处理指向 char 的指针时会有所不同?如果 char 指针只是存储字符串字面量的第一个元素的地址,那么为什么函数中的指针不指向相同的内存位置,而是指向内存中的新区域?这表明它不是复制 char 指针,而是创建一个新的 char 指针并将其分配给原始指针指向的值.如果是这样,我不明白为什么.
The address of the first element is different inside the function than it is outside the function. I understand that this is behaviour consistent with passing by value, and that a copy of the pointer is made within the function, however I was under the impression that a copy of a pointer still held the same address. Why is it different when dealing with pointers to char? If the char pointer just stores the address of the first element of the string literal, then why would the pointer in the function not point to the same memory location, but instead point to a new area in memory? That suggests to me that it isn't copying a char pointer, but creating a new char pointer and assigning it the value pointed to by the original pointer. If so, I don't understand why.
我知道您可以通过传递指向指针或引用指向指针来访问函数中的指针地址,但为什么会这样仍然让我感到困惑.
I understand that you can access the pointer address in the function by passing a pointer-to-pointer, or a reference-to-pointer instead, but why this is the case still confuses me.
传递一个指向 char 的指针;
void showChar(char* name){
cout << name << endl;
cout << &name << endl;
}
传递指向 int 的指针;
void showInt(int* num){
cout << num << endl;
cout << *num << endl;
}
推荐答案
你的 showChar
和 showInt
函数打印不同的东西.
Your showChar
and showInt
functions are printing different things.
在 showChar
中,这个:
cout << &name << endl;
打印name
的地址,这是一个局部变量.在 showInt
中,您不打印 &num
的值;而是打印 num
的 value,这是一个地址,但不是局部变量的地址.
prints the address of name
, which is a local variable. In showInt
, you don't print the value of &num
; rather you print the value of num
, which is an address, but not the address of a local variable.
在showChar
中,如果你想打印name
的值作为地址,你需要把它转换成其他指针类型如 void*
:
In showChar
, if you want to print value of name
as an address, you'll need to convert it to some other pointer type such as void*
:
cout << (void*)name << endl;
因为对于 char*
的 operator<<
的重载会取消引用 char*
指针并打印它指向的 C 样式字符串到.
because the overload of operator<<
for char*
dereferences the char*
pointer and prints the C-style string that it points to.
更详细的:
void showChar(char* name){
cout << name << endl; // prints the contents of the string that
// `name` points to
cout << &name << endl; // prints the address of the local variable `name`
}
void showInt(int* num){
cout << num << endl; // prints the value of the pointer `num`
cout << *num << endl; // prints the value of the `int` object that
// `num` points to
}
这篇关于传递给函数的 Char 指针与 Int 指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:传递给函数的 Char 指针与 Int 指针
基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01