#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NROWS 256
#define NCOLUMNS 256
typedef struct ARRAY{
unsigned char** a;
size_t nrows;
size_t nco;
}ARRAY;
int creaARRAY (ARRAY** p, size_t nrows, size_t ncolumns);
int rszARRAY (ARRAY** p, size_t nrows, size_t ncolumns);
void freeARRAY (ARRAY* p);
void freeARRAY_d(ARRAY** p);
ARRAY* somma (ARRAY* r, ARRAY* a, ARRAY* b);
void stampa (ARRAY* a);
int main(void)
{size_t i, j, h;
ARRAY my={NULL, 0, 0}, *r=0, *a=0, *b=0, *mmy=&my;
srand((unsigned)time(0));
if(creaARRAY(&mmy, NROWS, NCOLUMNS))
{my.a[255][255] = 'c';
printf("my.array1[255][255] = \'%c\'\n", my.a[255][255]);
}
if(rszARRAY(&mmy, NROWS+100, NCOLUMNS+100))
{my.a[255+100][255+100] = 'c';
printf("my.array1[255+100][255+100] = \'%c\'\n",
my.a[255+100][255+100]);
}
freeARRAY(&my);
for(h=0; h<50; ++h)
{unsigned len=rand()%20+1;
if(creaARRAY(&r, len, len) * creaARRAY(&a, len, len)*
creaARRAY(&b, len, len)!= 0 )
{for(i=0; i<len; ++i)
for(j=0; j<len; ++j)
a->a[j]= rand()%100;
for(i=0; i<len; ++i)
for(j=0; j<len; ++j)
b->a[j]= rand()%100;
if(somma(r, a, b))
{stampa(a); printf("+ \n");
stampa(b); printf("==\n");
stampa(r);
}
}
if(rszARRAY(&r, NROWS+100, NCOLUMNS+100))
{(*r).a[255+100][255+100] = 'c';
printf("my.array1[255+100][255+100] = \'%c\'\n",
(*r).a[255+100][255+100]);
}
if(rszARRAY(&a, 3, 3))
{(*a).a[2][2] = 'c';
printf("my.array1[2][2] = \'%c\'\n", (*a).a[2][2]);
}
if(rszARRAY(&a, NROWS+100, NCOLUMNS+100))
{(*a).a[255+100][255+100] = 'c';
printf("my.array1[255+100][255+100] = \'%c\'\n",
(*a).a[255+100][255+100]);
}
if(rszARRAY(&a, 1, 1))
{(*a).a[0][0] = 'c';
printf("my.array1[0][0] = \'%c\'\n", (*a).a[0][0]);
}
if(rszARRAY(&a, 1, 1))
(*a).a[0][0] = 'c';
//if(rszARRAY(&a, 0, 1));
//if(rszARRAY(&a, 1, 0));
//if(rszARRAY(&a, 0, 0)); non funzionano
if(rszARRAY(&a, 100, 199))
(*a).a[99][99] = 'c';
freeARRAY_d(&r); freeARRAY_d(&a); freeARRAY_d(&b);
}
return 0;
}
void stampa(ARRAY* a)
{size_t i, j;
if(!a) return ;
for( i=0; i< a->nrows; ++i )
{printf("\n");
for( j=0; j< a->nco ; ++j)
printf("%3u ", (unsigned) a->a[j]);
}
printf("\n");
}
ARRAY* somma(ARRAY* r, ARRAY* a, ARRAY* b)
{
size_t i, j;
unsigned char h;
if(r==0||a==0||b==0|| r->a==0||a->a==0||b->a==0)
return 0;
if(r->nrows!=a->nrows||r->nrows!=b->nrows||a->nrows!=b->nrows)
return 0;
if(r->nco!=a->nco||r->nco!=b->nco||a->nco!=b->nco)
return 0;
for( i=0; i< r->nrows; ++i)
for( j=0; j< r->nco; ++j)
{h = a->a[j] + b->a[j];
if( h< a->a[j] || h< b->a[j] )
return 0;
r->a[j]=h;
}
return r;
}
int creaARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{size_t i, j=0;
if(p==0) return 0;
if(*p==0) {if( (*p=malloc(sizeof(**p)))==0 )
return 0;
j=1;
}
if(((*p)->a = malloc(nrows * sizeof( *((*p)->a)))) == NULL)
{if(j) {free( *p ); *p=0;}
else {(*p)->nrows=0; (*p)->nco=0;}
return 0;
}
for( i=0; i<nrows; i++ )
{(*p)->a=malloc( ncolumns * sizeof( *((*p)->a) ) );
if((*p)->a == NULL)
{if(i)
{for( --i; i!=0; --i)
free( (*p)->a );
free( (*p)->a[0] );
}
free( (*p)->a ); /* Libera il massimo che può */
if(j) {free( *p ); *p=0; }
else {(*p)->a=0; (*p)->nrows=0; (*p)->nco=0;}
return 0;
}
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns;
return 1;
}
/* Da usarsi a matrice già creata con creaARRAY (e dimensione>0) */
/* Se riesce a fare il resize ritorna 1 altrimenti **p={0, 0, 0} */
/* *da liberare subito dopo* con l'opportuno freeARRAY */
int rszARRAY(ARRAY** p, size_t nrows, size_t ncolumns)
{size_t i;
unsigned char **temp, *tmp;
if( p==0 || *p==0 )
return 0;
if( nrows < (*p)->nrows )
for( i=nrows; i< (*p)->nrows; ++i )
free( (*p)->a );
if( (temp= realloc( (*p)->a, nrows * sizeof *temp ) )== NULL )
{i = nrows<(*p)->nrows ? nrows: (*p)->nrows ;
goto label;/* It will ok? */
}
(*p)->a=temp;
for( i=0; i<nrows; i++ )
{if( i >= (*p)->nrows )
(*p)->a=0; /* non c'è un puntatore risultato di malloc */
if( ( tmp=realloc((*p)->a, ncolumns * sizeof *tmp) ) == NULL )
{
i= i>=(*p)->nrows ? i: (*p)->nrows;
label:
if(i)
{for(--i; i!=0; --i)
free( (*p)->a );
free( (*p)->a[0] );
}
free( (*p)->a );/* libera il massimo che può */
(*p)->a = 0;
(*p)->nrows = 0;
(*p)->nco = 0;
return 0;
}
else (*p)->a=tmp;
}
(*p)->nrows = nrows ;
(*p)->nco = ncolumns; /* nuove righe e colonne */
return 1;
}
/* matrice parzialmente dinamica */
void freeARRAY(ARRAY* p)
{size_t i;
if( p==0 ) return ;
for(i=0; i < p->nrows; i++)
free( p->a );
free( p->a );
p->a = 0;
p->nco = 0;
p->nrows = 0;
}
/* matrice totalmente dinamica */
void freeARRAY_d(ARRAY** p)
{size_t i;
if( p==0 || (*p)==0 ) return ;
for(i=0; i < (*p)->nrows; i++)
free((*p)->a );
free( (*p)->a );
free(*p);
*p=0;
}