使用struct字段调用函数,该字段恰好是一个数组作为参数 [英] Call function with struct field that happens to be an array as argument

查看:70
本文介绍了使用struct字段调用函数,该字段恰好是一个数组作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于结构:

 typedef struct s {
char words [30]
char text [100]
} myS;



和函数:

 void c(char * param)
{
/ / code
}



和变量:

 myS a; 





我尝试了什么:



我如何传递价值结构字段对函数的影响?

我试过

 c(a.words); 

但这告诉我表达式必须有一个结构或联合类型。

解决方案

它可能是单词定义中缺少的分号 - 添加它,以及它适用于我:

 typedef struct s {
char words [30];
char text [100];
} myS;

void c(char * param)
{
* param ='X';
}

myS a;
int main()
{
strcpy(a.words,one,two,three \ n);
strcpy(a.text,Hello,good eveniong,welcome.\\\
);
c(a.words);
c(a.text);
printf(a.words);
printf(a.text);
返回0;
}

给我

 Xne,两个,三个
Xello,很好,甚至欢迎。


...程序用退出代码完成0
按ENTER退出控制台。


Given the struct:

typedef struct s {
    char words[30]
    char text[100]
} myS;


and the function:

void c(char *param)
{ 
    //code
}


and the variable:

myS a;



What I have tried:

How would I pass the value of the words field of the struct to the function?
I tried

c(a.words);

but this tells me the expression must have a struct or union type.

解决方案

It's probably the missing semicolon on the definition of words - add it, and it works for me:

typedef struct s {
    char words[30];
    char text[100];
} myS;

void c(char *param)
{ 
    *param = 'X';
}

myS a;
int main()
{
    strcpy(a.words, "one, two, three\n");
    strcpy(a.text, "Hello, good eveniong, and welcome.\n");
    c(a.words);
    c(a.text);    
    printf(a.words);
    printf(a.text);
    return 0;
}

Gives me

Xne, two, three                                                                                                                                              
Xello, good eveniong, and welcome.                                                                                                                           
                                                                                                                                                             
                                                                                                                                                             
...Program finished with exit code 0                                                                                                                         
Press ENTER to exit console.                                   


这篇关于使用struct字段调用函数,该字段恰好是一个数组作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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