-4

Good day,

I would like to reference a structure in a function by using a variable to store its name. Is this possible to do something like this in C++?

Definitely, all existing structures will be declared and initialised before any call is made (probably as global) and I will build in a check to make sure that only existing structures are referenced.

I would like something in this spirit:

struct StructName
{
...stuff
}a,b,c;

StructName a;
StructName b;
.
.
.

etc. including setting required values (in initialisation or elsewhere in code as needed)

and then I would have something like this to call from another portion of code:

void myFunction(char someInput)
{
some stuff
some stuff
externalFunction(static parameter, static parameter, _someInput_, static parameter);
yet some other stuff
}

where somInput is either a,b or c.

Please bear in mind I am a beginner with C, with little to no formal training in subject matter.

Thank you.

edit: If it was just myself, I would make do with case switch for someInput, referencing the structure directly in each case, but this part of a code is meant to be extendable by a non-programmer who would supply structures themselves, I would provide to him a template of structure initialisation code, and he would add the initialisation code to a specified place in the code, ammend the list of allowed names and compile the library.

Fabo.sk
  • 116
  • 1
  • 10
  • This is normally what pointers are for. Don't try to pass the *name* of the relevant struct, pass a pointer to it. – Steve Jessop Mar 02 '13 at 22:12
  • Thanks @SteveJessop - only problem being, I don't actually know how to use them. Time to hit the books, I reckon. – Fabo.sk Mar 02 '13 at 22:26
  • No, the pointer type you need is `StructName*` (or that would have to be `struct StructName *` in C, I'm not clear which language you're using). You need to learn the basics of the language, StackOverflow can answer one point at a time but it isn't a tutorial. – Steve Jessop Mar 02 '13 at 22:42
  • Ah, dreaded send on pressing enter. Actually I wanted to write this: void myFunction(struct* someInput){ externalFunction(staticparam, *someInput, staticparam); } to by called then by myFunction(&a); ? Thank you, I appreciate that StackOverflow is not a tutorial, however I simply did not know to use pointers, as I have never needed them before. As mentioned, I have no formal training in C/C++, I am a hobbyist that tries to write something for his hobby, one could tell. – Fabo.sk Mar 02 '13 at 22:47

2 Answers2

1

You cannot convert a char or a char const * (runtime data) into a type (compile time information).

[edit] Well, actually you can with something like the following code, but since it uses templates, it will still be available only at the compile time, so you will not be able to pass function parameters, for example.

template < char C >
struct GetType;

template < >
struct GetType< 'a' > {
    typedef struct { int foo; } type; };

template < >
struct GetType< 'b' > {
    typedef struct { int bar; } type; };

GetType< 'a' >::type foo;
GetType< 'b' >::type bar;
Maël Nison
  • 7,055
  • 7
  • 46
  • 77
  • does this definitely mean that what I want to do is impossible? (by using something like pointers or so - I have just a slight idea what they actually do) If so, I can get around the problem by having the partner run a console application that will provide a valid .cpp file (that uses case switch) that he can then include in the project to compile, though while that has the advantage of allowing him to use his data format, it has the disadvantage of, well, creating a code that has to be copied somewhere to be compiled... – Fabo.sk Mar 02 '13 at 22:18
0

Variable names disappear as part of the compilation step(s) in C and C++.

Typically, there are two scenarios that solve the type of problem you are describing:

  1. User input corresponds to specific variable name.
  2. You don't actually want the "name" of the variable, but just need a way to associate different data with different parts of your code.

In the second, simpler case, just use an array, and use the index to the element you want as the way to associate the correct data.

In the first case, you can use a std::map<std::string, structname&>. A std::map acts sort of like an array, but it is indexed by the first type give in the template parameters to std::map - so in this case, you can use std::string as an index.

Something like this:

#include <map>
#include <iostream>
#include <string>

struct StructName
{
    int x;
};

std::map<std::string, StructName *> vars;

StructName a;
StructName b;

void registerVars()
{
    vars["a"] = &a;
    vars["b"] = &b;
}


int main()
{
    std::string v;

    registerVars();

    while(std::cin >> v)
    {
    std::cin.ignore(1000, '\n');

    if (vars.find(v) == vars.end())
    {
        std::cout << "No such variable" << std::endl;
    }
    else
    {
        vars[v]->x++;
        std::cout << "variable " << v << " is " << vars[v]->x << std::endl;
    }
    }
    return 0;
}
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227