字符串和功能 [英] strings and functions

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

问题描述



我习惯Delphi和VB,其中函数可以返回字符串。我最近开始学习C的
我的调查结果是你可以用

外部函数构建字符串,但是函数不能返回

字符串本身相反,它需要更新一个数组变量或

指向一个数组。正确吗?


下面是一个''简单''测试,用于在

外部函数(和外部文件)中创建的字符串。根据我得到的结果(_ @),

我知道我不完全得到它然而。任何帮助将不胜感激。


//MAIN.C


#include< stdio.h>


main()


{


char * data_from_function;


my_function( data_from_function);


printf(我的函数的数据结果:%s \ n);


}


//MYFUNCTION.C


my_function(char * strData)


{


strData =" HELLO WORLD \ n";


}


(Linux 9 i386)

#gcc main.c myfunction.c


#。/ a.out


数据结果来自我的职责:_ @

-

通过 http发表://dbforums.com

解决方案

c_monty写道:

我习惯了Delphi和VB,函数可以返回字符串。我最近开始学习C和我的发现是你可以有外部函数构建字符串,但是函数不能返回
字符串本身,而是需要更新一个变量数组或
指向数组。正确吗?

下面是一个''简单''测试,用于处理在外部函数(和外部文件)中创建的字符串。根据我得到的结果(_ @),
我知道我没有完全得到它。然而。任何帮助将不胜感激。

// MAIN.C

#include< stdio.h>
main()


main()函数应该有一个返回类型:

int main(void)


{
char * data_from_function;
my_function(data_from_function);
printf(我的函数的数据结果:%s \ n;);


或许这应该是:

printf(" my_function的数据结果:%s \ n",

data_from_function); / *你忘了这个* /}
//MYFUNCTION.C
my_function(char * strData)
{
strData =" HELLO WORLD \ n" ;;
}


指针按C中的值传递。

如果要更改指针,请传递地址

或指向指针的指针:

void my_function(char * * strData)

{

* strData =" Hellow World \ n" ;

}


或将数据复制到原始

数组的位置:

void my_other_function(char * strData)

{

strcpy(strdata,Hello again.\ n);

return; < br $>
}


int main(无效)

{

char string_one [640];

char * string_two;


my_function(& string_two);

my_other_function(string_one);

printf("结果:\ n%s \ n%s \ n",

string_one,string_two);

返回0;

}
(Linux 9 i386)
#gcc main.c myfunction.c
#。/ a.out

来自My Function的数据结果:_ @
-
通过 http://dbforums.com 发布


-

托马斯马修斯

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

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html


c_monty< me *********@dbforums.com>写道:

我习惯了Delphi和VB,其中函数可以返回字符串。我最近开始学习C和我的发现是你可以有外部函数构建字符串,但是函数不能返回
字符串本身,而是需要更新一个变量数组或
指向数组。正确?


正确。

下面是一个''简单''测试,用于处理在
外部函数中创建的字符串(和外部函数)文件)。根据我得到的结果(_ @),
我知道我没有完全得到它。然而。任何帮助将不胜感激。

// MAIN.C

#include< stdio.h>

main()


int main(无效)

{
char * data_from_function;

my_function(data_from_function);

printf(我的函数的数据结果:%s \ n);


你的意思是'printf(我的函数的数据结果:%s \ nn,data_from_function);''?

}


在结束括号前插入return 0;。

// @YYFUNCTION.C

my_function(char * strData)
{
strData =" HELLO WORLD \ n";
}


`strData''是`my_function''的本地。你让`strData''指向一个字符串

literal。然后函数执行结束,'strData''的值是

忘记了。


你可以返回一个字符串文字(更准确地说:指向来自这样一个函数的第一个

字符串文字):

#include< stdio.h>


const char * my_function(无效)

{

返回< HELLO WORLD" ;;

}


int main(void)

{

printf("数据来自我的函数:%s \ n",my_function());

返回0;

}

无法修改字符串文字。如果你需要修改字符串,你必须将它复制到一个数组:

#include< stdio.h>

#include < string.h>


void my_function(char * const buffer)

{

strcpy(buffer," HELLO WORLD");

}


int main(无效)

{

/ *必须足够大的字符串,

包括终止''\ 0''字符。 * /

char my_buffer [12];


my_function(my_buffer);


my_buffer [1] = ''A'';


printf("数据来自我的函数:%s \ n",my_buffer);

返回0; < br $>
}


(Linux 9 i386)


< OT>

否这种事。 Linux目前的版本是2.4.21;开发分支

的版本是2.6.0-test3。

< / OT>

#gcc main.c myfunction.c




请至少使用以下标志调用gcc:-O -Wall -ansi -pedantic


我建议`-W ''除此之外。


