Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View codebeaker-example.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#include <stdio.h>
#include <stdlib.h>
 
struct config_struct {
int port;
char *hostname;
};
 
typedef struct config_struct config;
 
void setup(config*);
void change(config*);
void set_hostname(config*, char*);
void get_hostname_into(config*, char**);
void teardown(config*);
void inspect(config*);
 
enum Property { Port, Hostname };
void get_property(config*, enum Property, void*);
void get_property(config* c, enum Property p, void* target) {
switch(p) {
case Port:
{
int *port;
port = (int *) target;
*port = c->port;
}
break;
case Hostname:
{
char *hostname;
hostname = (char *) target;
*hostname = c->hostname;
}
break;
}
}
 
int main() {
 
char* hostname;
config* c;
c = calloc( 1, sizeof(config));
 
setup(c);
inspect(c);
 
change(c);
inspect(c);
 
set_hostname(c, "test.com");
inspect(c);
 
get_hostname_into(c, &hostname);
inspect(c);
printf("retrieved hostname is %s (%p)\n", hostname, &hostname);
 
int get_port_into_here = 0;
get_property(c, Port, &get_port_into_here);
printf("genericly retrieved port is %d (%p)\n", get_port_into_here, &get_port_into_here);
 
char *get_hostname_into_here;
get_property(c, Hostname, &get_hostname_into_here);
printf("genericly retrieved hostname is %s (%p)\n", get_hostname_into_here, &get_hostname_into_here);
 
teardown(c);
printf("retrieved hostname is %s (%p) (after teardown)\n", hostname, &hostname);
 
return EXIT_SUCCESS;
}
 
void setup(config* c) {
c->port = 9933;
c->hostname = "localhost";
}
 
void change(config* c) {
c->port = 12345;
c->hostname = "example.com";
}
 
void set_hostname(config* c, char* new_hostname) {
c->hostname = new_hostname;
}
 
void get_hostname_into(config* c, char** where) {
*where = c->hostname;
}
 
void teardown(config* c) {
free(c);
}
 
void inspect(config* c) {
printf("c is at %p\n", c);
printf("c is %ld bytes\n", sizeof(*c));
printf("c->port is %d (%p)\n", c->port, &(c->port));
printf("c->hostname is %s (%p)\n", c->hostname, &(c->port));
}
View codebeaker-example.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
untitled: In function ‘get_property’:
untitled:33: warning: assignment makes integer from pointer without a cast
c is at 0x1040008c0
c is 16 bytes
c->port is 9933 (0x1040008c0)
c->hostname is localhost (0x1040008c0)
c is at 0x1040008c0
c is 16 bytes
c->port is 12345 (0x1040008c0)
c->hostname is example.com (0x1040008c0)
c is at 0x1040008c0
c is 16 bytes
c->port is 12345 (0x1040008c0)
c->hostname is test.com (0x1040008c0)
c is at 0x1040008c0
c is 16 bytes
c->port is 12345 (0x1040008c0)
c->hostname is test.com (0x1040008c0)
retrieved hostname is test.com (0x7fff63b704c0)
genericly retrieved port is 12345 (0x7fff63b704b4)
genericly retrieved hostname is (null) (0x7fff63b704a8)
retrieved hostname is test.com (0x7fff63b704c0) (after teardown)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.