Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Jonathan, you beat me to an answer, but this may be helpful as well. Here, <code>api.c</code> contains the (private) implementation, and <code>api.h</code> provides the interface to be consumed by other code such as <code>main.c</code>.</p> <pre><code>// main.c: uses only the public interface to the private code #include "api.h" int main(int argc, char *argv[]) { void *foo; foo = create_foo("five", 5); print_foo(foo); delete_foo(foo); } // EOF main.c </code></pre> <p><br></p> <pre><code>// api.h: the public interface #ifndef _api_h_ #define _api_h_ void *create_foo(char *name, int number); void print_foo(void *foo); void delete_foo(void *foo); #endif // _api_h_ </code></pre> <p><br></p> <pre><code>// api.c: the private implementation #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; // The real structure is private to the implementation. typedef struct { char name[20]; int number; } real_struct; // Create a new structure, initialize, return as ptr-to-void. void *create_foo(char *name, int number) { real_struct *s = malloc(sizeof(real_struct)); strcpy(s-&gt;name, name); s-&gt;number = number; return (void *) s; } // Print the data. void print_foo(void *foo) { real_struct *s = (real_struct *) foo; printf("name: %s, number: %d\n", s-&gt;name, s-&gt;number); } // Release the memory. void delete_foo(void *foo) { free(foo); } // EOF api.c </code></pre> <p>This code should compile and run:</p> <pre><code>$ gcc -o foo main.c api.c $ ./foo name: five, number: 5 </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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