传递给函数的 Char 指针与 Int 指针 [英] Char pointers vs Int pointers passed to functions

查看:33
本文介绍了传递给函数的 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;
}

推荐答案

你的 showCharshowInt 函数打印不同的东西.

Your showChar and showInt functions are printing different things.

showChar 中,这个:

cout << &name << endl;

打印name的地址,这是一个局部变量.在 showInt 中,您不打印 &num 的值;而是打印 numvalue,这是一个地址,但不是局部变量的地址.

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 指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