Skip to content

Instantly share code, notes, and snippets.

@vlasovskikh
Created July 11, 2010 17:10
Show Gist options
  • Save vlasovskikh/471685 to your computer and use it in GitHub Desktop.
Save vlasovskikh/471685 to your computer and use it in GitHub Desktop.
Abstract data types in plain old C
syntax:glob
main
*.o
*~
typedef int Image;
Image *make_image(int foo);
char access_pixel(Image *img, int r, int c);
#include <stdlib.h>
#include <image.h>
typedef struct {
int foo, bar;
} IplImage;
Image *make_image(int foo) {
IplImage *img = (IplImage *) malloc(sizeof(IplImage));
if (!img)
return NULL;
img->foo = foo;
img->bar = foo + 14;
return (Image *) img;
}
char access_pixel(Image *img, int r, int c) {
IplImage *iplimg = (IplImage *) img;
return iplimg->foo + r + c;
}
#include <stdio.h>
#include <image.h>
int main() {
Image *img = make_image(42);
if (!img) {
fprintf(stderr, "error: querying an image for 42 failed\n");
return 1;
}
struct {
char c;
} a = {'a'};
printf("access_pixel(img, 0, 0) = %d\n", access_pixel(&a, 0, 0));
return 0;
}
SOURCES = image_cv.c main.c
OBJECTS = $(patsubst %.c,%.o,$(SOURCES))
CC = gcc
CFLAGS = -std=c99 -Wall -Wextra -Werror -pedantic
INCLUDE = -I.
default: all
all: $(OBJECTS) main
main: main.o image_cv.o
gcc -o main main.o image_cv.o
image_cv.c: image.h
%.o: %.c
$(CC) -c -o $@ $(CFLAGS) $(INCLUDE) $<
clean:
rm -f $(OBJECTS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment