将字符串参数传递给funtion [英] Passing string parameter to funtion

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

问题描述

char word [50];


in = fopen(" test.txt"," r");


while(fscanf(in,"%s",& word)!= EOF)

{


/ *打印所有单词* /

/ * printf("%s \ n",& word); * /

/ *我想做什么:处理所有的话,有些想法:* /


process_word(word);


/ *此格式用于传递参数* /

/ * process_word(" test"); * /

}


好​​吧这不起作用,我不能让word-variable以正确的方式运行

格式。如何将字变量传递给函数process_word()。


我尝试过:


process_word(word [50]);

process_word(& word);

process_word(* word);

process_word(word);


函数:

void processword(char input [50]);


所以process_word(" test"); processword(char input [50]);有效但


process_word(& word); processword(char input [50]);不工作,

如何更改语法使其工作。

char word[50];

in = fopen("test.txt", "r");

while(fscanf(in,"%s",&word)!=EOF)
{

/* Print all words */
/* printf("%s\n",&word); */
/* What I want to do: Process all words, somethink like: */

process_word(word);

/* This format works passing parameters */
/* process_word("test"); */
}

Well this does not work, I cant get word-variable to function in right
format. How do I pass word-variable to function process_word().

I have tried:

process_word(word[50]);
process_word(&word);
process_word(*word);
process_word(word);

Function:
void processword(char input[50]);

So process_word("test"); to processword(char input[50]); works but

process_word(&word); to processword(char input[50]); is not working,
how to change the syntax to make it work.

推荐答案

< ma ****** @ hotmail.com在消息新闻中写道:
<ma******@hotmail.comwrote in message news:

char word [50];


in = fopen(test.txt,r);


while(fscanf(in,%s,& word)!= EOF)

{


/ *打印所有单词* /

/ * printf("%s \ n",& word) ; * /


/ *我想做什么:处理所有的话,有些想法:* /


process_word(word);


/ *此格式用于传递参数* /

/ * process_word(" test"); * /

}


好​​吧这不起作用,我不能让word-variable以正确的方式运行

格式。如何将字变量传递给函数process_word()。


我尝试过:


process_word(word [50]);

process_word(& word);

process_word(* word);

process_word(word);


函数:

void processword(char input [50]);


所以process_word(" test"); processword(char input [50]);有效但


process_word(& word); processword(char input [50]);不工作,

如何更改语法以使其工作。
char word[50];

in = fopen("test.txt", "r");

while(fscanf(in,"%s",&word)!=EOF)
{

/* Print all words */
/* printf("%s\n",&word); */
/* What I want to do: Process all words, somethink like: */

process_word(word);

/* This format works passing parameters */
/* process_word("test"); */
}

Well this does not work, I cant get word-variable to function in right
format. How do I pass word-variable to function process_word().

I have tried:

process_word(word[50]);
process_word(&word);
process_word(*word);
process_word(word);

Function:
void processword(char input[50]);

So process_word("test"); to processword(char input[50]); works but

process_word(&word); to processword(char input[50]); is not working,
how to change the syntax to make it work.



运算符的地址对于数组是多余的。 (这只是C的滑稽怪癖中的一个。)

process_word(word);



process_word(" test");


都是使用char *参数调用函数的有效方法。如果

首先不起作用,那么数据中存储的数据肯定有问题。您的输入读取代码有点粗糙,但是您打印的值是
。他们是对的吗?尝试在字符串

之间加上星号,以确保没有杂散空格。

您还应该使用while(fscanf(..)== 1)作为测试条件,而不是EOF。

该函数返回正确读取的字段数。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk / ~bgy1mm

The address of operator is redundant for an array. (That''s just one of the
funny quirks of C).
process_word(word);
and
process_word("test");

are both valid ways of calling the function with a char * argument. If the
first doesn''t work, there must be something wrong with the data stored in
the array. Your input-reading code is a bit crude, however you are printing
out the values. Are they correct? Try putting asterisks between the string
to make sure there are no stray spaces.
You should also use while( fscanf(..) == 1) as the test condition, not EOF.
The function returns the number of fields read correctly.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


ma *** ***@hotmail.com 写道:

char word [50];


in = fopen (test.txt,r);
char word[50];

in = fopen("test.txt", "r");



检查fopen()

是成功还是失败是个好主意...

It would be a good idea to check whether the fopen()
succeeded or failed ...


while(fscanf(in,"%s",& word)!= EOF)
while(fscanf(in,"%s",&word)!=EOF)



摆脱` &安培; ''。请参阅

中的问题6.12和12.12b comp.lang.c常见问题(FAQ)列表

< http://www.c-faq.com /了解原因。另请参阅

问题12.20(及其脚注),了解为什么不受限制的

"%s"是危险的。

Get rid of the `&''. See Questions 6.12 and 12.12b in
the comp.lang.c Frequently Asked Questions (FAQ) list at
<http://www.c-faq.com/to understand why. Also see
Question 12.20 (and its footnote) for why an unrestricted
"%s" is dangerous.


