Skip to content

Instantly share code, notes, and snippets.

@leehambley
Created September 4, 2011 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leehambley/dee4a9a75a30882ac6e2 to your computer and use it in GitHub Desktop.
Save leehambley/dee4a9a75a30882ac6e2 to your computer and use it in GitHub Desktop.
#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));
}
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