Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Nov 15, 2012 #1
    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=calloc(6,sizeof(int));
    for(i=0;i<6;i++)
    for(j=0;j<6;j++)
    a[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[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!
     
  2. jcsd
  3. Nov 15, 2012 #2

    rcgldr

    User Avatar
    Homework Helper

    Re: multi-dimensional array allocation and using them in functions

    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).


    That statement should be:

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

    You did it correctly when you used:

    a[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.
     
    Last edited: Nov 15, 2012
  4. Nov 16, 2012 #3
    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:
    Code (Text):

     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.
     
  5. Nov 16, 2012 #4

    rcgldr

    User Avatar
    Homework Helper

    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).

    Code (Text):

    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:

    Code (Text):

    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.

    It might be more readable to use malloc and realloc, since the size parameter for both is specified in bytes:

    Code (Text):

    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);
     
     
    Last edited: Nov 16, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [C] multi-dimensional array allocation and using them in functions
Loading...