The main() function in the following code defines an m by n
matrix, assigns value(s) to its elements, then passes the matrix
to function foo().
For whatever it's worth, I have declared foo() so as to make
it treat its first argument as a "read-only" object, that is,
foo() can read but not alter the matrix.
I have a problem, however, with /calling/ foo. If I call
foo as foo(a,m,n), my compiler (gcc) complains about:
try.c:22: warning: passing argument 1 of 'foo' from
incompatible pointer type
I can silence the warning by using a cast:
foo((double const * const *)a, m, n);
I wonder, however, if there is a way to declare foo() to
make the cast unnecessary but at the same time tell it that
its argument is a read-only object.
%---% sample code: try.c %-----------------------------------%
#include <stdlib.h>
void foo(double const * const *a, size_t m, size_t n)
{
a[0] = NULL; /* illegal */
a[0][0] = 4.0; /* illegal */
2.0*a[0][0]; /* legal */
}
int main(void)
{
double **a;
size_t m=3, n=5;
size_t i;
a = malloc(m * sizeof *a);
for (i=0; i<m; i++)
a[i] = malloc(n * sizeof *a[i]);
a[0][0] = 1.0;
foo((double const * const *)a, m, n); /* really want foo(a,m,n); */
return 0;
}
%---% end sample code %--------------------------------------%
--
Rouben Rostamian 2 1877
In article <dt**********@pc18.math.umbc.edu>
Rouben Rostamian <ro****@pc18.math.umbc.edu> wrote: ... I have declared foo() so as to make it treat its first argument as a "read-only" object, that is, foo() can read but not alter the matrix.
I have a problem, however, with /calling/ foo. If I call foo as foo(a,m,n), my compiler (gcc) complains about:
try.c:22: warning: passing argument 1 of 'foo' from incompatible pointer type
"Const" is effectively broken in C. (It would work in C++.)
See <http://c-faq.com/ansi/constmismatch.html>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Rouben Rostamian wrote: The main() function in the following code defines an m by n matrix, assigns value(s) to its elements, then passes the matrix to function foo().
You should be clear about what you mean by a matrix since that is not
one of the types in the C language. In this case you are constructing it
dynamically as pointers to pointers.
For whatever it's worth, I have declared foo() so as to make it treat its first argument as a "read-only" object, that is, foo() can read but not alter the matrix.
I have a problem, however, with /calling/ foo. If I call foo as foo(a,m,n), my compiler (gcc) complains about:
try.c:22: warning: passing argument 1 of 'foo' from incompatible pointer type
This is a serious warning.
I can silence the warning by using a cast:
foo((double const * const *)a, m, n);
This is question 11.10 of the comp.lang.c FAQ http://c-faq.com/ansi/constmismatch.html
I wonder, however, if there is a way to declare foo() to make the cast unnecessary but at the same time tell it that its argument is a read-only object.
Any time you put in a cast to shut the compiler up you are doing the
wrong thing. There are times when a cast is needed, but you should only
use a cast because you know it is needed and understand *why* it is
needed. Once you've read question 11.10 ask about anything in the answer
you don't understand and once you understand the reason for a cast you
can cast. However, you have other problems as well...
%---% sample code: try.c %-----------------------------------% #include <stdlib.h>
void foo(double const * const *a, size_t m, size_t n) { a[0] = NULL; /* illegal */ a[0][0] = 4.0; /* illegal */ 2.0*a[0][0]; /* legal */
Do you want to make:
a = NULL;
illegal as well? If so you would need another const in there...
}
int main(void) { double **a; size_t m=3, n=5; size_t i;
a = malloc(m * sizeof *a);
You need to check to see if malloc succeeded (or if you've stripped out
checks to simplify code for posting, state up front that you have done
this).
for (i=0; i<m; i++) a[i] = malloc(n * sizeof *a[i]);
Again, the check if malloc succeeded.
a[0][0] = 1.0;
foo((double const * const *)a, m, n); /* really want foo(a,m,n); */
This is mentioned near the bottom of question 11.10. The question mostly
talks about assignment, but passing parameters uses the same rules.
return 0; } %---% end sample code %--------------------------------------%
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro - http://clc-wiki.net/wiki/Intro_to_clc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: CoolPint |
last post by:
Can anyone clearly explain the difference between constant reference to
pointers and reference to constant pointers?
What is const int * & ?
Is it a constant reference to a pointer to an...
|
by: bucher |
last post by:
Hi,
I want to push a const auto_ptr into a vector, but the compile reports
errors.
Below is the code.
class Folder;
class Result;
class Results
{
public:
int size(){return _Items.size();}
|
by: Der Andere |
last post by:
What exactly is the sense of using a "const" modifier before a function
declaration? Actually, you can use it in three places. For instance, take a
look at the following function declaration (from...
|
by: H. S. |
last post by:
Hi,
I am trying to compile these set of C++ files and trying out class
inheritence and function pointers. Can anybody shed some light why my
compiler is not compiling them and where I am going...
|
by: Alexander Farber |
last post by:
Hi,
does anyone have an idea, why do I get the following error
(I have to use g++296 on RedHat Linux as compiler):
In file included from r_dir.cpp:9:
r_obey.h:262: declaration of
`const...
|
by: July |
last post by:
Hello!
consider the following code:
class A {
public:
virtual void f() const{
cout << "A::f()" << endl;
}
};
|
by: C. J. Clegg |
last post by:
A month or so ago I read a discussion about putting const ints in
header files, and how one shouldn't put things in header files that
allocate memory, etc. because they will generate multiple...
|
by: Chris |
last post by:
Looking at some code I see a declaration inside a function like
static const string s("some string");
Does the static serve any purpose here?
|
by: liujiaping |
last post by:
I'm confused about the program below:
int
main(int argc, char* argv)
{
char str1 = "abc";
char str2 = "abc";
const char str3 = "abc";
const char str4 = "abc";
const char* str5 = "abc";
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |