473,662 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const and compiler warning

Can anyone explain to me why the code below generates a warning?
/////////////////////////////////////////////////////////////
typedef struct testStruct * TestStructRef;
typedef TestStructRef * TestStructRefPt r;
void func(const TestStructRefPt r data);

/* code that uses above */
const TestStructRef junk;
func(&junk); /* compiler warning about different 'const' qualifier,
here &junk is a pointer to a pointer */
//////////////////////////////////////////////////////////////

If I change it to use let say a 'char' instead of a struct I don't get
a compiler warning.

//////////////////////////////////////////////////////////////
void func2(const char ** data);

/* code that uses above */
const char * junk;
func2(&junk); /* no compiler warning, here &junk is a pointer to a
pointer */
///////////////////////////////////////////////////////////////

I'm seeing the warning with Visual Studio 2005. Sorry in advance if
it's something obvious but I'm at a loss to explain it. Any help
would be greatly appreciated.
Jun 27 '08 #1
9 1772
Hi

On Jun 12, 3:00 am, bacbacdin...@gm ail.com wrote:
Can anyone explain to me why the code below generates a warning?
/////////////////////////////////////////////////////////////
typedef struct testStruct * TestStructRef;
typedef TestStructRef * TestStructRefPt r;
void func(const TestStructRefPt r data);

/* code that uses above */
const TestStructRef junk;
func(&junk); /* compiler warning about different 'const' qualifier,
here &junk is a pointer to a pointer */
//////////////////////////////////////////////////////////////

If I change it to use let say a 'char' instead of a struct I don't get
a compiler warning.

//////////////////////////////////////////////////////////////
void func2(const char ** data);

/* code that uses above */
const char * junk;
func2(&junk); /* no compiler warning, here &junk is a pointer to a
pointer */
///////////////////////////////////////////////////////////////
I suggest:

1: don't use CaMelCaps. It makes everything
ReallyReallyDif fiCultToRead.

2: don't typedef pointer types. typedef your struct, and then use *,
which is universally understood.

If you do this you may see the mistake that you have made. Hint:
here &junk is a pointer to a pointer */
computer says no.

Also,
const TestStructRefPt r foo;
means
TestStructRef * const foo;

which is almost certainly not what you mean. Again, you would not
have made this mistake if you followed 2 above.

HTH

viza

Jun 27 '08 #2
On Jun 12, 4:32 am, viza <tom.v...@gmail .comwrote:
Hi

On Jun 12, 3:00 am, bacbacdin...@gm ail.com wrote:
Can anyone explain to me why the code below generates a warning?
/////////////////////////////////////////////////////////////
typedef struct testStruct * TestStructRef;
typedef TestStructRef * TestStructRefPt r;
void func(const TestStructRefPt r data);
/* code that uses above */
const TestStructRef junk;
func(&junk); /* compiler warning about different 'const' qualifier,
here &junk is a pointer to a pointer */
//////////////////////////////////////////////////////////////
If I change it to use let say a 'char' instead of a struct I don't get
a compiler warning.
//////////////////////////////////////////////////////////////
void func2(const char ** data);
/* code that uses above */
const char * junk;
func2(&junk); /* no compiler warning, here &junk is a pointer to a
pointer */
///////////////////////////////////////////////////////////////

I suggest:

1: don't use CaMelCaps. It makes everything
ReallyReallyDif fiCultToRead.

2: don't typedef pointer types. typedef your struct, and then use *,
which is universally understood.

If you do this you may see the mistake that you have made. Hint:
here &junk is a pointer to a pointer */

computer says no.

Also,
const TestStructRefPt r foo;
means
TestStructRef * const foo;

which is almost certainly not what you mean. Again, you would not
have made this mistake if you followed 2 above.

HTH

viza
Thanks for the reply. I have no choice as far as using CamelCase
since that's the standard where I work. What I'm trying to do is call
a function that returns a pointer to a constant structure. I changed
the example using the advice you gave to:

struct testStruct;
typedef struct testStruct TestStruct;

void func(const TestStruct ** data);

