[C] multi-dimensional array allocation and using them in functions


by Fb.Researcher
Tags: array, memory allocation, pointer
Fb.Researcher
Fb.Researcher is offline
#1
Nov15-12, 11:08 AM
P: 7
I am a beginer in C programming,so I have just became familiar with pointers.The problem I am writing a program to solve requires me to allocate a multidimensional array, reallocate it and use it in some functions.The simplest problem would be:allocating a two dimensional array, ask a function to print it's components, reallocate it and subtract it's rows by 2 and again give it to the function to print it's components.My code is:
main(){
int **a,i,j;
a=calloc(6,sizeof(int *));
for(i=0;i<6;i++)
a[i]=calloc(6,sizeof(int));
for(i=0;i<6;i++)
for(j=0;j<6;j++)
a[i][j]=i*j;
write(a);
return (0);
}
write is the function that should print the components:
void write(int a[6][6])
{ int i,j;
for(i=0;i<6;i++)
{for(j=0;j<6;j++)
printf("%4i",**a[i][j]);
printf("\n");
}
}
Now here are my questions:
Why the array pointer should be **a and not *a?I have this syntax by searching through the internet but there was no other explanation about it.The only thing I could find in books was that a pointer is defined by a statement like :
int *a;
another problem is using the allocated array in a function.You see compiling the code gives me the following error(Itried to solve it by defining the right kind of argument but I think the failure is due to my lacking of knowledge in pointers.I know about the calling by reference and value)

c:24:2: warning: passing argument 1 of ‘write’ from incompatible pointer type [enabled by default]
note: expected ‘int (*)[6]’ but argument is of type ‘int **’

The program is not compiled.My compiler is gcc.Please help me!
Phys.Org News Partner Science news on Phys.org
NASA's space station Robonaut finally getting legs
Free the seed: OSSI nurtures growing plants without patent barriers
Going nuts? Turkey looks to pistachios to heat new eco-city
rcgldr
rcgldr is offline
#2
Nov15-12, 03:58 PM
HW Helper
P: 6,925
Quote Quote by Fb.Researcher View Post
void write(int a[6][6])
That statement should be:

void write(int **a)

a[6][6] is a 2 dimensional matrix, all integers (no pointers). int **a or int*a[...] is a pointer to pointer to an integer, or an array of pointers to integers, or an array or pointers to arrays of integers (which is what your program is using).

Quote Quote by Fb.Researcher View Post
printf("%4i",**a[i][j]);
That statement should be:

printf("%4i",a[i][j]);

You did it correctly when you used:

a[i][j]=i*j;

Also use the tags [ code ] and [ /code ] (without the spaces) before and after your code to see the indentation of your code. If your code has tabs, they default to 8 spaces at this forum, so you may want to convert tabs to spaces in your source code before posting here.
Fb.Researcher
Fb.Researcher is offline
#3
Nov16-12, 12:32 AM
P: 7
Thank you for your explanation.So if I have truly understood for every dimension of the array I need a loop that allocate an extra dimension and an array of pointers to another array that could be another array of pointers or in two dimensions an array of integers. For example in three dimensions I need ***a.Am I right?
Now I have another question. Is there any other way to allocate an array? For example by using malloc function?
And please tell me how can I add or subract rows by n?I know that I should use realloc.but for a one dimensional array the syntax I have used seemed to be wrong.I used:
 int *ap;
ap=calloc(6,sizeof(int)); 
         //changing the valus of array's members 
ap=realloc(ap,sizeof(int)*2);
         //rest of the code and printing the results
free(ap);
it seems that using wrong syntaxes gives no errors but causes strange results.

rcgldr
rcgldr is offline
#4
Nov16-12, 03:01 AM
HW Helper
P: 6,925

[C] multi-dimensional array allocation and using them in functions


Quote Quote by Fb.Researcher View Post
So if I have truly understood for every dimension of the array I need a loop that allocate an extra dimension and an array of pointers to another array that could be another array of pointers or in two dimensions an array of integers. For example in three dimensions I need ***a.
Using an array of pointers for a matrix isn't required if the number of columns are fixed in size (at compile time). You can use a pointer to a matrix of integers instead. Note, without the parenthesis around *pmi , you'd have a matrix of pointers to integers (and the first dimension would have to be specified).

int i, j;
int (*pmi)[][6];        /* ptr to matrix with 6 columns per row */

    pmi = (void *) malloc(4*6*sizeof(int));

    for(i = 0; i < 4; i++){
        for(j = 0; j < 6; j++){
            (*pmi)[i][j] = i*j;
        }
    }

/* ... */

    free(pmatrix);
You could also use a pointer to an array of integers, then index the pointer to access a marix of integers:

int i, j;
int (*pai)[6];          /* ptr to array of 6 integers or 1st row of matrix */

    pai = malloc(4*6*sizeof(int));
    for(i = 0; i < 4; i++){
        for(j = 0; j < 6; j++){
            pai[i][j] = i*j;}}

/* ... */

    free(pai);
The advantage of using an array of pointers is that the number of columns doesn't have to be fixed at compile time. For a 3d tensor, you would use an array of pointers to arrays of pointers to arrays of integers. If the size of the last two dimensions is known at compile time, then you can use a ptr to an array of matrices or a ptr to the 1st matrix in an array of matrices, similar to the code above.

Quote Quote by Fb.Researcher View Post
realloc
It might be more readable to use malloc and realloc, since the size parameter for both is specified in bytes:

int i;
int *pi;                /* ptr to integer or 1st integer of array */

    pi = malloc(4*sizeof(int));
    for(i = 0; i < 4; i++)
        pi[i] = i;

    pi = realloc(pi, 8 * sizeof(int));
    for(i = 4; i < 8; i++)
        pi[i] = i;

/* ... */

    free(pi);


Register to reply

Related Discussions
C Programming: Dynamic allocation of 2D arrays using an array of pointers. Engineering, Comp Sci, & Technology Homework 2
Automatic array allocation in Fortran 95? Programming & Computer Science 2
What do you mean by 'multi-dimensional'? General Astronomy 8
MATLAB Array Pre-allocation help! Math & Science Software 4
Multi Dimensional QM Quantum Physics 9