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

查看:126
本文介绍了字符指针与Int指针传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我理解了指针,但我认为他们对待的方式有很多细微的差别,我不太关注。当我传递一个整数指针或一个整数地址给showInt时,它将打印出与该函数外部相同的内存地址。然而。当我将以下指针传递给showChar时;

  char * value =One; 
showChar(value);

函数内第一个元素的地址不同于函数外部的地址。我明白,这是与传递值相一致的行为,并且指针的副本是在函数中创建的,但是我的印象是指针的副本仍然保持相同的地址。为什么在处理指向char的指针时会有所不同?如果char指针只是存储字符串文本的第一个元素的地址,那么为什么函数中的指针不指向相同的内存位置,而是指向内存中的新区域?这向我暗示,它不是复制一个char指针,而是创建一个新的char指针并为其指定原始指针指向的值。如果是这样,我不明白为什么。



我明白你可以通过传递指针指针或引用指针来访问函数中的指针地址,但是为什么这种情况仍然让我困惑。



传递指向char的指针

  void showChar(char * name){

cout<<名称<< ENDL;
cout<< & name<< ENDL;

传递指向int的指针

  void showInt(int * num){

cout<< num<< ENDL;
cout<< * num<< ENDL;


解决方案

您的 showChar showInt 函数正在打印不同的东西。



c> showChar ,这个:

  cout<< & name<< ENDL; 

打印名称的地址,即一个局部变量。在 showInt 中,您不会输出& num 的值。而是打印 num ,这是一个地址,而不是局部变量的地址。



showChar 中,如果要将 name 的值作为地址输出,您需要将其转换为其他指针类型,例如 void *

  cout<< (void *)name<< ENDL; 

因为运算符<< / code>对于 char * 取消引用 char * 指针并打印它指向的C风格字符串。



更详细地说:

  void showChar(char * name){
cout<<名称<< ENDL; //打印字符串的内容
//`name`指向
cout<< & name<< ENDL; //打印局部变量`name`的地址
}

void showInt(int * num){
cout<< num<< ENDL; //打印指针`num`的值
cout<< * num<< ENDL; //输出
//`num`指向
}


的`int`对象的值

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);

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.

Passing a pointer to char;

void showChar(char* name){

 cout << name << endl;
 cout << &name << endl;
}

Passing a pointer to int;

void showInt(int* num){

 cout << num << endl;
 cout << *num << endl;
}

解决方案

Your showChar and showInt functions are printing different things.

In showChar, this:

cout << &name << endl;

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.

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;

because the overload of operator<< for char* dereferences the char* pointer and prints the C-style string that it points to.

In more detail:

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
}

这篇关于字符指针与Int指针传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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