C字符串不从函数返回 [英] C Strings not returning from a function

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

问题描述

我已经做了一个小程序来演示一个问题我正在用C修复

字符串。


我需要能够从文本行中删除HTML标记。


我创建我的变量,将其传递给我的函数,验证数据已经正确传递了
然后无法获取数据!


如果我将返回值更改为某个随机字符串文字,它会返回

罚款。


所有建议都表示赞赏。


#include< stdio.h>

#include< string.h>


char * FixString(char * strIn)

{

char * strMsg = strIn;

char strTmp [40 ] ="" ;;

char * strReturn =& strTmp;


int i = 0;

int j = strlen(strMsg);

for(i = 0; i< = j; ++ i)

{

/ *删除usenet上简洁的清理代码* /

strTmp [i] = strMsg [i];

}


printf( "%s \ n",strReturn);

/ *完美打印出字符串* /


返回strReturn; / *更改为返回全部清除它工作正常

* /

}


int main(无效)

{

char * strOut ="这是来自网页的一行< br>";

char * strBack = FixString(strOut);


printf("%s \ n",strBack); / *打印垃圾 - 为什么? * /


返回0;

}

I''ve made a small program to demonstrate one problem I''m having fixing
strings in C.

I need to be able to remove HTML mark-ups from text lines.

I create my variable, pass it to my function, verify that the data has
been passed correctly and then cannot get the data back!

If I change the return to some random string literal, it comes back
fine.

All advice appreciated.

#include <stdio.h>
#include <string.h>

char * FixString(char *strIn)
{
char *strMsg = strIn;
char strTmp[40] = "";
char *strReturn = &strTmp;

int i = 0;
int j = strlen(strMsg);
for (i = 0; i <= j; ++i)
{
/* removing the cleanup code for brevity on usenet */
strTmp[i] = strMsg[i];
}

printf("%s\n", strReturn);
/* Prints out the string perfectly */

return strReturn; /* change to return "All clear" and it works fine
*/
}

int main(void)
{
char *strOut = "This is a line from a web page <br>";
char *strBack = FixString(strOut);

printf("%s\n", strBack); /* Prints garbage - why? */

return 0;
}

推荐答案

pkirk25写道:
pkirk25 wrote:

我已经制作了一个小程序来演示一个问题我正在用C修复

字符串。


我需要能够从文本行中删除HTML标记。


我创建变量,将其传递给我的函数,验证数据是否已正确传递
然后无法获取数据!


如果我将返回值更改为某个随机字符串文字,则会返回

罚款。


所有建议都表示赞赏。


#include< stdio.h>

#include< string.h>


char * FixString(char * strIn)

