C++/basics

Advertisement


Question
sir,
what is difference between
(type)  variable
and  
type   (variable)
in c and c++ both?

Answer
The form:

   type    (variable);

is the same as:

   type    variable;

Parentheses are required in some more complex type declarations to handle order of precedence issues, for example:

   int *  array_var[10]

declares array_var to be an array of 10 pointers to int.

Whereas

   int    (* array_var)[10]

declares array_var to be a pointer to an array of 10 ints.

On the other hand:

   (type) variable

will be parsed as a C-style type cast variable to type, that is it will try to convert the value of variable of the type of variable to an equivalent value of type (creating a temporary object if necessary). The object named variable of course will have to have been declared/defined before hand.

C-style casts will try to make such a conversion happen even when doing so is unsafe and/or not really what the programmer intended - they are sometimes called a sledge hammer cast for this reason (sledgehammer to crack a nut). They are also hard to spot in code. C++ has a set of cast operators that can be used to be more specific about the conversion to be performed and are easily spotted in code. They are purposely a bit cumbersome and ugly as casts are a sign that something a little odd or low level or not-quite-clean is going on. You can look them up in any decent C++ language reference (e.g. "The C++ Programming Language, 3rd edition" by Bjarne Stroustrup, section 6.2.7) or even the ISO C++ standard document (see sections 5.2.7, 5.2.9, 5.2.10 and 5.2.11).

Hope this helps

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.