473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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_name = "Tiny, class-based MDI Example";

In another source file, I'm including the header globals.hpp and I'm using
the variable g_application_name. 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_name 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 6382
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******@hotmail.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_name = "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_name;

And, in one cpp file:

const char * g_application_name = "Tiny, class-based MDI Example";
In another source file, I'm including the header globals.hpp and I'm using
the variable g_application_name. 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_name 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
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...
7
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
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...
2
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...
15
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...
4
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",...
3
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...
5
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...
8
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.