Skip to content

Instantly share code, notes, and snippets.

@lichenbo
Created October 31, 2012 07:29
Show Gist options
  • Save lichenbo/3985610 to your computer and use it in GitHub Desktop.
Save lichenbo/3985610 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#define IDENT 1
#define OPER *
typedef int data_t;
typedef struct {
int len;
data_t *data;
} vec_rec, *vec_ptr;
data_t *get_vec_start(vec_ptr v){
return v->data;
}
int vec_length(vec_ptr v)
{
return v->len;
}
vec_ptr new_vec(int len){
/* allocate header structure */
vec_ptr result = (vec_ptr) malloc(sizeof(vec_rec));
if (!result)
return NULL; /*Couldn't allocate storage*/
result->len = len;
/* Allocate array */
if(len > 0) {
data_t *data = (data_t *)calloc(len, sizeof(data_t));
if(!data)
{
free((void *) result);
return NULL;
}
result -> data = data;
}
else
result -> data = NULL;
return result;
}
void combine4(vec_ptr v, data_t *dest){
int i;
int length = vec_length(v);
data_t *data = get_vec_start(v);
data_t x = IDENT;
*dest = IDENT;
for( i = 0; i < length; i++)
x = x OPER data[i];
*dest = x;
}
int main(){
int a;
vec_ptr v;
v = new_vec(3);
combine4(v,&a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment