I guess I'm expecting arrays to have a natively object-oriented implementation, which is silly in a non-object-oriented language. I have three other related questions which I think merit other threads. But, since I was wrong last time, I'll just ask them here.
When can I assign NULL? I'm trying to solve the problem of the array's length, but my dereferenced pointers don't like the NULL value. I have this problem on lines 29, 30, 31, and 37 of list.h.
Code:
error: incompatible types in assignment
In a linked list, how can the implementation allow all types of values but also let the downstream program know what types it's handling? Not classic generics (although I would prefer it) but downstream type-checking. EDIT: BTW, main.c doesn't reflect this endeavor yet.
Does C support throwing & catching exceptions rather than just printing to stderr?
main.c
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "list.h"
void readLine(char* string, int length, FILE* stream){
int i;
fgets(string, length, stream);
char* last = &string[strlen(string) - 1];
if(*last == '\n'){
*last = 0;
}
return;
}
void concat(char* array[], void (*callback)(char* result)){
int i, sum = 0, length = sizeof(array);
for(i = 0; i < length; i++){
sum += strlen(array[i]);
}
char* result = calloc(sum + 1, sizeof(char));
for(i = 0; i < length; i++){
if(array[i]){
strcat(result, array[i]);
}
}
callback(result);
free(result);
}
void interrupt(char* error){
fputs(error, stderr);
}
int main(int argc, char* argv[]){
if(argc < 2){
char* error = "Error: At least one argument (a filename) must be provided.";
fwrite(error, strlen(error), 1, stderr);
return 1;
}
char mode = 'r';
FILE* file = fopen(argv[1], &mode);
if(!file){
char* array[3];
array[0] = "Error: The file \"";
array[1] = argv[1];
array[2] = "\" could not be opened; check its permissions and try again.";
array[3] = NULL;
concat(array, &interrupt);
return 1;
}
char input[256];
readLine(input, sizeof(input), file);
printf("%s", input);
return 0;
}
list.h
Code:
/*
* File: list.h
* Author: Chris "Jesdisciple"
*
* Created on August 21, 2008, 12:47 PM
*/
#ifndef _LIST_H
#define _LIST_H
#ifdef __cplusplus
extern "C" {
#endif
#include "object.h"
struct node{
struct object* value;
struct node* next;
struct list* list;
};
struct list{
struct node* root;
struct node* current;
struct node* last;
int length;
};
void list(struct list* list){
*(*list).root = NULL;
*(*list).current = NULL;
*(*list).last = NULL;
(*list).length = 0;
}
void link(struct list* list, struct object* value){
struct node node;
*node.value = *value;
*node.next = NULL;
*node.list = *list;
*(*(*list).last).next = node;
*(*list).last = node;
(*list).length++;
}
#ifdef __cplusplus
}
#endif
#endif /* _LIST_H */
object.h
Code:
/*
* File: object.h
* Author: Chris "Jesdisciple"
*
* Created on August 21, 2008, 1:14 PM
*/
#ifndef _OBJECT_H
#define _OBJECT_H
#ifdef __cplusplus
extern "C" {
#endif
struct object{
int pointer;
int length;
int type;
};
#ifdef __cplusplus
}
#endif
#endif /* _OBJECT_H */