Martin


c_monty写道:


我习惯了Delphi和VB,其中函数可以返回字符串。我最近开始学习C和我的发现是你可以有外部函数构建字符串,但是函数不能返回
字符串本身,而是需要更新一个变量数组或
指向数组。正确?


差不多。 C函数不能返回字符串只是

C函数无法返回数组的特例因为

C中的字符串只是一个'char''元素数组,格式化为

。推荐阅读:第6节阵列

和指针在comp.lang.c中常见问题

(FAQ)列表

http://www.eskimo.com/~scs/C-faq/top.html

下面是一个简单的测试,用于处理在外部函数(和外部文件)中创建的字符串。根据我得到的结果(_ @),
我知道我没有完全得到它。然而。任何帮助将不胜感激。

// MAIN.C

#include< stdio.h>

main()

{

char * data_from_function;

my_function(data_from_function);

printf("数据来自我的函数:%s \\ \
");


其他地方有一些小的solecisms,但这是第一个

实时错误:你已经使用过%s了说明者告诉printf()

输出一个字符串,但是你没有告诉printf()你希望它输出什么字符串

! printf()信任你,在这个地方看起来

如果你提供了一个字符串''s char *''指针,那么你需要提供一个,获得一个某种垃圾,然后没有

保证会发生什么。你已经登陆了名为Undefined Behavior的危险地带。

}

// @YYFUNCTION.C

my_function(char * strData)



strData =" HELLO WORLD \ n" ;;


这是'第二个真实的实时错误:你不明白

C参数是按值传递的,而不是通过引用传递的。 />
请参阅FAQ中的问题4.8。

}

(Linux 9 i386)

#gcc main.c myfunction。 c


因为你在C中仍然有些不稳定,所以提高gcc的警告水平会是一个好主意。我使用


gcc -Wall -W -ansi -pedantic -O2 ...


....除了我省略 -ansi -pedantic对于那些

不能容忍这种刚性的程序,我提高了优于

级别高于-O2的程度。当情况需要时。

#。/ a.out

来自我的函数的数据结果:_ @




本来可以是任何东西,或者什么都没有。

未定义的行为是不可确定的。周围的朋友

在这里喜欢说U.B.可能会让恶魔飞出来。

。多年前,在同一个新闻组中,人们已经拥有了更多富有想象力的U.B.例子,但

鼻子恶魔似乎挤掉了创造力。


-
Er ********* @ sun.com



I am used to Delphi and VB, where functions can return strings. I
recently starting learning C and my findings are that you can have
external functions build strings, but the function cannot return the
string itself, rather, it needs to update a variable that is an array or
points to an array. Correct?

Below is a ''simple'' test to work with a string that was created in an
external function (and external file). Based on the result I get (_@),
I know I don''t fully "get it" yet. Any help would be appreciated.

//MAIN.C

#include<stdio.h>

main()

{

char *data_from_function;

my_function(data_from_function);

printf("Data results from My Function: %s\n");

}

//MYFUNCTION.C

my_function(char *strData)

{

strData = "HELLO WORLD\n";

}

(Linux 9 i386)

#gcc main.c myfunction.c

#./a.out

Data results from My Function: _@
--
Posted via http://dbforums.com

解决方案

c_monty wrote:

I am used to Delphi and VB, where functions can return strings. I
recently starting learning C and my findings are that you can have
external functions build strings, but the function cannot return the
string itself, rather, it needs to update a variable that is an array or
points to an array. Correct?

Below is a ''simple'' test to work with a string that was created in an
external function (and external file). Based on the result I get (_@),
I know I don''t fully "get it" yet. Any help would be appreciated.

//MAIN.C

#include<stdio.h>
main()
The main() function should have a return type:
int main(void)

{
char *data_from_function;
my_function(data_from_function);
printf("Data results from My Function: %s\n");
Perhaps this should have been:
printf("Data results from my_function: %s\n",
data_from_function); /* you forgot this */ } //MYFUNCTION.C
my_function(char *strData)
{
strData = "HELLO WORLD\n";
}
Pointers are passed by value in C.
If you want to change a pointer, pass the address
or a pointer to the pointer:
void my_function(char * * strData)
{
*strData = "Hellow World\n";
}

or copy the data to the location of the original
array:
void my_other_function(char * strData)
{
strcpy(strdata, "Hello again.\n");
return;
}

int main(void)
{
char string_one[640];
char * string_two;

my_function(&string_two);
my_other_function(string_one);
printf("Results:\n%s\n%s\n",
string_one, string_two);
return 0;
}

