Created
July 11, 2010 17:10
-
-
Save vlasovskikh/471685 to your computer and use it in GitHub Desktop.
Abstract data types in plain old C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax:glob | |
main | |
*.o | |
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef int Image; | |
Image *make_image(int foo); | |
char access_pixel(Image *img, int r, int c); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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