Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I assume that you're attempting to recreate what is allowed in more dynamic languages, such as this PHP example:</p> <pre><code>$QueryName = "GetAirports"; $$QueryName = array(/*...*/); </code></pre> <p>Where by altering the value of <code>$QueryName</code> variable you can refer to another object?</p> <p>If so, the simple answer is: you can't do that in C.</p> <p>What you can do, however, is use a single variable to <em>point to</em> multiple instances at different times.</p> <pre><code>Query *query_ptr = &amp;myFirstQueryObject; query_ptr = &amp;mySecondQueryObject; /* etc */ </code></pre> <p>However from your example code it appears you're simply wanting to allocate a structure? If so:</p> <pre><code>typedef struct Query { /* fields here, e.g: */ int id; int age; } Query_t; int main() { Query_t *query = malloc(sizeof(Query_t)); query-&gt;id = 1; query-&gt;age = 0; /* etc. */ return 0; } </code></pre> <p>Perhaps look up C structs and pointers.</p> <p><strong>EDIT</strong> so from further comments apparently you're wanting to create a map of <code>char*</code> <em>name</em> to <code>Query</code> object? There are several approaches for this, but the simplest is to create two arrays:</p> <pre><code>char *names[]; Query *queries[]; </code></pre> <p>As long as both arrays have the same number of elements, and the *n*th element in <code>names</code> corresponds with the *n*th in <code>queries</code>, you can iterate through <code>names</code> until you find your matching string, then use the current index to dereference the appropriate object in <code>queries</code>, or <em>vice versa</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload