473,471 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to pass multi-dimensional array?

Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?

Example:

int matrix_check(int *m)
{
/* how can we use m[i][j] type of representation? */
}

int main()
{
int mat[10][10];

if (matrix_check(&matrix[0][0]))
{
/* do something */
}
...
}

Oct 22 '07 #1
29 38654
Why Tea said:
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?
You have asked a variant of Q6.18 in the comp.lang.c FAQ, which you can
find at http://c-faq.com/index.html

I suggest that you read the appropriate answer, and then follow up here if
you have any further questions.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 22 '07 #2
On 10 22 , 8 39 , Why Tea <ytl...@gmail.comwrote:
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?

Example:

int matrix_check(int *m)
{
/* how can we use m[i][j] type of representation? */

}

int main()
{
int mat[10][10];

if (matrix_check(&matrix[0][0]))
{
/* do something */
}
...

}- -

- -
you do not declare matrix as "int *m",if you want to use m as 2-
dimention array,you should declare it as pointer's pointer--int **m.
And,in function,you can use this pointer's pointer freely,if you want
use this array as m[2][3],you can access m[1][2] by this way: (*m)
+(2*1+2) and so on .
o,BTW,you can not call your function as you do,you can call it
as :matrix_check(matrix)
And,in C,some rule is not rule.What i just said is original rule. You
can also use "(matrix_check(&matrix[0][0]))" and in function,you can
access any variable (m[1][2]) as this : m+2*1+2.
Oct 22 '07 #3
You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}

int main(int argc, char *argv[])
{
int mat[10][10];

if (matrix_check(mat))
{
/* do your work with mat or something else */
}
...
}

Why Tea wrote:
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?

Example:

int matrix_check(int *m)
{
/* how can we use m[i][j] type of representation? */
}

int main()
{
int mat[10][10];

if (matrix_check(&matrix[0][0]))
{
/* do something */
}
...
}
Oct 22 '07 #4
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation
You can't pass an array to a function. Ever. You can only pass a pointer. If
you have array[10][10], its basically the same as array[100]. The easiest
way to do what you want is to pass a pointer to the first element, and a
value indicating the 2nd dimensional size of the array.

int matrix_check(int *val, int width)
{
m[i+(j*width)] = whatever
}

matrix_check(mat,10);

Oct 22 '07 #5
>
you do not declare matrix as "int *m",if you want to use m as 2-
dimention array,you should declare it as pointer's pointer--int **m.
And,in function,you can use this pointer's pointer freely,if you want
use this array as m[2][3],you can access m[1][2] by this way: (*m)
+(2*1+2) and so on .

I think you mean *(m+(2*1)+2))

for example:

#define ROW 3
#define COL 4

#include<stdio.h>

void call_function(int** ptr);

int main(void)
{
int mat[ROW][COL] = { 1, 2, 3, 4,
5, 6, 7, 8,
9, 10,11,12};

call_function((int**)mat);

}

void call_function(int **m)
{
printf("%d",*(m+ ((2*COL)+ 3))); // To access 2*3
}

OUTPUT: 12

Hope it helps

Oct 22 '07 #6
zh************@gmail.com said:
On 10 22 , 8 39 , Why Tea <ytl...@gmail.comwrote:
>Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?

Example:

int matrix_check(int *m)
{
/* how can we use m[i][j] type of representation? */

}

int main()
{
int mat[10][10];

if (matrix_check(&matrix[0][0]))

you do not declare matrix as "int *m",if you want to use m as 2-
dimention array,you should declare it as pointer's pointer--int **m.
No, you shouldn't: int [10][10] is not an int **, and must not be treated
like one.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 22 '07 #7
Prayag Narula said:

<snip>
void call_function(int** ptr);

int main(void)
{
int mat[ROW][COL] = { 1, 2, 3, 4,
5, 6, 7, 8,
9, 10,11,12};

call_function((int**)mat);
Casts are not magic wands. Get the right type for call_function(), and you
won't need to cast. In this case, you want int (*)[COL], not int **.
Hope it helps
Correct answers help far more than incorrect answers. Try to get into the
habit of giving correct answers.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 22 '07 #8
Why Tea:
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element.

If the multi-dimensional array is of known dimensions, I'd go with:

void Func( int (*const p)[7][3] )
{
#define arr (*p)

arr[0][2] = 3;

#undef arr
}

int main(void)
{
int arr[7][3];

Func(&arr);

return 0;
}

Otherwise, go with

#include <stddef.h>

void Func( int (*const p)[3], size_t len )
{
p[len -1][2] = 3;
}

int main(void)
{
int arr[7][3];

Func(arr,7);

return 0;
}
Martin

Oct 22 '07 #9
zh**********@gmail.com wrote:
You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}

int main(int argc, char *argv[])
{
int mat[10][10];

if (matrix_check(mat))
{
/* do your work with mat or something else */
}
...
}
Of course, that only works when both dimensions of the array are fixed.
If the first dimension is variable, then the actual length of that
dimension should be passed to matrix_check. That works because 'm' is
really a pointer type pointing at int[10], and it can point at an
arbitrarily long array of int[10]. If the second dimension is variable,
it gets more complicated.

A more general approach that became feasible in C99 uses the new
variable length array feature to deal with that problem:

int matrix_check(int rows, int cols, int array[rows][cols])
{
// Check 2-dimensional array
return your_return_value;
}

int main(int argc, char *argv[])
{
int mat[10][10];

// fill in contents of mat
if(matrix_check(10, 10, mat))
{
// work with mat
}
// more stuff
}
Oct 22 '07 #10
On Oct 22, 2:07 am, "MisterE" <Mist...@nimga.comwrote:
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation

You can't pass an array to a function. Ever. You can only pass a pointer. If
you have array[10][10], its basically the same as array[100]. The easiest
This is you second post today with incorrect or misleading
information. Please desist.

While array[10][10] may hold the same number of scalar elements as
array[100], they are not basically the same at all.
way to do what you want is to pass a pointer to the first element, and a
value indicating the 2nd dimensional size of the array.
And this is hardly the easiest.
>
int matrix_check(int *val, int width)
{
m[i+(j*width)] = whatever

}

matrix_check(mat,10);
Oct 22 '07 #11
On Oct 21, 9:10 pm, zhangqw.off...@gmail.com wrote:
On 10 22 , 8 39 , Why Tea <ytl...@gmail.comwrote:


Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?
Example:
int matrix_check(int *m)
{
/* how can we use m[i][j] type of representation? */
}
int main()
{
int mat[10][10];
if (matrix_check(&matrix[0][0]))
{
/* do something */
}
...
}- -
- -

you do not declare matrix as "int *m",if you want to use m as 2-
dimention array,you should declare it as pointer's pointer--int **m.
If the argument being passed is in fact defined as a 2D array, then
using an int** is incorrect both syntactically and logically.
And,in function,you can use this pointer's pointer freely,if you want
use this array as m[2][3],you can access m[1][2] by this way: (*m)
+(2*1+2) and so on .
m[i][j] is an int. (*m) is an int*. Adding an integer value to it
does not change its type. The two types are completely different.
o,BTW,you can not call your function as you do,you can call it
as :matrix_check(matrix)
And,in C,some rule is not rule.What i just said is original rule. You
can also use "(matrix_check(&matrix[0][0]))" and in function,you can
access any variable (m[1][2]) as this : m+2*1+2.- Hide quoted text -

- Show quoted text -

Oct 22 '07 #12
<snip>
Casts are not magic wands. Get the right type for call_function(), and you
won't need to cast. In this case, you want int (*)[COL], not int **.
My mistake. gcc summoned a warning and I just type casted and the
warning disappeared, I assumed that it was causing the problem. Should
have been more careful. Note to self: should desist from type casting
as the first move to solve problems.
>
Hope it helps

Correct answers help far more than incorrect answers. Try to get into the
habit of giving correct answers.
Apologies

cheers

Oct 23 '07 #13
Prayag Narula said:
<snip>
>Casts are not magic wands. Get the right type for call_function(), and
you won't need to cast. In this case, you want int (*)[COL], not int **.

My mistake. gcc summoned a warning and I just type casted and the
warning disappeared, I assumed that it was causing the problem. Should
have been more careful. Note to self: should desist from type casting
as the first move to solve problems.
Good plan.
Hope it helps

Correct answers help far more than incorrect answers. Try to get into
the habit of giving correct answers.

Apologies
Well done. score += 10;

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 23 '07 #14
<zh**********@gmail.coma écrit dans le message de news:
11*********************@v23g2000prn.googlegroups.c om...
Why Tea wrote:
>Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?

Example:

int matrix_check(int *m)
{
/* how can we use m[i][j] type of representation? */
}

int main()
{
int mat[10][10];

if (matrix_check(&matrix[0][0]))
{
/* do something */
}
...
}

You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}
Or even simpler to grasp with:

int matrix_check(int m[10][10]) { ... }

The only subtle trap with this notation is that sizeof(m) is not the size of
the matrix.
>
int main(int argc, char *argv[])
{
int mat[10][10];

if (matrix_check(mat))
{
/* do your work with mat or something else */
}
...
}
--
Chqrlie.
Oct 23 '07 #15
"Charlie Gordon" <ne**@chqrlie.orgwrites:
<zh**********@gmail.coma écrit dans le message de news:
11*********************@v23g2000prn.googlegroups.c om...
[...]
>You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}

Or even simpler to grasp with:

int matrix_check(int m[10][10]) { ... }

The only subtle trap with this notation is that sizeof(m) is not the size of
the matrix.
[...]

The only other subtle trap is that the first ``10'' is silently
ignored.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 27 '07 #16
Keith Thompson wrote:
>
"Charlie Gordon" <ne**@chqrlie.orgwrites:
<zh**********@gmail.coma écrit dans le message de news:
11*********************@v23g2000prn.googlegroups.c om...
[...]
You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}
Or even simpler to grasp with:

int matrix_check(int m[10][10]) { ... }

The only subtle trap with this notation
is that sizeof(m) is not the size of the matrix.
[...]

The only other subtle trap is that the first ``10'' is silently
ignored.
int matrix_check(int (*m)[10]) { ... }

I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.

--
pete
Oct 27 '07 #17
Barry Schwarz <sc*******@yahoo.comwrites:
On Oct 22, 2:07 am, "MisterE" <Mist...@nimga.comwrote:
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation

You can't pass an array to a function. Ever. You can only pass a pointer. If
you have array[10][10], its basically the same as array[100]. The easiest

This is you second post today with incorrect or misleading
information. Please desist.

While array[10][10] may hold the same number of scalar elements as
array[100], they are not basically the same at all.
[...]

They're likely (guaranteed, I think) to be represented the same way.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 27 '07 #18
pete <pf*****@mindspring.comwrites:
Keith Thompson wrote:
>"Charlie Gordon" <ne**@chqrlie.orgwrites:
<zh**********@gmail.coma écrit dans le message de news:
11*********************@v23g2000prn.googlegroups.c om...
[...]
>You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}

Or even simpler to grasp with:

int matrix_check(int m[10][10]) { ... }

The only subtle trap with this notation
is that sizeof(m) is not the size of the matrix.
[...]

The only other subtle trap is that the first ``10'' is silently
ignored.

int matrix_check(int (*m)[10]) { ... }

I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.
Exactly. IMHO, allowing array notation for parameters that are really
pointers was a language design mistake.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 27 '07 #19
pete wrote:
....
int matrix_check(int (*m)[10]) { ... }

I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.
If I declared it the way you suggest, the compiler I use most frequently
would insist on reminding me that I should declare 'm' to be const. It
issues no such reminder when I use "int m[][10]", even though that's
functionally equivalent to your declaration. I can't turn off that
reminder without turning off other 'const'-related reminders that I
actually find useful.

It's annoying how stupid things like that can affect your programming style.
Oct 27 '07 #20
"pete" <pf*****@mindspring.coma écrit dans le message de news:
47***********@mindspring.com...
Keith Thompson wrote:
>>
"Charlie Gordon" <ne**@chqrlie.orgwrites:
<zh**********@gmail.coma écrit dans le message de news:
11*********************@v23g2000prn.googlegroups.c om...
[...]
>You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}

Or even simpler to grasp with:

int matrix_check(int m[10][10]) { ... }

The only subtle trap with this notation
is that sizeof(m) is not the size of the matrix.
[...]

The only other subtle trap is that the first ``10'' is silently
ignored.

int matrix_check(int (*m)[10]) { ... }

I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.
The difference between ``int (*m)[10]'' and ``int *m[10]'' is far beyond too
many programmers' understanding of the C syntax. Beginners will find it
much more "understandable" to write ``int m[10][10]''. As long as they do
not attempt to use sizeof, the informative nature of ``10'' will help them.
Too bad this size is completely ignored, to the point where you can "pass"
an array of any length as an argument for this parameter.

What really sucks is the lack of a countof operator that would produce the
number of elements of an array, actual or implied by the ``int m[10][10]''
syntax, while refusing to compile for mere pointer arguments. The
implementation of countof() as a macro in terms of sizeof is still too error
prone.

--
Chqrlie.
Oct 27 '07 #21
Charlie Gordon wrote:
"James Kuyper" <ja*********@verizon.neta écrit dans le message de news:
svyUi.368$TO4.343@trnddc07...
>pete wrote:
...
>>int matrix_check(int (*m)[10]) { ... }

I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.
If I declared it the way you suggest, the compiler I use most frequently
would insist on reminding me that I should declare 'm' to be const. It
issues no such reminder when I use "int m[][10]", even though that's
functionally equivalent to your declaration. I can't turn off that
reminder without turning off other 'const'-related reminders that I
actually find useful.

It's annoying how stupid things like that can affect your programming
style.

Interesting!

Does it suggest const because it determines the array is not modified in the
body of the function?
No, telling me that would be useful, at least for one-dimensional
arrays. This "remark" (it's not labeled as a warning or an error
message, just a remark) is generated because it determines that the
pointer itself is not modified in the body of the function. It is
suggesting that I declare the pointer itself const, not the thing it
points at.
What compiler are you using?
The compiler that comes bundled with SGI's IRIX operating system.
Oct 27 '07 #22
"James Kuyper" <ja*********@verizon.neta écrit dans le message de news:
n6FUi.95$a01.79@trnddc06...
Charlie Gordon wrote:
>"James Kuyper" <ja*********@verizon.neta écrit dans le message de news:
svyUi.368$TO4.343@trnddc07...
>>pete wrote:
...
int matrix_check(int (*m)[10]) { ... }

I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.
If I declared it the way you suggest, the compiler I use most frequently
would insist on reminding me that I should declare 'm' to be const. It
issues no such reminder when I use "int m[][10]", even though that's
functionally equivalent to your declaration. I can't turn off that
reminder without turning off other 'const'-related reminders that I
actually find useful.

It's annoying how stupid things like that can affect your programming
style.

Interesting!

Does it suggest const because it determines the array is not modified in
the body of the function?

No, telling me that would be useful, at least for one-dimensional arrays.
This "remark" (it's not labeled as a warning or an error message, just a
remark) is generated because it determines that the pointer itself is not
modified in the body of the function. It is suggesting that I declare the
pointer itself const, not the thing it points at.
This is so stupid!
Declaring function parameters as const is IMHO useless bloat. It provides
no information to the compiler, and confuses the reader.
Does it complain for all function parameters or just certain types of
pointers?
>What compiler are you using?

