473,738 Members | 5,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Warning about defined but not used for global variable

Hello, I have a few global variables in my program. One of them holds the
name of the application and it's defined in a header file globals.hpp (and
the point of definition also happen to be the point of declaration of this
variable, correct?):

static const char * g_application_n ame = "Tiny, class-based MDI Example";

In another source file, I'm including the header globals.hpp and I'm using
the variable g_application_n ame. I do, however, get the following warning
when building my program:
globals.hpp:12: warning: 'g_application_ name' defined but not used

This warning is repeated for every file that includes globals.hpp, either
directly or indirectly.

However, if I change the type of g_application_n ame to const char * const,
the warning disappears.

So I guess that the proper way to define a global string variable that is
not to be written, only read, and should be shared for all source files is
to the define it with the type const char * const?

/ Eric
Jul 23 '05 #1
3 6444
Yes you should use const char * const. const char * means that the
string buffer is writable but the pointer itself is constant. You
should treat string literals as read-only.

Jul 23 '05 #2
al******@hotmai l.com wrote:
Yes you should use const char * const. const char * means that the
string buffer is writable but the pointer itself is constant. You
should treat string literals as read-only.


Hm isn't it the other way around? AFAIK const char* means that the
character data is constant, but the pointer to it isn't, whereas char*
const means that the character data is variable and the pointer constant.
Correct me if I'm wrong.

--
Regards,
Matthias
Jul 23 '05 #3
On 2005-01-26, Eric Lilja <er************ *@yahoo.com> wrote:
Hello, I have a few global variables in my program. One of them holds the
name of the application and it's defined in a header file globals.hpp (and
the point of definition also happen to be the point of declaration of this
variable, correct?):

static const char * g_application_n ame = "Tiny, class-based MDI Example";
Lose the static keyword; with static you get a separate variable named
'g_application_ name' local to each translation unit that includes that
header - not 'global' at all. Change it to extern:

extern const char * g_application_n ame;

And, in one cpp file:

const char * g_application_n ame = "Tiny, class-based MDI Example";
In another source file, I'm including the header globals.hpp and I'm using
the variable g_application_n ame. I do, however, get the following warning
when building my program:
globals.hpp:12: warning: 'g_application_ name' defined but not used

This warning is repeated for every file that includes globals.hpp, either
directly or indirectly.
However, if I change the type of g_application_n ame to const char * const,
the warning disappears.
I'm not sure what the rules are here - but for types like int:

const int name = 0;

in a header is used everywhere possibly in read only memory and doesn't
need to be set up like a global variable that might change. That
apparently applies to 'const char * const' too, at least for your
compiler.
So I guess that the proper way to define a global string variable that is
not to be written, only read, and should be shared for all source files is
to the define it with the type const char * const?


I think I would still do it the proper way, with a an extern declaration
in the header and a single definition in one cpp. At a minimum, if you
change the name of your application, you won't have to recompile all
files that include the header.
Jul 23 '05 #4

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

Similar topics

3
17442
by: William Payne | last post by:
Hello, when compiling my program I get a warning in one of my header files (globals.h) each time a source file includes it. The warning reads: globals.h:28: warning: `const char*g_mdi_child_class_name' defined but not used line 28 of globals.h is: static const char* g_mdi_child_class_name = "MDIChildClass"; Why do I get this warning for this variable? It's used at three places throughout the program. I have two static const int...
7
1988
by: chand | last post by:
Hi., In my api.py file 'g_opt_list' is defined globally g_opt_list =,,,,,,] I am using this global list in the fucntion def function (): gloabl g_opt_list
0
294
by: chand | last post by:
Hi., In my api.py file 'g_opt_list' is defined globally g_opt_list =,,,,,,] when I run the py file, I am getting the Following Error SyntaxWarning: name 'g_opt_list' is used prior to global declaration SyntaxWarning: name 'layers' is used prior to global declaration
2
2160
by: chand | last post by:
Hi., In my api.py file 'g_opt_list' is defined globally g_opt_list =,,,,,,] when I run the py file, I am getting the Following Error SyntaxWarning: name 'g_opt_list' is used prior to global declaration SyntaxWarning: name 'layers' is used prior to global declaration
15
5206
by: Robert | last post by:
Hi, Flexelint says: Warning 528: Symbol 'MY_STATIC_CONST_INT_VAR' (line 426, file headfile.h) not referenced I do understand why, but I'm not sure I wanna change the code to remove the warning. How do I inhibit the warning for just MY_STATIC_CONST_INT_VAR?
4
4118
by: bingfeng | last post by:
I have some codes generated by perl, in which initialize some huge struct,such as PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "", "configuration on a designated vlan", PRO_REQUIRED }; const char* TOS_network_spantree_set_0_para_1_emvalue = { "disable", "enable", NULL }; PARA TOS_network_spantree_set_0_para_1 = { "", emENUM, TOS_network_spantree_set_0_para_1_emvalue, "", "enable or disable STP", PRO_REQUIRED };
3
2726
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released storage Keywords.Keyword reachable from global A global variable does not satisfy its annotations when control is transferred. (Use -globstate to inhibit warning) keywords.c:60:11: Storage Keywords.Keyword released
5
8086
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right thing to do. And I wonder why VB .NET keeps silence and makes such function return some default value instead. Isn't it error-prone? -- Dmitriy Lapshin
8
2010
by: Charles Sullivan | last post by:
I have a program written in C under Linux (gcc) which a user has ported to run under AT&T SysV R4. He sent me a copy of his makelog which displays a large number of compiler warnings similar to this: warning: semantics of ">>" change in ANSI C; use explicit cast The statement to which this applies is: xuc = ((uc & 0xF0 ) >4);
0
8969
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
9335
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
9263
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
8210
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
6751
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
6053
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
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
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
3
2193
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.