r/C_Programming • u/NameFast691 • 3d ago
i need your advice
Hey guys, I’ve run into a bit of a snag while learning C. I was looking at some source code (shown below) and noticed a function call that passes a pointer. However, the function receiving this pointer declares it as a two-dimensional array rather than as a pointer (like `int *name`). This is where I’m stuck—why declare it as a 2D array instead of as a pointer, the way you would for a 1D array?
#include <stdio.h>
// Function that displays a 2D array
// The number of columns (3) must be known at compile time
void afficherMatrice(int mat[][3], int lignes) {
for (int i = 0; i < lignes; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
// Alternative with both dimensions specified
void initialiserMatrice(int mat[2][3]) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
mat[i][j] = i * 10 + j;
}
}
}
int main() {
int matrice[2][3] = {{1, 2, 3}, {4, 5, 6}};
afficherMatrice(matrice, 2);
initialiserMatrice(matrice);
printf("\nAfter initialization:\n");
afficherMatrice(matrice, 2);
return 0;
}
(I think there is an error in this code)
5
u/SmokeMuch7356 3d ago •
Remember that under most circumstances, an expression of type "array of
T" will "decay" or evaluate to an expression of type "pointer toT" and the value of the expression will be the address of the first element of the array. When you call a function with an array parameter likethat's equivalent to writing
Since
a[0]has typeT,fooreceives a value of typeT *.If we replace
Twith an array typeR [M], we get:which is still equivalent to
This time,
a[0]has typeR [M], sofooreceives a value of typeR (*)[M](pointer to array ofR).In the context of a function parameter declaration,
T a[N]andT a[]are "adjusted" toT *a. Similarly, declarations likeR a[N][M]andR a[][M]are adjusted toR (*)[M].6
u/ReallyEvilRob 3d ago
Help us help you by formatting your code.
2
u/tastygames_official 3d ago edited 3d ago
if you hit the "Aa" button in the bottom of the text box it opens up some controls to allow you to format code:
But technically in C you do not have arrays. You have pointers to the first element in an array, and you have to keep track of the length of the array yourself. The compiler can keep track of the "stride" of the array (how big each element is) by how you define its type, but really all it is is a series of data in memory.
So a type declaration of int[x][y] will simply be a series of x * y integers (probably 32-bit signed integers) in memory. But the variable you declare is really only just a pointer to the first element, and then you use the [x][y] to determine how far away from that starting memory address that particular element is.
So basically if you know the size of your matrix at compile time, then you're better off declaring it as such. I believe also starting at C99 or maybe C11 you can also have variable-length arrays and matrices, so that's nice, too. But doing anything more than 1 dimension starts to get tricky when dealing wth pure pointers. E.g.:
I hope that makes sense.
2
u/[deleted] 3d ago edited 3d ago
Pointers and arrays are functionally the same. Both of them point to the first element in the array. So either way, a pointer variable and an array variable point at the same thing.However, declaring the variable as an array gets you access to C's array syntax (accessing elements with the subscript operator []). This makes the array much easier to interact with in a loop, since otherwise you would have to do the math for accessing individual elements yourself as an offset of the original pointer. Multidimensional arrays make this more complicated.So all this is doing is making the code inside the loop a little easier to read.Edit: This is not quite right so retracting, my bad!
1
u/Educational-Paper-75 3d ago
Can't see any.