My first question has to do with encoutering errors in library routines. Here's a quick simple function for an example.In this code, some special return value is not an option for indicating an error occurred, much less the exact type of error. How is this type of situation handled in C?Code:void * stack_pop(struct stack *s) { if (s->top == -1) { /* stack is empty */ ??? } s->top--; if (s->capacity - s->top - 1 >= 1.5 * __STACK_CAPACITY_INCREMENT) { s->capacity -= __STACK_CAPACITY_INCREMENT; s->stack = realloc(s->stack, s->capacity * sizeof(void *)); if (s->stack == NULL) { /* reallocation failed */ ??? } } return s->stack[1 + s->top]; }
My other question has to do with .h files. Frankly, I simply don't understand how they're linked to the corresponding .c file. I've looked through .h files in other C libraries and I don't see any reference to the .c file. So my assumption is that the compiler knows to automatically look for a .c file of the same name. I tried a small test to see if this was the case:Code:/* hello.h */ void hello(void);Code:/* hello.c */ #include <stdio.h> void hello(void) { printf("Hello, World!"); return; }I'm compiling this with GCC on Win32. But, naturally I get the error undefined reference to 'hello'.Code:/* main.c */ #include "hello.h" int main(void) { hello(); return 0; }
So, quite simply, I don't get it. How do they get linked together?



LinkBack URL
About LinkBacks


