为什么这不行?
void func(const char* &pointer){...}
//in main
const char* mainPointer = "a word";
func(mainPointer);
我的意图是发送一个指向函数的指针,该函数会更改它(指针)但不会更改它指向的字符。
为什么这不行?
void func(const char* &pointer){...}
//in main
const char* mainPointer = "a word";
func(mainPointer);
我的意图是发送一个指向函数的指针,该函数会更改它(指针)但不会更改它指向的字符。
以下代码 (MS Visual C++ 2010) 表明它是完全可能的并且有效。输出是:“世界!”
#include "stdafx.h"
#include <iostream>
using namespace std;
void func(const char*& ptr)
{
ptr += 6;
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* Ptr = "Hello World!";
func(Ptr);
cout << Ptr << endl;
return 0;
}
请注意,与 Null Voids 代码相比,我们确实在此处修改了 func 中的指针。
完全没问题。事实上,它就像一个魅力。
#include <iostream>
using namespace std ;
void func(const char* &pointer)
{
cout<<" \n "<<pointer<<"\n";
}
int main()
{
const char* mainPointer = "a word";
func(mainPointer);
cout<<" \nPress any key to continue\n";
cin.ignore();
cin.get();
return 0;
}
程序输出:
a word
Press any key to continue
编译输出:
Compiling...
this.cpp
Linking...
this.exe - 0 error(s), 0 warning(s)