char *作为参数的问题 [英] Problem with char* as a parameter

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

问题描述

我正在处理一个代码,该代码应该从测试函数返回一个字符串到我的主要

函数。测试中的cout返回我的名字,

但是主要的cout返回一些垃圾字符。


谁能告诉我如何修复它所以我得到我的名字打印自

main也。谢谢。


#include< iostream>

使用命名空间std;


void test(char * temp [])

{

char name [] =" My Name";

temp [0] = name;

cout<< temp [0]<<结束;

}


int main()

{

char * info [9] ;

test(info);

cout<< info [0]<< endl;

}

I am working on a code that is supposed to return a string to my main
function from the test function. The cout in test returns "My Name",
but the cout in main returns some junk characters.

Can anyone tell me how I could fix it so I get "My Name" printed from
main also. Thanks.

#include <iostream>
using namespace std;

void test (char * temp[])
{
char name[] = "My Name";
temp[0] = name;
cout << temp[0] << endl;
}

int main ()
{
char* info[9];
test (info);
cout << info[0] << endl;
}

推荐答案

Neil写道:
Neil wrote:

我正在处理一个代码,该代码应该从测试函数返回一个字符串到我的main

函数。测试中的cout返回我的名字,

但是主要的cout返回一些垃圾字符。


谁能告诉我如何修复它所以我得到我的名字打印自

main也。谢谢。


#include< iostream>

使用命名空间std;


void test(char * temp [])

{

char name [] =" My Name";

temp [0] = name;

cout<< temp [0]<<结束;

}
I am working on a code that is supposed to return a string to my main
function from the test function. The cout in test returns "My Name",
but the cout in main returns some junk characters.

Can anyone tell me how I could fix it so I get "My Name" printed from
main also. Thanks.

#include <iostream>
using namespace std;

void test (char * temp[])
{
char name[] = "My Name";
temp[0] = name;
cout << temp[0] << endl;
}



将第一行更改为:


静态字符名称[] =我的名字;


我会让你弄清楚为什么这是必要的,很棒的想法

练习会教你一个相当基本的东西。

Change the first line to:

static char name[] = "My Name";

I''m going to let you figure out why that is necessary, great thought
exercise that will teach you a rather fundamental thing.


Neil写道:
Neil wrote:

我正在处理一个应该返回的代码字符串到我的主要

函数来自测试函数。
I am working on a code that is supposed to return a string to my main
function from the test function.



std :: string my_test_function();

std::string my_test_function();


测试中的cout返回我的名字 ,

但是主要的cout会返回一些垃圾字符。


任何人都可以告诉我如何修复它以便我得到我的名字。打印自

main也。谢谢。
The cout in test returns "My Name",
but the cout in main returns some junk characters.

Can anyone tell me how I could fix it so I get "My Name" printed from
main also. Thanks.



没有适用于您的计划的修正。它已经超出了修复范围。

你需要用''std :: string''重写它。

There is no fixing applicable to your program. It''s beyond fixing.
You need to rewrite it in terms of ''std::string''.


>

#include< iostream>

使用命名空间std;


void test(char * temp [])

{

char name [] =" My Name";

temp [0] = name;

cout< ;< temp [0]<<结束;

}


int main()

{

char * info [9] ;

test(info);

cout<< info [0]<< endl;

}
>
#include <iostream>
using namespace std;

void test (char * temp[])
{
char name[] = "My Name";
temp[0] = name;
cout << temp[0] << endl;
}

int main ()
{
char* info[9];
test (info);
cout << info[0] << endl;
}



#include< iostream>

#include< string>

使用命名空间std;


std :: string test()

{

return" My Name" ;;

}


int main()

{

cout<< test()<<结束;

}


V

-

请删除大写''A'' s通过电子邮件回复

我没有回复最热门的回复,请不要问

#include <iostream>
#include <string>
using namespace std;

std::string test()
{
return "My Name";
}

int main()
{
cout << test() << endl;
}

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


6月11日,上午11点12分,Noah Roberts< u ... @ example.netwrote:
On Jun 11, 11:12 am, Noah Roberts <u...@example.netwrote:

Neil写道:
Neil wrote:

我正在处理一个代码,该代码应该从测试函数返回一个字符串到我的main

函数。测试中的cout返回My Name,

但是main中的cout返回一些垃圾字符。
I am working on a code that is supposed to return a string to my main
function from the test function. The cout in test returns "My Name",
but the cout in main returns some junk characters.


任何人都可以告诉我如何修复它,以便我得到我的名字。打印自

main也。谢谢。
Can anyone tell me how I could fix it so I get "My Name" printed from
main also. Thanks.


#include< iostream>

using namespace std;
#include <iostream>
using namespace std;


void test(char * temp [])

{

char name [] =我的名字;

temp [0] =姓名;

cout<< temp [0]<<结束;

}
void test (char * temp[])
{
char name[] = "My Name";
temp[0] = name;
cout << temp[0] << endl;
}



将第一行更改为:


静态字符名称[] =我的名字;


我会让你弄清楚为什么这是必要的,很棒的想法

练习会教你一个相当基本的东西。


Change the first line to:

static char name[] = "My Name";

I''m going to let you figure out why that is necessary, great thought
exercise that will teach you a rather fundamental thing.



谢谢,它正在运作。问题是名称是在内部声明的吗?
测试函数和temp [0]只存储指向

名称的指针,一旦函数返回就会被回收,并且制作

它是一个类变量解决了名义上的价值问题

丢失了吗?

Thanks, it''s working. Was the problem that name was declared inside
the test function and temp[0] was only storing the pointer to the
name, which would be recycled once the function returned, and making
it a class variable solves the problem of the value in name getting
lost?


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

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