字符串作为函数的参数/参数 [英] Strings as parameters/arguments to a function
问题描述
我似乎无法弄清楚如何将字符串作为参数发送到
函数。
我有这样的结构:
结构列表{
char thread [25];
struct list * previous;
struct list * next;
};
struct list * test;
当我调用这个函数时我想给它一个字符串(char数组)作为
参数,它将被写入列表结构中的线程字段:
void fill(char [] arg){
test-> thread = arg;
}
但似乎它的不是这样做的,希望有人可以提供帮助。
JS
I can''t seem to figure out how to send a string as a paramenter to a
function.
I have this structure:
struct list {
char thread[25];
struct list *previous;
struct list *next;
};
struct list *test;
When I call this function I would like to give it a string (char array) as
argument that will be writtin to the thread field in the list struct:
void fill(char[] arg){
test->thread = arg;
}
But it seems that its not the way to do it, hope someone can help.
JS
推荐答案
这是怎么回事应该完成....
无效填充(char arg [])
{
strcpy(test->线程,arg);
}
u可能要做 - #include< string.h>如果你使用的是c ++编译器
..
JS写道:
this is how it should be done ....
void fill(char arg[])
{
strcpy(test->thread,arg);
}
u may have to do - #include <string.h> if you are using a c++ compiler
..
JS wrote:
我似乎无法弄清楚如何发送一个字符串作为
函数的参数。
我有这样的结构:
struct list {
char thread [25];
struct list * previous;
struct list * next;
};
struct list * test;
当我调用这个函数时我想给它是一个字符串(char
数组)作为参数,将写入列表结构中的线程字段:
void fill(char [] arg){
> test-> thread = arg;
}
但似乎不是这样做的,希望有人可以提供帮助。
> JS
I can''t seem to figure out how to send a string as a paramenter to a
function.
I have this structure:
struct list {
char thread[25];
struct list *previous;
struct list *next;
};
struct list *test;
When I call this function I would like to give it a string (char array) as argument that will be writtin to the thread field in the list struct:
void fill(char[] arg){
test->thread = arg;
}
But it seems that its not the way to do it, hope someone can help.
JS
ra **********@gmail.com 写道:
ra**********@gmail.com wrote:
这是应该怎么做的....
> void fill(char arg [])
{
strcpy(test-> thread,arg);
}
this is how it should be done ....
void fill(char arg[])
{
strcpy(test->thread,arg);
}
我也试过使用char * arg它也工作正常......有没有
的区别?
I have also tried to use char *arg and it also works fine...are there any
difference?
JS写道:
我似乎无法弄清楚如何将字符串作为参数发送到
函数。
<我有这样的结构:
struct list {
char thread [25];
struct list * previous;
struct list * next;
};
结构列表*测试;
当我调用这个函数时,我想给它一个字符串(char
数组)作为参数将是写入列表结构中的线程字段:
void fill(char [] arg){
test-> thread = arg;
>}
但似乎不是这样做的,希望有人可以提供帮助。
JS
I can''t seem to figure out how to send a string as a paramenter to a
function.
I have this structure:
struct list {
char thread[25];
struct list *previous;
struct list *next;
};
struct list *test;
When I call this function I would like to give it a string (char array) as argument that will be writtin to the thread field in the list struct:
void fill(char[] arg){
test->thread = arg;
}
But it seems that its not the way to do it, hope someone can help.
JS
请参阅 http://www.eskimo。 com /~scs / C-faq / q8.3.html
你可能想要这样的东西(没有编译或测试过的代码)
void fill(const char * arg)
{
/ *如果arg为NULL,以某种方式处理* /
size_t len = strlen(arg);
if(len< sizeof(test-> thread)){
memmove(test-> thread,arg,len + 1);
}
否则{
/ *不合适,交易* /
}
}
我假设你已经分配了清单,如
test = malloc(sizeof * test)?
-David
See http://www.eskimo.com/~scs/C-faq/q8.3.html
You probably want something like this (not compiled or tested code)
void fill(const char *arg)
{
/* if arg is NULL, deal somehow */
size_t len = strlen(arg);
if (len < sizeof(test->thread)) {
memmove(test->thread, arg, len+1);
}
else {
/* doesn''t fit, deal */
}
}
I''m assuming you have allocated the list, as in
test = malloc(sizeof *test)?
-David
这篇关于字符串作为函数的参数/参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!