{


/ *打印所有单词* /

/ * printf (QUOT;%s\\\
",&安培;字); * /
{

/* Print all words */
/* printf("%s\n",&word); */



如果您取消评论,请删除`&''。

If you un-comment this, get rid of the `&''.


>


/ *我想做什么:处理所有的话,有些想法:* /


process_word(word);
>

/* What I want to do: Process all words, somethink like: */

process_word(word);



这是正确的,假设process_word(test)是

也正确。

This is right, assuming that process_word("test") is
also right.


/ *此格式有效传递参数* /

/ * process_word(" test"); * /

}


好​​吧这不起作用,我不能让word-variable以正确的方式运行

格式。如何将字变量传递给函数process_word()。
/* This format works passing parameters */
/* process_word("test"); */
}

Well this does not work, I cant get word-variable to function in right
format. How do I pass word-variable to function process_word().



你的意思是什么?不起作用?


-

Eric Sosman
es*****@ieee-dot-org.inva lid

What do you mean by "does not work?"

--
Eric Sosman
es*****@ieee-dot-org.invalid


" Malcolm McLean" < re ******* @ btinternet.comwrites:
"Malcolm McLean" <re*******@btinternet.comwrites:

< ma ****** @ hotmail.comwrote in message news:
<ma******@hotmail.comwrote in message news:

> char word [50];
>char word[50];



[...]

[...]


> ; / *此格式用于传递参数* /
/ * process_word(" test"); * /
}

这不起作用,我不能让word-variable以正确的格式运行。如何将字变量传递给函数process_word()。

我试过:

process_word(word [50]);
>/* This format works passing parameters */
/* process_word("test"); */
}

Well this does not work, I cant get word-variable to function in right
format. How do I pass word-variable to function process_word().

I have tried:

process_word(word[50]);



这会尝试传递char类型的值。因为它是一个

不存在的对象(刚好超过数组末尾的元素)的值,所以即使它是正确的,它也会是错误的。类型。

This tries to pass a value of type char. Since it''s the value of a
nonexistent object (the element just past the end of your array), it
would be wrong even if it were of the correct type.


> process_word(& word);
>process_word(&word);



Nope。

Nope.


> process_word( *字);
>process_word(*word);



Nope。

Nope.


> process_word(字);
>process_word(word);



这应该没问题。你确定你试过吗?

This should have been ok. Are you sure you tried it?


>功能:
void processword(char input [50]);

所以process_word(测试); processword(char input [50]);有效但

process_word(& word); processword(char input [50]);不起作用,
如何更改语法以使其工作。
>Function:
void processword(char input[50]);

So process_word("test"); to processword(char input[50]); works but

process_word(&word); to processword(char input[50]); is not working,
how to change the syntax to make it work.



你应该阅读comp.lang.c FAQ的第6部分。


一个函数可以'真的有一个数组类型的参数。如果你试图

声明这样的参数,它实际上是指针类型。所以这个

声明:


void processword(字符输入[50]);


的确意味着:


void processword(char * input);


请注意50。完全被忽略了。 (这是我最不喜欢的B $ b最喜欢的C语言功能之一。)

You should read section 6 of the comp.lang.c FAQ.

A function can''t really have a parameter of array type. If you try to
declare such a parameter, it''s really of pointer type. So this
declaration:

void processword(char input[50]);

really means this:

void processword(char *input);

Note that the "50" is completely ignored. (This is one of my least
favorite C language features.)


运算符的地址对于数组来说是多余的。 (这只是C中有趣的怪癖之一)。
The address of operator is redundant for an array. (That''s just one of the
funny quirks of C).



否,&运算符对于数组来说不是多余的。如果word是一个

数组(因为它在这里,声明为char word [50];"),那么& word是

的地址整个数组,类型为char(*)[50]的表达式。


在大多数情况下,表达式word评估为

数组的第一个元素,类型为char *。碰巧的是,这比一般的数组地址更有用了。

处理数组的常用方法是通过指向其第一个元素的指针。

注意你需要一些额外的机制来指示

数组的长度,或者你感兴趣的部分长度。

No, the "&" operator is not redundant for an array. If word is an
array (as it is here, declared as "char word[50];"), then &word is the
address of the entire array, an expression of type "char(*)[50]".

The expression "word", in most contexts, evaluates to the address of
the first element of the array, of type char*. As it happens, this is
useful much more often than the address of the array as a whole; the
usual way to deal with an array is via a pointer to its first element.
Note that you need to have some additional mechanism to indicate the
length of the array, or of the portion of it you''re interested in.


process_word(word);

和$ / b
process_word(" test");


都是使用char *参数调用函数的有效方法。如果

首先不起作用,那么数据中存储的数据肯定有问题。
process_word(word);
and
process_word("test");

are both valid ways of calling the function with a char * argument. If the
first doesn''t work, there must be something wrong with the data stored in
the array.



如果第一个不起作用,你需要非常清楚

什么不起作用手段。你试过的时候发生了什么?你有什么期望?b $ b预计会发生什么?这些有何不同?

If the first doesn''t work, you need to be very clear about what
"doesn''t work" means. What happened when you tried it? What did you
expect to happen? How do these differ?


您的输入读取代码有点粗糙,但是您打印的值为
。他们是对的吗?尝试在字符串

之间加上星号,以确保没有杂散空格。

您还应该使用while(fscanf(..)== 1)作为测试条件,而不是EOF。

该函数返回正确读取的字段数。
Your input-reading code is a bit crude, however you are printing
out the values. Are they correct? Try putting asterisks between the string
to make sure there are no stray spaces.
You should also use while( fscanf(..) == 1) as the test condition, not EOF.
The function returns the number of fields read correctly.



fscanf会针对某些类型的错误返回EOF。在某些情况下,它也可以返回
0。是的,如果您正在阅读单个项目,则检查

结果对1是有意义的;如果你得到1以外的其他东西,那么

可能会想要采取其他行动,具体取决于它是否b / b $ b $返回0或EOF。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

fscanf does return EOF for certain kinds of error. It can also return
0 in some cases. Yes, if you''re reading a single item, checking the
result against 1 makes sense; if you get something other than 1, you
might then want to take some other action depending on whether it
returned 0 or EOF.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于将字符串参数传递给funtion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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