好吧,我错了(或者我的脚仍然没那么好吃) [英] Ok, I was in error (or my foot still doesn't taste so great)

查看:65
本文介绍了好吧,我错了(或者我的脚仍然没那么好吃)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近回复了MC felons发布的返回字符串的帖子,并且在我试图做的事情上是错误的。
。在完全通过我的代码运行后,我做了
发现我误解了C ++如何与char数组一起工作。经过多次测试后,我想提交以下代码供评论:


#include< Stdlib.h>

char * Func1()

/ * ------------------------------ -------------------------------------

分配内存并填充内存用字符串,Hello World!并且

将所有权转让给调用函数

--------------------------- ------------------------------------------ * /

{

char * tmp =(char *)malloc(20); //为数组分配空间

char T2 [20] =" Hello World!" ;;

int i = 0;


//填充char数组

do

{

tmp [i] = T2 [i];

i ++;

} while(T2 [i]!= 0);

tmp [i] = 0;

返回tmp;

};


// ------------------------ ---------------------------------------

void Func2( char * tmp)

/ * ------------------------------------ --------------------------

通过引用获取字符串参数并将其更改为

Not Hello World

---------------------------------- ----------------------------- * /

{

char * T2 =Not Hello World \ 0;


int i = 0;

do

{

tmp [i] = T2 [i];

i ++;

} while(T2 [i]!= 0);

tmp [i] = 0;

};


// ---------------- ----------------------------------------------- <无线电通信/>
int main()

{

char * Tmp = Func1(); //为数组分配内存,并用Hello

World填充它。
Func2(Tmp); //将字符串更改为Not Hello World!

free(Tmp); //在Func1中分配的可用内存,并分配给/或​​
到Tmp;

};


在我的所有测试运行中,我得到没有任何错误的预期结果。虽然我确认使用字符串函数会更好,但我仍然喜欢编写自己的解决方案,有时只是为了学习体验。

I recently replied to MC felons post on returning strings and was in error
on what I was trying to do. After running completely thru my code, I did
find that I was misunderstanding how C++ works with char arrays. After much
testing, I would like to submit the following code for comments:

#include <Stdlib.h>

char *Func1()
/*-------------------------------------------------------------------
allocates memory and fills it with the string, "Hello World!" and
passes ownership to the calling function
---------------------------------------------------------------------*/
{
char *tmp = (char*)malloc(20); // allocate space for the array
char T2[20] = "Hello World!";
int i = 0;

// fill the char array
do
{
tmp[i] = T2[i];
i++;
}while(T2[i] != 0);
tmp[i] = 0;
return tmp;
};

//---------------------------------------------------------------
void Func2(char *tmp)
/*--------------------------------------------------------------
Takes a string argument by reference and changes it to
"Not Hello World"
---------------------------------------------------------------*/
{
char* T2 = "Not Hello World\0";

int i=0;
do
{
tmp[i] = T2[i];
i++;
}while(T2[i]!=0);
tmp[i]=0;
};

//---------------------------------------------------------------
int main()
{
char *Tmp = Func1(); // allocates memory for array and fills it with "Hello
World"
Func2(Tmp); // change string to "Not Hello World!"
free(Tmp); // free memory allocated in Func1 and assigned
to Tmp;
};

In all my test runs, I get the desired results without any errors. While I
acknowledge that using the string functions would be preferable, I still
like to code my own solutions sometimes just for the learning experience.

推荐答案

David Pratt写道:
David Pratt wrote:

我最近回复了MC felons帖子,返回字符串并且在

错误

我想做什么。在完全通过我的代码运行后,我做了
发现我误解了C ++如何与char数组一起工作。在

多次测试之后,我想提交以下代码进行评论:
I recently replied to MC felons post on returning strings and was in
error
on what I was trying to do. After running completely thru my code, I did
find that I was misunderstanding how C++ works with char arrays. After
much testing, I would like to submit the following code for comments:



我确定看起来像C给我。

I sure looks like C to me.


#include< Stdlib.h>


// --------------- ------------------------------------------------ <无线电通信/>
int main()

{

char * Tmp = Func1(); //为数组分配内存并用

Hello World填充它

Func2(Tmp); //将字符串更改为Not Hello World!

free(Tmp); //在Func1中分配的空闲内存和

指定

到Tmp;

};


在我的所有测试运行中,我得到了所需的结果,没有任何错误。虽然我确认使用字符串函数会更好,但我仍然喜欢编写自己的解决方案,有时只是为了学习体验。
#include <Stdlib.h>

//---------------------------------------------------------------
int main()
{
char *Tmp = Func1(); // allocates memory for array and fills it with
"Hello World"
Func2(Tmp); // change string to "Not Hello World!"
free(Tmp); // free memory allocated in Func1 and
assigned
to Tmp;
};

In all my test runs, I get the desired results without any errors. While I
acknowledge that using the string functions would be preferable, I still
like to code my own solutions sometimes just for the learning experience.



为什么不看看< string.h的GNU实现>?


BTW,有我不喜欢C的两个主要原因。一个是malloc,

另一个是免费的。

-

NOUN:1 。遗嘱遗留给他人的金钱或财产。 2.从祖先或前任或过去那里下来的东西:b
宗教自由的遗产。 ETYMOLOGY:MidE legacie,副手办公室,来自OF,来自ML legatia的
,来自L legare,以及deute,遗赠。 www.bartleby.com/61/




David Pratt写道:

David Pratt wrote:

我最近回复MC felons帖子返回字符串并且错误。在完全通过我的代码运行后,我做了
发现我误解了C ++如何与char数组一起工作。经过多次测试后,我想提交以下代码供评论:


#include< Stdlib.h>

char * Func1()

/ * ------------------------------ -------------------------------------

分配内存并填充内存用字符串,Hello World!并且

将所有权转让给调用函数

--------------------------- ------------------------------------------ * /

{

char * tmp =(char *)malloc(20); //为数组分配空间

char T2 [20] =" Hello World!" ;;

int i = 0;


//填充char数组

do

{

tmp [i] = T2 [i];

i ++;

} while(T2 [i]!= 0);

tmp [i] = 0;
I recently replied to MC felons post on returning strings and was in error
on what I was trying to do. After running completely thru my code, I did
find that I was misunderstanding how C++ works with char arrays. After much
testing, I would like to submit the following code for comments:

#include <Stdlib.h>

char *Func1()
/*-------------------------------------------------------------------
allocates memory and fills it with the string, "Hello World!" and
passes ownership to the calling function
---------------------------------------------------------------------*/
{
char *tmp = (char*)malloc(20); // allocate space for the array
char T2[20] = "Hello World!";
int i = 0;

// fill the char array
do
{
tmp[i] = T2[i];
i++;
}while(T2[i] != 0);
tmp[i] = 0;



为什么重新发明轮子?你可以用

strcpy完成整个循环,这比使用std :: string要多得多。

Why reinvent the wheel? You could have just done this whole loop with
strcpy and it''s all a whole lot more work than using std::string.




" Noah Roberts" < ro ********** @ gmail.com写信息

新闻:11 ******************** *@16g2000cwy.googlegrou ps.com ...

"Noah Roberts" <ro**********@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...

为什么重新发明轮子?你可以用

strcpy完成整个循环,这比使用std :: string要多得多。
Why reinvent the wheel? You could have just done this whole loop with
strcpy and it''s all a whole lot more work than using std::string.



啊但是如果我这样做的话,我仍然会在假设C ++处理指针的方式下运行假b / b
。现在我知道了。什么可以

我说?我是大学里的一名工程专业学生,我曾经有过这样的礼物,想知道事情是如何运作的,特别是当我认为

会起作用的时候。吨。在过去的几天里,我学到了很多关于C ++

基础知识的知识。试图重新发明轮子让我们跟踪了车辆。


有时学习你不知道的东西,你不知道可以更多

比了解你所知道的你不知道的重要。 Wener

Earhart,est。的创始人

Ah but if I had done that, I would still be operating under false
assumptions about the way C++ handles pointers. Now I KNOW better. What can
I say? I was an engineering student in college, and I have this ever present
itch to try and figure out how things work, especially when what I thought
would work doesn''t. I learned a lot over the past couple of days about C++
basics. And trying to reinvent the wheel got us tracked vehicles.

"Sometimes learning what you don''t know that you don''t know can be more
important than learning about what you know that you don''t know." Wener
Earhart, founder of est.


这篇关于好吧,我错了(或者我的脚仍然没那么好吃)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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