The compiler that comes bundled with SGI's IRIX operating system.
I'm glad I stay away from those ;-)

--
Chqrlie.
Oct 27 '07 #23
Charlie Gordon wrote:
"James Kuyper" <ja*********@verizon.neta écrit dans le message de news:
n6FUi.95$a01.79@trnddc06...
....
>This "remark" (it's not labeled as a warning or an error message, just a
remark) is generated because it determines that the pointer itself is not
modified in the body of the function. It is suggesting that I declare the
pointer itself const, not the thing it points at.

This is so stupid!
Declaring function parameters as const is IMHO useless bloat. It provides
no information to the compiler, and confuses the reader.
Does it complain for all function parameters or just certain types of
pointers?
All parameters, as far as I can remember; it's been a while since I
compiled with all warnings turned on. I do that at least once before
each major code delivery; but I've been caught up in a flurry of minor
deliveries for the last year or so, so it's been awhile since I've
compiled in that mode.
Oct 27 '07 #24
Charlie Gordon wrote:
"James Kuyper" <ja*********@verizon.neta écrit dans le message de
news: n6FUi.95$a01.79@trnddc06...
>Charlie Gordon wrote:
>>"James Kuyper" <ja*********@verizon.neta écrit dans le message de
news: svyUi.368$TO4.343@trnddc07...
pete wrote:
...
int matrix_check(int (*m)[10]) { ... }
>
I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.
If I declared it the way you suggest, the compiler I use most
frequently would insist on reminding me that I should declare 'm'
to be const. It issues no such reminder when I use "int m[][10]",
even though that's functionally equivalent to your declaration. I
can't turn off that reminder without turning off other
'const'-related reminders that I actually find useful.

It's annoying how stupid things like that can affect your
programming style.

Interesting!

Does it suggest const because it determines the array is not
modified in the body of the function?

No, telling me that would be useful, at least for one-dimensional
arrays. This "remark" (it's not labeled as a warning or an error
message, just a remark) is generated because it determines that the
pointer itself is not modified in the body of the function. It is
suggesting that I declare the pointer itself const, not the thing it
points at.

This is so stupid!
Declaring function parameters as const is IMHO useless bloat.
Can you explain why?

Oct 27 '07 #25
"James Kuyper" <ja*********@verizon.neta écrit dans le message de news:
jdIUi.3541$eD3.3423@trnddc03...
Charlie Gordon wrote:
>"James Kuyper" <ja*********@verizon.neta écrit dans le message de news:
n6FUi.95$a01.79@trnddc06...
...
>>This "remark" (it's not labeled as a warning or an error message, just a
remark) is generated because it determines that the pointer itself is
not modified in the body of the function. It is suggesting that I
declare the pointer itself const, not the thing it points at.

This is so stupid!
Declaring function parameters as const is IMHO useless bloat. It
provides no information to the compiler, and confuses the reader.
Does it complain for all function parameters or just certain types of
pointers?

All parameters, as far as I can remember; it's been a while since I
compiled with all warnings turned on. I do that at least once before each
major code delivery; but I've been caught up in a flurry of minor
deliveries for the last year or so, so it's been awhile since I've
compiled in that mode.
make ALL_WARNINGS_ON=1 | grep -v 'parameter should be declared const'

