字符串作为参考参数 [英] strings as reference parameters

查看:94
本文介绍了字符串作为参考参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是一直在玩着弦乐器以及它们的不同用途,后来我偶然发现了一个奇怪的怪异现象.我想到,在某个时候将字符串作为参考参数传递给函数可能很有用,因此您只需调用填充字符串的函数即可.好吧,引用参数基本上只是指向要传递的变量的指针,然后函数中该变量的每次使用实际上都是取消引用的指针,对吗?好吧,字符串已经是指向字符数组中第一个字符的指针.所以我想,为什么你不能只传递指针,然后像这样改变字符串的值:


i''ve just been playing around strings and different uses for them and whatnot, and then i stumbled upon a weird anomoly. I got it in my head that it might be useful at some point to pass a string into a function as a reference parameter so you can just call a function that fills a string. Well, a reference parameter is basically just a pointer to the variable being passed and then every usage of that variable within a function is actually a dereferenced pointer right? well, strings are already pointers to the first character in the array of characters. so i figured, why can''t you just pass the pointer and then change the value of the string like so:


#include <windows.h>
#include <iostream>
using namespace std;

void pass(LPTSTR herp);

int main (int argc, char * argv[])
{
	TCHAR herp[1024] = "";
	pass(herp);
	cout << herp << "   " << strlen(herp) << "\n";
	system("PAUSE");
        return 0;
}


void pass(LPTSTR herp)
{
	herp = TEXT("blah blah blah");
	cout << herp << "   " << strlen(herp) << "\n";
}



在这里,我只是传递了char * herp,然后尝试在函数中更改其值.奏效了!当我在函数中退出时,herp确实是等等等等"并且包含14个字符.但是然后,当函数结束,并且主体恢复时,我退出了,herp是空的,包含0个字符.因此,我只是想知道函数结束时,它会清除在我的传递函数有效期内分配的内存.那么,问题是,是否有必要在pass函数期间分配内存,并在我的main函数的整个生命周期中都保留在那里?

很奇怪,无法找出为什么c + +格式讨厌该代码.嗯,您就明白了.



Here, i just passed the char* herp, and then tried to change it''s value within the function. which worked! when i cout within the function, herp is indeed "blah blah blah" and contains 14 characters. but then, when the function ends, and the main resumes, and i cout there, herp is empty with 0 characters. So i just figure that when the function ends, it just clears the memory that was allocated during the lifetime of my pass function. So, the question, is there anyway to make the memory allocated during the pass function, stay there throughout the lifetime of my main function?

weird, can''t figure out why the c++ formatting hates that code. ah well, you get the picture.

推荐答案

您正在将指针的值"与指针指向的值"混合在一起. />
在传递函数中,LPTSTR herp的声明本质上是TCHAR *herp.在pass()函数内部,指针的本地值位于堆栈上,语句herp = TEXT("blah blah blah");在herp的本地副本中存储指向字符串"blah blah blah"的指针. main()函数中的指针(也称为herp)不受此更改的影响,因此这就是您看到看到的结果的原因.
如果您确实要在主代码中更改指针herp的值,则需要再次取消对指针的引用.
You are mixing up the "value of the pointer" versus the "value of what the pointer points to".

In your pass function, the declaration of LPTSTR herp is essentially TCHAR *herp. Inside the pass() function, a local value of the pointer is on the stack, the statement herp = TEXT("blah blah blah"); stores a pointer to the string "blah blah blah" in the local copy of herp. The pointer (also named herp) in the main() function is not affected by this change and that''s why you see the results you see.

If you really want to change the value of the pointer herp in the main code, you need to de-reference the pointer one more time.
*herp = TEXT("blah blah blah");

这只会更改指针的值,而不会更改字符数组.

如果要实际更改缓冲区本身的内容,则必须使用strcpy(herp, TEXT("blah blah blah");之类的字符将字符复制到数组中,换句话说,使用进入的指针指向字符串复制函数的目标.

防止缓冲区溢出是学生的一项练习.

and this only changes value of the pointer, not the contents of the TCHAR herp[1024] = ""; array of chars.

If you intent is to actually change the content of the buffer itself, then you have to copy the characters into the array with something like strcpy(herp, TEXT("blah blah blah");, in other words, use the incoming pointer to point to the destination of a string copy function.

Protecting against overflowing the buffer is an exercise for the student.


下面的代码将完成您想要做的事情.
The code below would do what you''re after.
void pass(TCHAR** herp)
{
    delete[] *herp;
    *herp = TEXT("blah blah blah");
    cout << *herp << "   " << strlen(*herp) << "\n";
}
 
int main (int argc, char * argv[])
{
    LPTSTR herp = new TCHAR[1024];
    pass(&herp);
    cout << herp << "   " << strlen(herp) << "\n";
    system("PAUSE");
}



自从使用C ++以来已经有10年了,所以我的C ++术语至少可以说是不好,所以我很难解释您的代码出了什么问题,但是这里就来了.

当像这样TCHAR herp[1024] = "";定义herp时,您不能更改地址,因为它是不可修改的左值.因此,您需要将其重写为指针LPTSTR herp = new TCHAR[1024];

pass函数中,您需要获取实际字符串的地址,您需要获取要更改的指针的地址.您可以使用&获得指针的地址.因此,为了将herp指针的地址传递给pass函数,您将编写pass(&herp)

如果您有任何问题,请随时提出,我将尝试更详细地说明我是否可以:)如果我无法做到,我可以确定一些经常参加会议的伟人问题与解答将可以.



It''s been almost 10 years since I worked with C++, so my C++ terminology is bad to say the least, so I have trouble explaining what the problem is with your code, but here goes.

When you define herp like this TCHAR herp[1024] = ""; you cannot change the address as it''s a non-modifiable lvalue. So you''ll need to rewrite it to a pointer LPTSTR herp = new TCHAR[1024];

In the pass function you take an address to the actual string, you''ll need to get the address of to the pointer you wish to change. you get the address of a pointer by using &. So in order to pass the address of the herp pointer to the pass function you''ll write pass(&herp)

If you have any question feel free to ask and I''ll try to explain in more detail if I''m able to :) If I''m not able I''m sure that some of the great guys who frequent the Q&A will be able to.


这篇关于字符串作为参考参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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