0

My goal is to call a member of a struct without using the actual member. It sounds confusing but here's my sample code for further explanation:


#include <stdio.h>
#include <conio.h>
#include <string.h>

typedef struct record
{
    char name[50];
}REC;

int main()
{
    REC r;
    char input[50] = "name"; //goal is to access r.name
    char test1[50] = "Successful!";
    r.input = test1; //This returns an error obviously
}

I declared char input to "name" as one of REC struct members. When compiled, error returns with "REC has no member named 'input'". How can i use 'input' to call one of rec struct members? Thank you in advance.

diegz25
  • 45
  • 1
  • 6
  • @RSahu While your marked dupe is related, it isn't the answer to this question IMO. The answer to this question is simply "you can't; C++ doesn't have reflection" – Miles Budnek Jul 18 '19 at 04:06
  • @MilesBudnek, agreed. – R Sahu Jul 18 '19 at 04:07
  • Related: https://stackoverflow.com/questions/14187217/invalid-array-assignment and https://stackoverflow.com/questions/4118732/c-array-assign-error-invalid-array-assignment – R Sahu Jul 18 '19 at 04:07
  • 1
    I was going to mark this as a dupe of [this question](https://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application), but someone removed the [tag:c++] tag. If you want a C++ answer that's the one. Maybe [this question](https://stackoverflow.com/questions/1353022/reflection-support-in-c) is a good C dupe? – Miles Budnek Jul 18 '19 at 04:17
  • Possible XY problem. Why do you want to do this? What problem are you trying to solve? – paddy Jul 18 '19 at 04:27
  • 1
    There is no `name2` anywhere in the code. What exactly are you trying to do? – Rishikesh Raje Jul 18 '19 at 04:31
  • No, that is not what a struct is, a struct just describes the layout of a piece of memory. If you want have some kind lookup of names you need another type of data structure, like a hash table or map - which you would need to code yourself since C is a low-level language.. – AndersK Jul 18 '19 at 04:34

2 Answers2

0

Like this, basically:

if (!strcmp(input, "name"))
    strcpy(r.name, test1);
else
    printf("Invalid field!\n");

C does not provide a way to access struct fields based on their name.

Perhaps you can think of more clever ways to write the above code, but whichever way you do it, you'll need to write a list of all the possible fields yourself.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • I've already had this idea. I just tried to look if there are more "direct" ways to access the members of struct through a representation, "reflection" as they were saying. However, this is approach (your answer) is acceptable, although on a limited basis. Thank you – diegz25 Jul 24 '19 at 02:49
0

I didn't figure out your goal. It seems that you want the member name of struct variable. But the member name if fixed when you define the struct. I only image maybe you have several different struct define. In that case, you can set struct member's value as following:

#include <stdio.h>

#define STR_SET(str,m,v) (str.m=(v))
#define MEM1 name
#define MEM2 age

typedef struct
{
    char* name;
}REC_1;

 typedef struct
{
    char* age;
}REC_2;

int main(void)
{
    REC_1 r1;
    REC_2 r2;
    char test1[50] = "Successful!";
    STR_SET(r1, MEM1, test1);
    STR_SET(r2, MEM2, test1);
}
alk
  • 69,737
  • 10
  • 105
  • 255
Koen
  • 311
  • 2
  • 11