Dynamic array with macro

zhaozj2021-02-16  76

Tip: The following code has not been tested in detail. If it is free, put out some opinions, be grateful, and the topic is implemented with macro, so don't say more.

#ifndef _vector_h_ # define _Vector_h_

#include

Typedef struct _Vector {int size; void * data;} vector, * pvector;

#define vector_init (x, t, z) DO {/ pVector v; int N; / v = (pVector) (x); n = (int); / v-> data = malloc (v-> size = SIZEOF (T) * N); /} While (0) #define vector_cleanup (x) Free ((x) -> data) #define vector_get (v, t, n) (((t *) ((V) -> data)) [N])

#define vector_set_fast (v, t, n, d) (((((t *) ((V) -> DATA)) [N] = (d))

#define vector_set (x, t, z, d) DO {/ pVector v; int N; t temp; / v = (pVector) (x); n = (int) (z); TEMP = (d) / if (N * SIZEOF (T)> V-> size) / {/ void * dt; dt = realloc (V-> size = n * sizeof (t)); / if (! dt) Break; / v-> data = DT; /} / Memcpy ((((t *) V-> Data) N, & TEMP, SIZEOF (T)); /} While (0)

#ENDIF

转载请注明原文地址:https://www.9cbs.com/read-14376.html

New Post(0)