473,663 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about a prototype involving "const"

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
Feb 19 '06 #1
2 1922
In article <dt**********@p c18.math.umbc.e du>
Rouben Rostamian <ro****@pc18.ma th.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.h tml>.
--
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.
Feb 19 '06 #2
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
Feb 19 '06 #3

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

Similar topics

2
16235
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 integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? How about int * const & ? Is this a reference to a constant pointer to an integer? Or
1
2496
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();}
11
4754
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 the introductory book "Absolute C++" by W. Savitch, p. 315) class Money { Money(); const Money operator + (const Money& amount2) const; private:
3
2472
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 wrong? I am using g++ (GCC) 3.3.5 on a Debian Sarge system. The compiler complains: //**************************** //**************************** Compiler output starts *********** cd /home/red/tmp/testprogs/
3
1857
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 AreaSet &CObeyFile::AreaSet () const' r_areaset.h:197: changes meaning of `AreaSet' from `class AreaSet'
9
3619
by: July | last post by:
Hello! consider the following code: class A { public: virtual void f() const{ cout << "A::f()" << endl; } };
4
6075
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 definition errors if the header file is #include'd in more than one code file. The answer was that constants have internal linkage unless declared extern, so it's OK. So, you can put something like...
16
10811
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?
20
3539
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";
0
8858
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8548
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5657
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4182
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.