如何根据字符串的值访问“struct"的成员? [英] How to access members of a `struct' according to a value of a string?

查看:27
本文介绍了如何根据字符串的值访问“struct"的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用字符串的值访问结构中的成员:

I would like to access a member within a struct by using the value of a string:

struct hello_world
{
           char rate;
           char ssid;
};

假设有一个变量

char *string="ssid";

我想使用这个字符串的值来引用 hello_world 结构中的 ssid 成员.这可能吗?

I would like to use the value of this string to refer to the ssid member within the hello_world struct. Is this possible?

推荐答案

不,不是.

您需要一个(长)if-else 语句来执行此操作.喜欢:

You need a (long) if-else statement, that will do this. Like:

struct hello_world hw;
char *string="ssid";

if( 0 == strcmp( "ssid", string ) )
{
     // use hw.ssid
}
else if ...

这篇关于如何根据字符串的值访问“struct"的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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