返回一个字符串 [英] return a string

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

问题描述

您好,


如何通过函数返回字符串。

我写了以下函数:


char prt_tralha(int num)

{

int i;

char tralha [num];


tralha ="#" ;;

for(i = 0; i< num-1; i ++)

strcpy (tralha,strcat(tralha,#));


返回tralha;

}


我真的很想使用它,因此:


int main()

{

printf("%s \ n",prt_tralha(5));


返回0;

}


但是当我编译它,gcc显示这条消息:


tmp.c:在函数`prt_tralha'':

tmp.c:16:错误:不兼容的类型赋值

tmp.c:20:警告:返回从指针生成整数而没有强制转换

tmp.c:20:warning:函数返回局部变量的地址


谢谢,


Nascimen to

Hello,

How to I do to return a string as a result of a function.
I wrote the following function:

char prt_tralha(int num)
{
int i;
char tralha[num];

tralha = "#";
for( i = 0; i < num-1; i++ )
strcpy(tralha, strcat(tralha,"#"));

return tralha;
}

And I really like to use it, thus:

int main()
{
printf("%s \n", prt_tralha(5));

return 0;
}

But when I compile it, gcc shows this message:

tmp.c: In function `prt_tralha'':
tmp.c:16: error: incompatible types in assignment
tmp.c:20: warning: return makes integer from pointer without a cast
tmp.c:20: warning: function returns address of local variable

Thanks,

Nascimento

推荐答案

Nascimento写道:
Nascimento wrote:
你好,

我如何作为函数的结果返回一个字符串。
我写了以下函数:

char prt_tralha(int num)
{
int i;
char tralha [num];

tralha ="#" ;;
for(i = 0;我< NUM-1; i ++)
strcpy(tralha,strcat(tralha,"#"));

返回tralha;
}

我真的很喜欢使用它,因此:

int main()
{/> printf("%s \ n",prt_tralha(5));

返回0;
}

但是当我编译它时,gcc会显示以下消息:

tmp.c:在函数`prt_tralha'':
tmp.c:16:错误:赋值中不兼容的类型
tmp.c:20:警告:返回从指针生成整数而没有强制转换
tmp.c:20:warning:函数返回地址局部变量

谢谢,

Nascimento
Hello,

How to I do to return a string as a result of a function.
I wrote the following function:

char prt_tralha(int num)
{
int i;
char tralha[num];

tralha = "#";
for( i = 0; i < num-1; i++ )
strcpy(tralha, strcat(tralha,"#"));

return tralha;
}

And I really like to use it, thus:

int main()
{
printf("%s \n", prt_tralha(5));

return 0;
}

But when I compile it, gcc shows this message:

tmp.c: In function `prt_tralha'':
tmp.c:16: error: incompatible types in assignment
tmp.c:20: warning: return makes integer from pointer without a cast
tmp.c:20: warning: function returns address of local variable

Thanks,

Nascimento




#include< stdio.h>

#include< stdlib.h>

const char * string_function(void)

{

static const char * my_text =" ;我的文字;

返回my_text;

}


int main(无效)

{

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

返回EXIT_SUCCESS;

}


从根本上说,你将一个指针传递给一个字符串;不是

字符串。


-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/ ~shiva / welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.comeaucomputing.com/learn/faq/

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍
http://www.sgi.com/tech/stl - 标准模板Li brary



#include <stdio.h>
#include <stdlib.h>
const char * string_function(void)
{
static const char * my_text = "My text";
return my_text;
}

int main(void)
{
printf("%s\n", string_function());
return EXIT_SUCCESS;
}

Fundamentally, you pass a pointer to a string; not
the string.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library




Nascimento写道:

Nascimento wrote:
你好,

我怎么做作为函数的结果返回一个字符串。
我写了以下函数:


有两种方法可以返回来自函数的字符串。一个是

只需退回...

char prt_tralha(int num)


I.E. char * ptr_tralha(int num)


这里我们返回一个指向char或string的指针。这个问题是

返回值不能是自动变量,所以你需要用malloc来工作




另一种方法是使字符串成为参数列表的一部分。这可能是首选的,因为内存管理发生在

以外的功能,并且(可以说)更容易。


IE char ptr_tralha(int num,char * ret)

{
int i;
char tralha [num];


假设后面的方法,tralha只是这个

函数的参数,而不是在这里声明。

tralha = "#" ;;
for(i = 0; i< num-1; i ++)
strcpy(tralha,strcat(tralha,"#"));
<返回tralha;


没有必要返回 tralha在这里,因为它是通过参数列表中的指针传递给



}

我真的很想使用它,因此:

int main()
{/> printf("%s \ n",prt_tralha(5));


试试这个:


char tralha [5];

ptr_tralha(5,tralha);

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

返回0;
}

但是当我编译它时,gcc显示了这条消息:

tmp.c:在函数`prt_tralha'':
tmp.c:16:错误:赋值中不兼容的类型


嗯,当然。函数是(是)返回一个字符,但是你想要使用它就好像它是一个字符*。

tmp.c:20:警告:返回从没有转换的指针生成整数
tmp.c:20:warning:函数返回局部变量的地址

谢谢,


希望这会有所帮助。我相信其他人也会有建议。


-Jason
Nascimento
Hello,

How to I do to return a string as a result of a function.
I wrote the following function:

There are two ways to "return" a string from a function. One is to
simply return it...
char prt_tralha(int num)
I.E. char * ptr_tralha(int num)

Here we return a pointer to char, or string. The problem with this is
the return value can''t be an automatic variable, so you''ll need to work
with malloc.

Another method is to make the string part of the argument list. This is
probably prefered, since the memory management takes place outside of
the function and is (arguably) easier.

I.E. char ptr_tralha(int num, char *ret)
{
int i;
char tralha[num];
Assuming the later method, tralha would simply be the argument to this
function, and not declared here.

tralha = "#";
for( i = 0; i < num-1; i++ )
strcpy(tralha, strcat(tralha,"#"));

return tralha;
There would be no need to "return" tralha here, since it was passed in
via pointer in the argument list.
}

And I really like to use it, thus:

int main()
{
printf("%s \n", prt_tralha(5));
try this instead:

char tralha[5];
ptr_tralha(5,tralha);
printf("%s \n", tralha);

return 0;
}

But when I compile it, gcc shows this message:

tmp.c: In function `prt_tralha'':
tmp.c:16: error: incompatible types in assignment
Well, of course. The function is (was) returning a char, but you were
trying to use it as if it were a char *.
tmp.c:20: warning: return makes integer from pointer without a cast
tmp.c:20: warning: function returns address of local variable

Thanks,
Hope this helps. I''m sure others will have suggestions as well.

-Jason
Nascimento





Nascimento,

你不能为这样的字符串指定一个字符:

tralha ="#"

你可以这样做:tralha [0] =''#'';

一个更优雅的代码用于你的目的是这样的:


--- tralha.c ---


void

ptr_tralha(char * s)

{

while(* s!=''\ 0'')/ *虽然s的内容不同

*来自NULL * /

* s ++ =''# ''; / * s =''#''。增量s * /

}

int

main(无效)

{

char s [5];

ptr_tralha(s);

puts(s);


返回0;

}

Nascimento,
you cannot assign a char to a string like that:
tralha = "#"
you can do it like this: tralha[0]=''#'';
A more elegant code for your purpose goes like this:

--- tralha.c ---

void
ptr_tralha(char *s)
{
while (*s != ''\0'') /* while the contents of s are different
* from NULL */
*s++ = ''#''; /* s = ''#'' . increment s */
}
int
main(void)
{
char s[5];
ptr_tralha(s);
puts(s);

return 0;
}


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

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