{

char * strMsg = strIn;

char strTmp [40] ="" ;;

char * strReturn =& strTmp;
I''ve made a small program to demonstrate one problem I''m having fixing
strings in C.

I need to be able to remove HTML mark-ups from text lines.

I create my variable, pass it to my function, verify that the data has
been passed correctly and then cannot get the data back!

If I change the return to some random string literal, it comes back
fine.

All advice appreciated.

#include <stdio.h>
#include <string.h>

char * FixString(char *strIn)
{
char *strMsg = strIn;
char strTmp[40] = "";
char *strReturn = &strTmp;



你的编译器没有给你一个有用的诊断这里?如果没有,请将

提高到警告级别或使用更好的编译器。

Didn''t your compiler give you a helpful diagnostic here? If not, turn
up its warning level or use a better compiler.


int i = 0;

int j = strlen(strMsg);


for(i = 0; i< = j; ++ i)

{

/ *删除清理代码以简化usenet * /

strTmp [i] = strMsg [i];

}

printf("%s \ n",strReturn);

/ *完美打印出字符串* /


返回strReturn ; / *更改为返回全部清除并且工作正常
int i = 0;
int j = strlen(strMsg);
for (i = 0; i <= j; ++i)
{
/* removing the cleanup code for brevity on usenet */
strTmp[i] = strMsg[i];
}

printf("%s\n", strReturn);
/* Prints out the string perfectly */

return strReturn; /* change to return "All clear" and it works fine



你试图返回一个指向本地变量的指针,不要这样做
。传入输出字符串,或者使用动态缓冲区

由函数分配并返回。


-

Ian Collins。

You are attempting to return a pointer to a local variable, don''t do
this. Either pass in the output string, or use a dynamic buffer
allocated by the function and return this.

--
Ian Collins.


[snip]

我的编译器是Visual C ++ 6我知道它已经老了但是它是什么

可用。


我真的认为我的问题是挣扎于间接挣扎

和指针更多的编译器,诱惑它是责备

Microsoft。
[snip]
My complier is Visual C++ 6 wcih I know is old but it is what was
available.

I genuinely think my problems are dwon to struggling with indirection
and pointers moret han the compiler, tempting tho it is to blame
Microsoft.

>

你试图返回一个指向局部变量的指针,不要这样做$ / b $这个。传入输出字符串,或使用函数分配的动态缓冲区

并返回此值。
>
You are attempting to return a pointer to a local variable, don''t do
this. Either pass in the output string, or use a dynamic buffer
allocated by the function and return this.



Ian,如果我重新执行* strReturn指向的字符串数组strTmp,我将获得同样的混乱。


你能用传入输出字符串来解释你的意思吗?什么

我会改变我的代码来做到这一点?很抱歉要求用勺子喂。

Ian, if i retrun the string array strTmp that *strReturn points to, I
get the same mess.

Can you explain what you mean by "pass in the output string"? What
would I change in my code to do that? Sorry to ask to be spoon fed.


pkirk25写道:
pkirk25 wrote:

[snip]

我的编译器是Visual C ++ 6 wcih我知道它已经老了但它是什么

可用。


我真的认为我的问题dwon是为了与间接困难而苦苦挣扎b / b
指针编译器,诱惑它是责怪

微软。
[snip]
My complier is Visual C++ 6 wcih I know is old but it is what was
available.

I genuinely think my problems are dwon to struggling with indirection
and pointers moret han the compiler, tempting tho it is to blame
Microsoft.



C ++编译器应该拒绝编译你剪掉的行;


char * strReturn =& strTmp;


你在这里尝试使用char **初始化char *,使用


char * strReturn = strTmp;





char * strReturn =& strTmp [0];

A C++ compiler should refuse to compile the line you snipped;

char *strReturn = &strTmp;

Here you are attempting to initialise a char* with a char**, either use

char *strReturn = strTmp;

or

char *strReturn = &strTmp[0];


>>您正在尝试返回指向局部变量的指针,不要这样做。传入输出字符串,或使用由函数分配的动态缓冲区并返回此信息。
>>You are attempting to return a pointer to a local variable, don''t do
this. Either pass in the output string, or use a dynamic buffer
allocated by the function and return this.




Ian,如果我重新执行* strReturn指向的字符串数组strTmp,我将获得同样的混乱。


你能用传入输出字符串来解释你的意思吗?什么

我会改变我的代码来做到这一点?很抱歉要求用勺子喂食。



Ian, if i retrun the string array strTmp that *strReturn points to, I
get the same mess.

Can you explain what you mean by "pass in the output string"? What
would I change in my code to do that? Sorry to ask to be spoon fed.



添加一个额外的参数:


char * FixString(const char * strIn, char * strOut)


注意将strIn更改为const,你不会在你的

函数中更改它。在你使用strTmp的地方使用strOut。


然后你可以用它来调用它:

const char * strOut ="这是一行来自一个网页< br>"


char * backBuff = char [someSize];


char * strBack = FixString(strOut, backBuff);


-

Ian Collins。

Add an extra parameter:

char* FixString( const char* strIn, char* strOut )

Note the change of strIn to const, you aren''t changing it in your
function. Use strOut where you use strTmp.

You can then call it with:

const char *strOut = "This is a line from a web page <br>"

char *backBuff = char[someSize];

char *strBack = FixString(strOut, backBuff);

--
Ian Collins.


这篇关于C字符串不从函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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