Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access members of a `struct' according to a value of a string?

Tags:

c

string

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;
};

There is a varibale let's say

char *string="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?

like image 606
Daylite Avatar asked Feb 17 '23 01:02

Daylite


1 Answers

Nope, it's not.

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 ...
like image 185
Kiril Kirov Avatar answered Apr 27 '23 11:04

Kiril Kirov