const TestStruct * pData = 0;
func(&pData);
Jun 27 '08 #3
ba**********@gm ail.com wrote:
On Jun 12, 4:32 am, viza <tom.v...@gmail .comwrote:
//////
>I suggest:

1: don't use CaMelCaps. It makes everything
ReallyReallyDi ffiCultToRead.

2: don't typedef pointer types. typedef your struct, and then use *,
which is universally understood.

If you do this you may see the mistake that you have made. Hint:
>
Thanks for the reply. I have no choice as far as using CamelCase
since that's the standard where I work. What I'm trying to do is call
a function that returns a pointer to a constant structure.
Camel case is just a common choice of style.

The use of typedefs for pointer types is just bad style!

--
Ian Collins.
Jun 27 '08 #4
Ian Collins wrote:

[ ... ]
The use of typedefs for pointer types is just bad style!
One might make an exception to that when constructing opaque data types.
This is a situation where you want to hide the pointer nature and
present it as a handle.

Jun 27 '08 #5
On Jun 12, 3:00*am, bacbacdin...@gm ail.com wrote:
Can anyone explain to me why the code below generates a warning?
/////////////////////////////////////////////////////////////
typedef struct testStruct * * TestStructRef;
typedef TestStructRef * *TestStructRefP tr;
void func(const TestStructRefPt r data);
Function func has one parameter of type "TestStructRefP tr". The fact
that you wrote "const" in front of it is completely pointless and
doesn't mean anything, because qualifiers are ignored in a function
declaration. If you used the same "const" in a function definition,
then it would mean that the variable "data" is const and cannot be
modified; for example, you wouldn't be allowed to write "data =
NULL;".

You seem to believe that "const TestStructRefPt r" is the same as
"const TestStructRef *". It isn't. The former is a pointer to
TestStructRef, and the pointer itself cannot be modified. The latter
is a pointer to TestStructRef, and the TestStructRef pointed to cannot
be modified.

Jun 27 '08 #6
"christian. bau" wrote:
bacbacdin...@gm ail.com wrote:
>Can anyone explain to me why the code below generates a warning?
/////////////////////////////////////////////////////////////
typedef struct testStruct * TestStructRef;
typedef TestStructRef * TestStructRefPt r;
void func(const TestStructRefPt r data);

Function func has one parameter of type "TestStructRefP tr". The
fact that you wrote "const" in front of it is completely ...
Besides, he is probably not using a C99 compiler, so that line of
division symbols is an automatic syntax error. :-)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #7
On Jun 12, 12:49 pm, "christian. bau"
<christian....@ cbau.wanadoo.co .ukwrote:
On Jun 12, 3:00 am, bacbacdin...@gm ail.com wrote:
Can anyone explain to me why the code below generates a warning?
/////////////////////////////////////////////////////////////
typedef struct testStruct * TestStructRef;
typedef TestStructRef * TestStructRefPt r;
void func(const TestStructRefPt r data);

Function func has one parameter of type "TestStructRefP tr". The fact
that you wrote "const" in front of it is completely pointless and
doesn't mean anything, because qualifiers are ignored in a function
declaration. If you used the same "const" in a function definition,
then it would mean that the variable "data" is const and cannot be
modified; for example, you wouldn't be allowed to write "data =
NULL;".

You seem to believe that "const TestStructRefPt r" is the same as
"const TestStructRef *". It isn't. The former is a pointer to
TestStructRef, and the pointer itself cannot be modified. The latter
is a pointer to TestStructRef, and the TestStructRef pointed to cannot
be modified.
The behavior I wanted was the latter. Thanks for the help, very much
appreciated.
Jun 27 '08 #8
On Jun 12, 12:23 pm, santosh <santosh....@gm ail.comwrote:
Ian Collins wrote:

[ ... ]
The use of typedefs for pointer types is just bad style!

One might make an exception to that when constructing opaque data types.
This is a situation where you want to hide the pointer nature and
present it as a handle.
The intent is to construct an opaque data type but it looks like using
a typedef to a pointer isn't the way to go.
Jun 27 '08 #9
ba**********@gm ail.com writes:
On Jun 12, 12:23 pm, santosh <santosh....@gm ail.comwrote:
>Ian Collins wrote:

