473,769 Members | 5,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strange warnings passing int* to const int*

hi,

i'm using "gcc version 3.3.3 (Debian)" to compile the program pasted below.
I get the two warnings you can see in the remarks. The second warning is
perfectly OK for me, but the first one I don't understand.

Isn't the "const int *" construct in the signature of func1 a hint for the
user, that func1 doesn't modify **arg? Why then is it dangerous to
pass an alterable argmument?

thanks for your help!

----------------------------------------------------
void func1(const int *arg[]) { }

void func2(int *arg[]) { }

int main(argc)
{
const int *ary1[10];
int *ary2[10];

func1(ary1);
func1(ary2); // warning: passing arg 1 of `func1' from incompatible pointer type
func2(ary1); // warning: passing arg 1 of `func2' from incompatible pointer type
func2(ary2);
}
----------------------------------------------------
Nov 14 '05 #1
1 2118

On Fri, 9 Jul 2004, george doubleu wrote:

i'm using "gcc version 3.3.3 (Debian)" to compile the program pasted below.
I get the two warnings you can see in the remarks. The second warning is
perfectly OK for me, but the first one I don't understand.

Isn't the "const int *" construct in the signature of func1 a hint for the
user, that func1 doesn't modify **arg? Why then is it dangerous to
pass an alterable argument?
void func1(const int *arg[]) { }

int *ary2[10];

func1(ary2); // warning: passing arg 1 of `func1'
// from incompatible pointer type

'ary2' is an 'array[10] of pointer to int'. It decays in this context
to a 'pointer to pointer to int'. That is, it is the address of a
'pointer to int'.

'func1' expects a parameter of type 'pointer to pointer to const int'.
That is, it expects the address of a 'pointer to const int'.

'pointer to int' and 'pointer to const int' are two different
types, in C. Pointers to different types are not compatible. Thus
'pointer to (pointer to int)' is not compatible with 'pointer to
(pointer to const int)'. Thus GCC gives you a warning.

How to fix it? See another thread in this newsgroup about
improper const-ifying's leading to type errors (Passing const void*
to free, or some such). Also Google for "const poisoning."
The bottom line is that this kind of error can look bizarre, but
it never occurs in well-written, real-life code, so just accept
it and you'll never see it again. :)

-Arthur
Nov 14 '05 #2

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

Similar topics

10
2436
by: Sony Antony | last post by:
I have the following simple program in Solaris Forte compiler 5.4 producing the warning. Though it produces the warning, it works fine as expected. This has been compiling fine without any warnings in the older 5.1 compiler. Since the latest compiler produces a warning, it makes me suspecious about my own code. I still cannot find any problems with it though. It essentially produces a warning whenever a copy constructor of a class with...
2
2173
by: Naren | last post by:
Hello All, I dont get errors but I get warnings though the code runs fine. Could anyone help me removing these warnings c:\program files\microsoft visual studio\vc98\include\vector(48) : warning C4786: '??0?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V ?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2 @@std@@QAE@IABV...
13
1870
by: Neil Zanella | last post by:
Hello, I wonder whether anyone has ever come across the following g++ compiler error message. I don't recall ever seeing it before. I solved my problem but I am still not sure about what this message is all about. Any ideas? error: invalid initialization of non-const reference of
12
4888
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic -Wall -Wunused -Werror -W -Wmissing-prototypes -Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -c -o foo.o foo.c
15
2630
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I'm trying to initialize an array of error messages, so that I can print out an error message by using the 'nth string in an array, e.g. printf("%s\n", messages); I'm still hazy on arrays of pointers to strings, so you may want to finish your drinks before examining my code. Here's a small sample program. #include <stdio.h>
3
1697
by: gaulle | last post by:
i'm a newbie in c. now i 'm learning it.i read a code,and compile it in dev-c++4.9.9.0.it can be compiled and pass.but there are some warnings.while in TC2.0,no any warning.it's why? #include <stdio.h> #include <time.h> #include <stdlib.h> void shuffle(int);
10
1755
by: Bernard Liang | last post by:
Consider the following excerpt below, with the included relevant declarations. Firstly, the lines marked with **, ***, **** at the beginning are not supposed to have those stars at the beginning; they are only used to direct attention. Suppose, first of all, that the lines with the **** are not present. p_num is somehow getting changed from its previous value to 0 by the sscanf command (***) that doesn't even involve p_num. I cannot
28
2144
by: charlie | last post by:
Hi, I found an article on informit.com that talks about C++ casts http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=285&rl=1 The strange thing is the author says the following code will *not* compile: double d=15.95; int n= static_cast<int(d);
11
1748
by: Christopher Key | last post by:
Hello, Can anyone suggest why gcc (-W -Wall) complains, test.c:22: warning: passing arg 1 of `f' from incompatible pointer type when compiling the following code? Change the declation of f to, void f(int *const *const p, int *const *const q) {
0
9587
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
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...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
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
5298
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
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
3561
muto222
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.