--
Chqrlie.
Oct 27 '07 #26
"santosh" <sa*********@gmail.coma écrit dans le message de news:
ff**********@registered.motzarella.org...
Charlie Gordon wrote:
>"James Kuyper" <ja*********@verizon.neta écrit dans le message de
news: n6FUi.95$a01.79@trnddc06...
>>Charlie Gordon wrote:
"James Kuyper" <ja*********@verizon.neta écrit dans le message de
news: svyUi.368$TO4.343@trnddc07...
pete wrote:
...
>int matrix_check(int (*m)[10]) { ... }
>>
>I like the pointer notation for the parameter,
>because that makes it more obvious what the parameter really is.
If I declared it the way you suggest, the compiler I use most
frequently would insist on reminding me that I should declare 'm'
to be const. It issues no such reminder when I use "int m[][10]",
even though that's functionally equivalent to your declaration. I
can't turn off that reminder without turning off other
'const'-related reminders that I actually find useful.
>
It's annoying how stupid things like that can affect your
programming style.

Interesting!

Does it suggest const because it determines the array is not
modified in the body of the function?

No, telling me that would be useful, at least for one-dimensional
arrays. This "remark" (it's not labeled as a warning or an error
message, just a remark) is generated because it determines that the
pointer itself is not modified in the body of the function. It is
suggesting that I declare the pointer itself const, not the thing it
points at.

This is so stupid!
Declaring function parameters as const is IMHO useless bloat.

Can you explain why?
Whether a function parameter is changed during execution of the function
body is usually not important, it depends on what the function does, and how
it is implemented. Sometimes it is appropriate to push a pointer argument,
sometimes it is better to use a local variable. Reading the function will
tell you how the arguments are used, the information the const qualifier
gives you is a minuscule hint, much less useful than say a proper name for
the argument. Sprinkling const qualifiers everywhere actually causes more
obfuscation than good, because of the extra syntax clutter.

Conversely, it is very helpful to const qualify pointer targets: this
conveys useful information, namely the object is not modified by the
function. That is useful for all users of the function. Whether the
function modifies its parameter in its implementation is a useless detail.

--
Chqrlie.
Oct 27 '07 #27
"Charlie Gordon" <ne**@chqrlie.orgwrites:
"pete" <pf*****@mindspring.coma écrit dans le message de news:
47***********@mindspring.com...
>Keith Thompson wrote:
>>"Charlie Gordon" <ne**@chqrlie.orgwrites:
[...]
>Or even simpler to grasp with:

int matrix_check(int m[10][10]) { ... }

The only subtle trap with this notation
is that sizeof(m) is not the size of the matrix.
[...]

The only other subtle trap is that the first ``10'' is silently
ignored.

int matrix_check(int (*m)[10]) { ... }

I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.

The difference between ``int (*m)[10]'' and ``int *m[10]'' is far beyond too
many programmers' understanding of the C syntax. Beginners will find it
much more "understandable" to write ``int m[10][10]''. As long as they do
not attempt to use sizeof, the informative nature of ``10'' will help them.
Too bad this size is completely ignored, to the point where you can "pass"
an array of any length as an argument for this parameter.
Then write ``int m[][10]''. It means exactly the same thing to the
compiler as ``int m[][10]'' (in a parameter declaration), and it
accurately expresses what's really going on.

