字符串作为函数的参数/参数 [英] Strings as parameters/arguments to a function

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

问题描述

我似乎无法弄清楚如何将字符串作为参数发送到

函数。


我有这样的结构:


结构列表{

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屋!

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