3 Replies - 2302 Views - Last Post: 08 March 2011 - 04:27 PM Rate Topic: -----

#1 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

templated struct's in c

Posted 01 January 2011 - 02:24 PM

Description: this uses an dynamic array to show how templated structures cane be made in c. you can use the same method to make more complex templated structures, you can also make templated functions with this method as well by passing the type to the macro.an example witch shows how you can create templated struct's in c using macros.
note: this isn't strictly supported by ISO C90
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define Array(t)  struct{t *p; size_t size;}*

#define InitArray(A)
	(A) = malloc(sizeof(*(A)));
	(A)->p = NULL;
	(A)->size = 0;

#define FreeArray(A)
	free((A)->p);
	free((A));

#define ArrayPush(A,val)
	(A)->p = realloc((A)->p,(++(A)->size)*sizeof(*(A)->p));
	(A)->p[(A)->size-1] = val;

#define ArrayPop(A) (A)->p = realloc((A)->p,(--(A)->size)*sizeof(*(A)->p))

#define ArrayAt(A,index) ((A)->p[index])

#define ArrayBegin(A) ((A)->p)

#define ArrayEnd(A) ((A)->p+(A)->size)

#define ArrayFront(A) (*((A)->p))

#define ArrayBack(A) (*((A)->p+(A)->size-1))

#define ArraySize(A) ((const size_t)((A)->size))

#define ArrayResize(A,S) (A)->p = realloc((A)->p,((A)->size=S)*sizeof(*(A)->p));

#define ArrayResizeVal(A,S,val)
	(A)->p = realloc((A)->p,(S)*sizeof(*A->p));
	for(;(A)->size<S;++(A)->size)
		(A)->p[(A)->size] = val;

#define ArrayEmpty(A) ((A)->size==0)

#define ArrayClear(A)
	free(A->p);
	A->size = 0;

int main() {
	Array(int) MyIntArray;
	InitArray(MyIntArray);
	ArrayResize(MyIntArray,30);
	ArrayAt(MyIntArray,0) = 30;
	printf("%i,%un",ArrayAt(MyIntArray,0),ArraySize(MyIntArray));
	ArrayClear(MyIntArray);
	printf("%un",ArraySize(MyIntArray));
	FreeArray(MyIntArray);

	Array(double) MyDoubleArray;
	InitArray(MyDoubleArray);
	ArrayResizeVal(MyDoubleArray,10,30.343);
	printf("%f,%fn",ArrayBack(MyDoubleArray),ArrayFront(MyDoubleArray));
	FreeArray(MyDoubleArray);

	Array(char*) MyStringArray;
	InitArray(MyStringArray);
	ArrayPush(MyStringArray,"world");
	ArrayPush(MyStringArray,"hello ");
	printf("%s",*(ArrayEnd(MyStringArray)-1));
	ArrayPop(MyStringArray);
	printf("%sn",*ArrayBegin(MyStringArray));
	if(ArrayEmpty(MyStringArray)) {
		puts("how did that happen?");
	} else {
		puts("looks like to works to me");
	}
	FreeArray(MyStringArray);
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: templated struct's in c

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: templated struct's in c

Posted 07 March 2011 - 10:24 PM

I needed PellesC to compile this, none of the other compilers worked.
Was This Post Helpful? 0
  • +
  • -

#3 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: templated struct's in c

Posted 08 March 2011 - 04:26 PM

MinGW didn't work? it compiles fine for me on MinGW. it won't compile on MSVC becuase it requires an explicit cast from a void* to another pointer. i did say "...this isn't strictly supported by ISO C90" so i expected it not to compile on all compilers. i havn't even ever herd of PellesC
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: templated struct's in c

Posted 08 March 2011 - 04:27 PM

also i used mixed declarations here so you might need to declare everything first
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1