[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 27 '07 #28
"Keith Thompson" <ks***@mib.orga écrit dans le message de news:
ln************@nuthaus.mib.org...
"Charlie Gordon" <ne**@chqrlie.orgwrites:
>"pete" <pf*****@mindspring.coma écrit dans le message de news:
47***********@mindspring.com...
>>Keith Thompson wrote:
"Charlie Gordon" <ne**@chqrlie.orgwrites:
[...]
>>Or even simpler to grasp with:

int matrix_check(int m[10][10]) { ... }

The only subtle trap with this notation
is that sizeof(m) is not the size of the matrix.
[...]

The only other subtle trap is that the first ``10'' is silently
ignored.

int matrix_check(int (*m)[10]) { ... }

I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.

The difference between ``int (*m)[10]'' and ``int *m[10]'' is far beyond
too
many programmers' understanding of the C syntax. Beginners will find it
much more "understandable" to write ``int m[10][10]''. As long as they
do
not attempt to use sizeof, the informative nature of ``10'' will help
them.
Too bad this size is completely ignored, to the point where you can
"pass"
an array of any length as an argument for this parameter.

Then write ``int m[][10]''. It means exactly the same thing to the
compiler as ``int m[][10]'' (in a parameter declaration), and it
accurately expresses what's really going on.
Sometimes, we really would want the size to be part of the type and make it
obvious with the D syntax:

int matrix_check(int[10][10] m) { ... }

--
Chqrlie.
Oct 27 '07 #29
"Charlie Gordon" <ne**@chqrlie.orgwrites:
"Keith Thompson" <ks***@mib.orga écrit dans le message de news:
ln************@nuthaus.mib.org...
>"Charlie Gordon" <ne**@chqrlie.orgwrites:
[...]
>>The difference between ``int (*m)[10]'' and ``int *m[10]'' is far
beyond too many programmers' understanding of the C syntax.
Beginners will find it much more "understandable" to write ``int
m[10][10]''. As long as they do not attempt to use sizeof, the
informative nature of ``10'' will help them. Too bad this size is
completely ignored, to the point where you can "pass" an array of
any length as an argument for this parameter.

Then write ``int m[][10]''. It means exactly the same thing to the
compiler as ``int m[][10]'' (in a parameter declaration), and it
accurately expresses what's really going on.

Sometimes, we really would want the size to be part of the type and make it
obvious with the D syntax:

int matrix_check(int[10][10] m) { ... }
Sure, sometimes we want it, but we can't have it in C.

If you really want to pass fixed-size two-dimensional arrays to a
function, consider wrapping them in a structure:

struct matrix_10_10 {
int m[10][10];
};

void func(struct matrix_10_10 *m);

Note that we have to pass it via a pointer explicitly (unless we don't
mind copying the entire matrix on each call), since there's no
implicit struct-to-pointer conversion.

Or you could point to the matrix itself:

void func(int (*m)[10][10]);

In this case, both ``10''s are significant, but the usual
array/pointer confusion may make this unacceptable. A novice C
programmer using this interface is likely to get compiler error
messages, and will be tempted to "fix" them by adding casts. Pointing
to a struct rather than to an array avoids this.

However, I think the need for fixed-size matrices is fairly rare. If
you need to handle 10x10 matrices, you probably need to handle NxN or
MxN matrices. Such code should not be attempted by anyone who hasn't
read section 6 of the comp.lang.c FAQ.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 27 '07 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stephen Thorne | last post by:
Decorators have been getting lots of air-time at the moment, but only really the syntax. After a short discussion on irc the other night I decided to download python2.4 from experimental and write...
1
by: Berend | last post by:
I am trying to pass multi values into a where clause with an in clause in a store procedure to use in a Crystal report. This can change depending on the user. Maybe there is another way to pass...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name...
4
by: andrewcw | last post by:
With a Multi-USe COM DLL object another applic. can get reference to it. - And if that DLL's counter continue to have a least one reference, then another application or reference to the DLL can...
5
by: Alexandre Badez | last post by:
Hye, I'm developing a little app, and I want to make multi heritage. My problem is that my both parent do have __slots__ define. So I've got something like: class foo(object): __slots__ = ...
6
by: python | last post by:
I need to generate multi-level incrementing labels for an outline/hierarchy where the string for each level of a label is based on an incrementing sequence like 1, 2, 3 or A, B, C, or even I, II,...
0
by: Mark C. Stock | last post by:
"Mark C. Stock" <mcstockX@Xenquery .comwrote in message news:... | | "Berend" <Berend.Brinkhuis@evatone.comwrote in message | news:bdd9ac20.0401271301.22cdb65e@posting.google.com... | | I am...
1
Kosal
by: Kosal | last post by:
Dear Sir/Madam I would like you to help give me the an example pass multi parameter to crystal report. thanks Best Regard Kosal
3
by: lee weaver | last post by:
I have an odd situtation where a section of code is resetting the status of either multi line If statments which then gives me the error " end if without block if" or something very similure, or ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.