This doesn't address all your questions, but here's a quick take on it. I added BoyFerrell's posted program, and an example of realloc on a 1D array (all I have on realloc()).
/*
This example makes a global array, of undecided size, being declared
at run time, and subsequently malloc'd. You should add the check for
success of the malloc call, as you see in the second program example.
Code:
*/
#include <stdlib.h>
#include <stdio.h>
int **A;
int main() {
int i, j, k, size;
void printIt(int size);
printf("\n\n Enter the size for this square array: ");
scanf("%d", &size);
i = getchar();
A = malloc(size * sizeof(int*)); //allocation of memory for the pointers;
for(i = 0; i < size; i++) //and now for the int's in each col
A[i] = malloc(size * sizeof(A[0]));
printf("\n\n");
for(i = 0, k = 0; i < size; i++)
for(j = 0; j < size; j++) {
A[i][j] = 10 + k;
k++;
printf("%d ", A[i][j]);
}
printIt(size);
//free the memory
for(i = 0; i < size; i++)
free(A[i]);
free(A);
printf("\n\n\t\t\t program complete, press enter when ready");
i = getchar();
return 0;
}
void printIt(int size) {
int i, j;
printf("\n\n");
for(i = 0; i < size; i++) {
for(j = 0; j < size; j++)
printf("%d ", A[i][j]);
printf("\n");
}
}
/* An example of a 2D malloc array (not global) (Vart's linked post
of boyferrel's post)
#include <stdio.h>
#include <stdlib.h>
void read_data(unsigned int rows, const unsigned int cols, double **data)
{
;
}
int main(void)
{
double **data; /* 2d array to hold input file values */
const unsigned long ncols=29; /* number of columns to read */
unsigned long nrows=0; /* number of rows in input file */
unsigned i;
/* allocate data array */
data = malloc(nrows*sizeof(double*));
if (data==NULL)
{
printf("Could not allocate data array of size %lu\n",nrows*sizeof(*data));
return 1;
}
for (i=0; i<nrows; i++)
{
data[i]=malloc(ncols*sizeof(*data[i]));
if (data[i]==NULL)
{
printf("Could not allocate array data[%u] of size %lu\n",i, ncols*sizeof(*data[i]));
return 1;
}
}
/* read data into data array */
read_data(nrows, ncols, data);
return 0;
}
*/
/* This is an example of using realloc(): only example I have.
#include <stdio.h>
#include <alloc.h>
#include <string.h>
int main(void)
{
char *str;
/* allocate memory for string */
str = malloc(10);
/* copy "Hello" into string */
strcpy(str, "Hello");
printf("String is %s\n Address is %p\n", str, str);
str = realloc(str, 20);
printf("String is %s\n New address is %p\n", str, str);
/* free memory */
free(str);
return 0;
}
*/