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

  • Thread starter Thread starter Fb.Researcher
  • Start date Start date
  • Tags Tags
    Array Functions
AI Thread Summary
The discussion focuses on allocating and using multi-dimensional arrays in C, particularly the differences between using pointers and arrays. It highlights that a two-dimensional array should be defined as `int **a` instead of `int *a`, as the former represents an array of pointers to integers. The compiler error arises from passing an incompatible pointer type to the function, which expects a specific array format. The conversation also covers memory allocation techniques, suggesting that using `malloc` or `realloc` can be more effective for dynamic arrays, and emphasizes the need for proper syntax to avoid unexpected behavior. Understanding the structure of pointers is crucial for effectively managing multi-dimensional arrays in C programming.
Fb.Researcher
Messages
9
Reaction score
0
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!
 
Technology news on Phys.org


Fb.Researcher said:
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).

Fb.Researcher said:
printf("%4i",**a[j]);

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:
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:
 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.
 
Fb.Researcher said:
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).

Code:
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:
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.

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

Code:
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:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top