473,763 Members | 8,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const parameter problem - clarification needed

Consider the following program:

#include <stdio.h>

void myfn(const int **a)
{
static int i, j, k;

a[0] = &i;
a[1] = &j;
a[2] = &k;

return;
}

int main(void)
{
int *a[3];
int x, y, z;

a[0] = &x;
a[1] = &y;
a[2] = &z;

x = 10;
y = 20;
z = 30;

myfn(a);

return 0;

}

Suppose the program name is tmp.c

When this program is compiled with gcc under Redhat Enerprise Linux,
with the command
gcc -std=c99 tmp.c

the following Compilation warning is produced with gcc
const_ptr.c: In function `main':
const_ptr.c:27: warning: passing arg 1 of `myfn' from incompatible
pointer type

If I remove the const qualifier in myfn( ), the program compiles with
gcc without any warning.

However with VC++ 2005, there is no compilation warning with the
original program ie even when the const qualifier is present.

QUESTION:
-----------------
Why is the warning generated with gcc ?

This question arises because of the following reason.
We can pass a char[] to a function which receives it as const char*.
Here there is no compilation warning reported. But when a char *a[] is
passed, why can't it be received as "const char ** " ?

Feb 19 '07 #1
2 1717
On Feb 19, 7:11 am, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
Consider the following program:

#include <stdio.h>

void myfn(const int **a)
{
static int i, j, k;

a[0] = &i;
a[1] = &j;
a[2] = &k;

return;

}

int main(void)
{
int *a[3];
int x, y, z;

a[0] = &x;
a[1] = &y;
a[2] = &z;

x = 10;
y = 20;
z = 30;

myfn(a);

return 0;

}

Suppose the program name is tmp.c

When this program is compiled with gcc under Redhat Enerprise Linux,
with the command
gcc -std=c99 tmp.c

the following Compilation warning is produced with gcc
const_ptr.c: In function `main':
const_ptr.c:27: warning: passing arg 1 of `myfn' from incompatible
pointer type

If I remove the const qualifier in myfn( ), the program compiles with
gcc without any warning.

However with VC++ 2005, there is no compilation warning with the
original program ie even when the const qualifier is present.

QUESTION:
-----------------
Why is the warning generated with gcc ?

This question arises because of the following reason.
We can pass a char[] to a function which receives it as const char*.
Here there is no compilation warning reported. But when a char *a[] is
passed, why can't it be received as "const char ** " ?
I think the problem is if you are using "const int **", you are
telling the compiler that you are passing a pointer of pointer
pointing to a constant integer value. But in your main, "a" is not
pointing to a const value. This will have problem.

Feb 19 '07 #2
su************* *@yahoo.com, India wrote:
...
void myfn(const int **a)
{
...
}

int main(void)
{
int *a[3];
...
myfn(a);
...
}

...
the following Compilation warning is produced with gcc
const_ptr.c: In function `main':
const_ptr.c:27: warning: passing arg 1 of `myfn' from incompatible
pointer type

If I remove the const qualifier in myfn( ), the program compiles with
gcc without any warning.
...
Argument 'a' in the function call has type 'int*[3]' which in this particular
context decays to type 'int**'. The corresponding parameter has type 'const
int**', which means that in this case you are trying to convert a 'int**' value
to 'const int**' type implicitly. This is not legal in C language. 'int*' can be
implicitly converted to 'const int*', but 'int**' cannot be implicitly converted
to 'const int**'. It the latter conversion were legal, it would let one to
circumvent const-correctness rules without an explicit cast. See the following
FAQ item for more detail

http://www.parashift.com/c++-faq-lit...html#faq-18.17

(This is a C++ FAQ entry, but the principle it illustrates applies immediately
to C as well)

--
Best regards,
Andrey Tarasevich
Feb 21 '07 #3

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

Similar topics

5
5064
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too stupid to understand and make them work. E.g. I have read that boost's smart pointers are able to do this convertion, but the following code doesn't compile (VC++6.0):
6
2423
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk implicitly. The destructor of this class saves the object to the disk if it is dirty. The problem comes in the following scenario when a function returns an uncommitted pointer class because same copies will be committed as two separate objects on disk....
2
2685
by: Betrock | last post by:
This is probably very simple, but I just can't see my way thru it..... Short version: keyed values(numeric)in a lookup table are stored in a main table. They are displayed as text values - the keyed values description. I need a parameter query to return a range of these values. The parameter input won't accept the 'text' version. You can do it with 'Find' because you can select 'as formatted'. But I need a range. Please, any...
18
1613
by: hzmonte | last post by:
typedef int t_compare_func(const void *, const void *); struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { struct node *cur_item; int result; if (root == NULL) return NULL; cur_item = root; while (cur_item != NULL) {
20
5060
by: Brien King | last post by:
If I have a parameter that has an Object type (as opposed to something like a string), can I make that parameter a CONST? Right now, if you pass an object into a sub/function, that sub/function can modify the object no matter how it's defined (ByVal or ByRef). In some cases, I want to make sure that you cannot modify the object. Is there a way to do that in VS2003 or the up comming VS2005?
16
3164
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed inside the function. And it is for this reason that some people advocate that it is a good idea to...
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) {
5
449
by: George2 | last post by:
Hello everyone, This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to correct me. Thanks. 1. A const reference can be binded to a rvalue, for example, a temporary object. And the "life" of the temporary object is guaranteed to be extended and we can safely operate through the const-reference.
3
1553
by: Ben Thomas | last post by:
Hello, I have the following code which I don't understand why it works : #include <iostream> using namespace std; void DontWork (unsigned int& i) { cout << i << endl; }
0
9564
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
9387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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
9823
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
8822
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...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
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
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2794
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.