char *作为参数的问题

编程入门 行业动态 更新时间:2024-10-23 13:38:51
本文介绍了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 ... @ examplewrote: On Jun 11, 11:12 am, Noah Roberts <u...@examplewrote: 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 *作为参数的问题

本文发布于:2023-10-28 23:26:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538073.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   char

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!