codice:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int m, n;
char *v;
} charv;
int new_charv(charv *v, int n, int m) {
v->m = m;
v->n = n;
v->v = (char *)malloc(sizeof(char)*n*m);
return (v->v == NULL) ? 0 : 1;
}
int expand_charv(charv *v, int m) {
char *tmp = (char *)realloc(v->v, sizeof(char)*v->n*m);
if(tmp != NULL) {
v->m = m;
v->v = tmp;
return 1;
}
return 0;
}
int insert_charv(charv *v, int m, int n, char c) {
if(m >= v->m) {
if(!expand_charv(v, m)) return 0;
}
v->v[m*(v->n)+n] = c;
}
char get_charv(charv *v, int m, int n) {
return v->v[m*(v->n)+n];
}
void free_charv(charv *v) {
free(v->v);
v->v = NULL;
}
int main()
{
charv v;
new_charv(&v, 2, 2);
insert_charv(&v, 0, 0, 'a');
insert_charv(&v, 2, 1, 'c');
printf("%c%c", get_charv(&v, 0, 0), get_charv(&v, 2, 1));
free_charv(&v);
return 0;
}
Questo è un esempio, ovviamente non ti è necessario usare quelle funzioni di accesso se sei sicuro di rispettare i limiti...
Puoi ben notare che è modificabile solo il numero di righe e non quello di colonne, nel caso ti servisse modificare anche quello devi usare una struttura diversa: un array allocato dinamicamente di puntatori ad array allocati dinamicamente...
Oppure questo stesso metodo usato sopra e un po' di memcpy al posto giusto...