[ ... ]
The use of typedefs for pointer types is just bad style!

One might make an exception to that when constructing opaque data types.
This is a situation where you want to hide the pointer nature and
present it as a handle.

The intent is to construct an opaque data type but it looks like using
a typedef to a pointer isn't the way to go.
Then you may be giving up too early. If you need to point to a
constant and typedef the whole lot (because you do want it to be just
an opaque handle) you need to put the const into the typedef. You
can't add constness to the to pointed-to type once the typedef is
defined (as you discovered). Thus:

typedef const struct my_hidden_struc t *Handle;

Allows one to declare, for example,

void function(Handle h);

and the struct pointed to by h is const. Of course, since the struct
is opaque, your users can't get at the members pointed to by one of
these handles, so all you gain by making it const is to prevent
accidental copying like: '*h = *other_handle;' .

--
Ben.
Jun 27 '08 #10

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

Similar topics

4
6381
by: thule | last post by:
Okay, thanks to John (THANK YOU SO MUCH). I switched all my header files over to using the new Standard <iostream> and included the using namespace std; . This seems to have fixed all the errors I was getting because of mixing the old and new headers. The problem I am running into now is not an error but 10 warnings when compiling the same class header implimentation file that I was working with when I was having the problems with the map...
5
2497
by: Adrian | last post by:
Hi all, I am getting a warning compiling the following code using Borland C++ Builder 6, but I dont think I am doing anything wrong. When using g++ I get no warnings at all with a g++ -Wall -ansi -pedantic. Now I usually assume the compiler is correct but in this case I am not sure. push 1 gives the warning the other 2 go through fine.
1
2064
by: Marco Singer | last post by:
Hi all, how can I disable a special compiler warning (for example CS0162) within a region of code? In C++ there is the "#pragma warning" compiler directive. But I did not found any matching in C#. Thanks Marco
5
6433
by: Robert A Riedel | last post by:
I have a class that is intended to be exported in a DLL that uses another class that is in a static library. All clients that use the DLL will also link with the same static library. In summary, the following condition exists: class A {} ; // Instantiated in the static library and specified // in a header included by all clients. // // This is the class using A that is exported by a DLL.
5
3868
by: Alan Cobb | last post by:
Hi, In the managed C++ class below I get compile warning C4677 from VS2003. "signature of non-private function contains assembly private type", even though the managed enum is public. I have to drop the "enum" keyword from my member variable declaration to keep the compiler happy. Shouldn't I technically be able to use the "enum" keyword without a warning?
1
5791
by: John Harris | last post by:
We have some C++ code that has a makefile which contains both /W2 and /W3. Due to the way the makefiles are written to be shared across multiple projects, it's not trival to eliminate the duplicate compiler warning-level directives. I'm trying to find a way to silence the following warning displayed by Visual Studio 2005 for each file compiled: cl : Command line warning D9025 : overriding '/W2' with '/W3' One would think you could add...
6
11509
by: chrisb | last post by:
Hi, Does anyone know if it's possible to generate a custom compiler warning in vs2005? Something like the //todo: comments (possibly //warning: ), but it would show up in the warnings on every build with my custom message? It would be useful to remind me to do something later that depends upon code that has yet to be written. Thanks
11
2014
by: zeppe | last post by:
Hi all, I've a problem. The code that follows creates a warning in both gcc and visual c++. However, I think it's correct: basically, there is a function that return an object of a derived class, that's bounded to a base class reference to delay the destruction of the actual object to the end of the reference scope. Actually, I don't use the reference: the code that matters is in the destructor, and I want it to be executed at the end...
29
1921
by: Bob | last post by:
Hi, I have been trying to use some inventive alternative idioms for infinite loops in my code, rather than the same old for(;;) and while(1) - this could be a nice amusing Easter-egg for any future maintenance programmers! One of my ideas was this: unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
0
8432
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
8857
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
8764
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...
1
8546
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
7367
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
6186
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
4180
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
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
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

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.