(Linux 9 i386)
#gcc main.c myfunction.c
#./a.out

Data results from My Function: _@
--
Posted via http://dbforums.com


--
Thomas Matthews
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html


c_monty <me*********@dbforums.com> writes:

I am used to Delphi and VB, where functions can return strings. I
recently starting learning C and my findings are that you can have
external functions build strings, but the function cannot return the
string itself, rather, it needs to update a variable that is an array or
points to an array. Correct?
Correct.
Below is a ''simple'' test to work with a string that was created in an
external function (and external file). Based on the result I get (_@),
I know I don''t fully "get it" yet. Any help would be appreciated.

//MAIN.C

#include<stdio.h>

main()
int main (void)
{
char *data_from_function;

my_function(data_from_function);

printf("Data results from My Function: %s\n");
Do you mean `printf("Data results from My Function: %s\n", data_from_function);''?
}
Insert `return 0;'' before the closing brace.

//MYFUNCTION.C

my_function(char *strData)
{
strData = "HELLO WORLD\n";
}
`strData'' is local to `my_function''. You make `strData'' point to a string
literal. Then the function execution ends, and the value of `strData'' is
forgotten.

You can return a string literal (more precisely: a pointer to the first
character of string literal) from a function like this:
#include <stdio.h>

const char *my_function (void)
{
return "HELLO WORLD";
}

int main (void)
{
printf ("Data results from My Function: %s\n", my_function ());
return 0;
}
A string literal cannot be modified. If you need to modify the string, you
must copy it to an array:
#include <stdio.h>
#include <string.h>

void my_function (char *const buffer)
{
strcpy (buffer, "HELLO WORLD");
}

int main (void)
{
/* Must be large enough for the string,
including the terminating ''\0'' character. */
char my_buffer [12];

my_function (my_buffer);

my_buffer [1] = ''A'';

printf ("Data results from My Function: %s\n", my_buffer);
return 0;
}

(Linux 9 i386)
<OT>
No such thing. Linux is currently at version 2.4.21; the development branch
is at version 2.6.0-test3.
</OT>
#gcc main.c myfunction.c



Please invoke gcc with at least the following flags: -O -Wall -ansi -pedantic

I recommend `-W'' in addition to that.

Martin


c_monty wrote:


I am used to Delphi and VB, where functions can return strings. I
recently starting learning C and my findings are that you can have
external functions build strings, but the function cannot return the
string itself, rather, it needs to update a variable that is an array or
points to an array. Correct?
Pretty much. "C functions can''t return strings" is just
a special case of "C functions can''t return arrays," because
a string in C is just an array of `char'' elements formatted
in a particular way. Recommended reading: Section 6 "Arrays
and Pointers" in the comp.lang.c Frequently Asked Questions
(FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
Below is a ''simple'' test to work with a string that was created in an
external function (and external file). Based on the result I get (_@),
I know I don''t fully "get it" yet. Any help would be appreciated.

//MAIN.C

#include<stdio.h>

main()

{

char *data_from_function;

my_function(data_from_function);

printf("Data results from My Function: %s\n");
There are minor solecisms elsewhere, but this is the first
Real Live Error: You''ve used the "%s" specifier to tell printf()
to output a string, but you haven''t told printf() what string
you want it to output! printf() trusts you, looks in the place
where the string''s `char*'' pointer would have been if you''d
supplied one, gets some kind of garbage, and then there''s no
guarantee of what might happen. You''ve landed yourself in the
perilous land called Undefined Behavior.

}

//MYFUNCTION.C

my_function(char *strData)

{

strData = "HELLO WORLD\n";
Here''s the second Real Live Error: You don''t understand
that C arguments are passed by value, not by reference.
See Question 4.8 in the FAQ.
}

(Linux 9 i386)

#gcc main.c myfunction.c
Since you''re still somewhat shaky in C, it would be a
good idea to crank up gcc''s warning levels a bit. I use

gcc -Wall -W -ansi -pedantic -O2 ...

.... except that I omit "-ansi -pedantic" for programs that
can''t tolerate such rigidity, and I raise the optimization
level higher than "-O2" when circumstances warrant it.
#./a.out

Data results from My Function: _@



Could have been anything at all, or nothing at all.
Undefined Behavior is, well, undefinable. Folks around
here are fond of saying U.B. might make demons fly out
of your nose. Years ago on this same newsgroup, people
had a wider variety of imaginative U.B. examples, but the
nasal demons seem to have crowded out the creativity.

--
Er*********@sun.com


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

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