Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should give your structs names. For example,</p> <pre><code>struct foo { char * str1; float tt1; }; </code></pre> <p>Then you can declare your instances like this:</p> <pre><code>struct foo var1 = { "This is me", 12 }; </code></pre> <p>Then you can cast the <code>void *</code> to a <code>struct foo *</code> in your function:</p> <pre><code>void printStruct(void * ptr) { printf("str=%s\n", ((struct foo *)ptr)-&gt;str1); } </code></pre> <p>And you can call it like this:</p> <pre><code>int main(...) { struct foo var1 = { "This is me", 12 }; printStruct(&amp;var1); } </code></pre> <p><strong>Update:</strong> Commenter Johannes was correct, my answer doesn't account for the fact that the original poster said he might not know the type of each struct. Updated solution is below:</p> <p>Since you know that all your structs contain a <code>char * str1</code>, you can take advantage of C's "flat memory model" by casting your structs directly to generic, tagged struct that only contains <code>char * str1</code>. <strong>This will only work if <code>char * str1</code> is the <em>first</em> element of the structs</strong>.</p> <pre><code>struct base { char * str1; }; struct { char * str1; float tt1; } var1 = { "This is me", 12 }; struct { char * str1, int tt2; } var2 = { "This is me", 18 }; void printStruct(void * ptr) { printf("str is %s\n", ((struct base *)ptr)-&gt;str1); } </code></pre> <p>This is a pretty dirty hack, though, IMO. If there are other members that you want to share, you won't be able to use this same trick.</p> <p>Another option is to define a <code>struct</code> that uses a union with an enumerator to keep track of which type is actually being used in the union:</p> <pre><code>enum { FLOAT_TYPE, INT_TYPE }; struct foo { char * str1; union { float tt1; int tt2; } numericValue; int unionType; }; </code></pre> <p>This would let you define variables like this:</p> <pre><code>struct foo var1 = { .str1 = "This is me", .numericValue.tt1 =12, .unionType = FLOAT_TYPE }; struct foo var2 = { .str1 = "This is me", .numericValue.tt2 =18, .unionType = INT_TYPE }; </code></pre> <p>Then you don't need to handle for different types of structs, you can cast your void pointer to <code>struct foo *</code> instead:</p> <pre><code>void printStruct(void * ptr) { struct foo * p = (struct foo *)ptr; printf("string is %s\n", p-&gt;str1); } </code></pre> <p>This might be require a bit more work but it's much cleaner, IMO.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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