473,382 Members | 1,404 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,382 software developers and data experts.

Warnings for implicit casts to a narrower type

I'd like to have warnings for code like this:

void blah(char c) {}

int main() {
**int*a*=*500;
**blah(a);*//*want*a*warning*here
**char*b*=*a;*//*warning*here*would*be*nice*too
}
Can this be achieved via any trick of the language, or is it
compiler-specific?

Thanks...
Jul 22 '05 #1
3 1278
"Pete" <yo**************@spamsrus.co.uk> wrote in message
news:fo*******************@fe1.news.blueyonder.co. uk...
I'd like to have warnings for code like this:

void blah(char c) {}

int main() {
int a = 500;
blah(a); // want a warning here
char b = a; // warning here would be nice too
}
Can this be achieved via any trick of the language, or is it
compiler-specific?


Check your compiler's documentation. Warnings are usually configurable.
For example, g++ has a -W flag to control which warnings it displays.
Warnings for those implicit conversions are probably turned off because most
programmers find them annoying, distracting, and not helpful.

--
David Hilsee
Jul 22 '05 #2
Pete wrote:
I'd like to have warnings for code like this:

void blah(char c) {}

int main() {
int a = 500;
blah(a); // want a warning here
char b = a; // warning here would be nice too
}
Can this be achieved via any trick of the language, or is it
compiler-specific?


The C languages permit all kinds of abuses, so an independent system called
"lint" was invented to report all sorts of situations of varying
consequence.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3
On Sun, 08 Aug 2004 18:35:55 GMT, Pete
<yo**************@spamsrus.co.uk> wrote in comp.lang.c++:
I'd like to have warnings for code like this:

void blah(char c) {}

int main() {
**int*a*=*500;
**blah(a);*//*want*a*warning*here
**char*b*=*a;*//*warning*here*would*be*nice*too
}
Can this be achieved via any trick of the language, or is it
compiler-specific?
Most compilers will emit such warnings if you set the appropriate
compile time option. Consult your compiler's documentation.
Thanks...


There are also static code analysis tools that will evaluate your
source code and emit such warnings. AFAIK, there are no free ones for
C++. The least expensive would be PC-Lint (http://www.gimpel.com) if
you work on a Windows platform.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #4

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

Similar topics

5
by: Andrew | last post by:
Hello, Is there anyway to detect when scanf implicitly casts a value? If I have a scanf call which expects a float and is passed a double I believe that testing the scanf call with...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
6
by: pete142 | last post by:
When I compile this code: typedef unsigned char BYTE; BYTE * IpString(unsigned int ip) { static BYTE ipString; ipString = (BYTE) 0xff & (ip >24); ipString = (BYTE) 0xff & (ip >16);
82
by: robert bristow-johnson | last post by:
here is a post i put out (using Google Groups) that got dropped by google: i am using gcc as so: $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure...
61
by: jacob navia | last post by:
Continuing the discussion about casts, I would like to know your opinions about the hairy subject of casts as lvalues, i.e. This will fail under lcc-win32, but MSVC and gcc will accept it. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.