944,199 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2252
  • C++ RSS
Dec 10th, 2005
0

complex declarations & other doubts

Expand Post »
1) Plz explain these complex declarations(from K&R).--
C++ Syntax (Toggle Plain Text)
  1. char(* (*f())[]) () // f : a function returning pointer to array[] of pointer to function returning char.
  2. char(* (*x[3])()) [5] // x: array[3] of pointer to function returning pointer to array[3] of char.
The one line explanations which r given as comment were given in K&R but i couldnt understand them.
Do u know some links to tutorial of these kind of complex declarations?


2) Is there any special reason of- Why most C compilers include most of the header files implicitly, but in C++ we have to include them explicitly?

3) Can a global variable in C be declared anywhere in the program(outside all functions)?
ex-
int i;
main()
{
/*body*/
}
int j;
test()
{
/*body*/
}
int k;

Is this code valid according to C standards?


4) Is the value of NULL=0 according to C/C++ standard?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akshayabc is offline Offline
15 posts
since Jun 2005
Dec 10th, 2005
0

Re: complex declarations & other doubts

Quote originally posted by akshayabc ...
2) Is there any special reason of- Why most C compilers include most of the header files implicitly, but in C++ we have to include them explicitly?
I don't think that is the case. You have to include header files in C also. Many (most) system headers include other headers.

Quote originally posted by akshayabc ...
3) Can a global variable in C be declared anywhere in the program(outside all functions)?
ex-
int i;
main()
{
/*body*/
}
int j;
test()
{
/*body*/
}
int k;

Is this code valid according to C standards?
yes, but remember that the variable has to have been declared before it can be used. That means function main() cannot use variable k.


Quote originally posted by akshayabc ...
4) Is the value of NULL=0 according to C/C++ standard?
c++ yes, C no. In C it can be (void *)0, and I've even seen several other variations on that depending on the compiler.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Dec 10th, 2005
0

Re: complex declarations & other doubts

1)
Just work your self out from the middle, if they are given in k&r they describe them in k&r ?
char(* (*f())[]) ()
*f() => f is function pointer taking zero arguments
returning (* ... [])() => returning array of pointer to functions
returning char ... => returning char...
(Ok, this was a pain in the ass, but I doubt youll ever see anything like that in real life)

2)
C does not include default headers, instead C has implicit function declarations...
if the compiler sees a function call it has not seen before it assumes it is declared as a function int function_name( function_args ) (ie it returns an int) leaving the linker to fail if there really is no such function. I think you can get a warning for this with -Wall or some other flag but I'm not sure. (Or compile it with a c++ compiler).
Reputation Points: 29
Solved Threads: 4
Junior Poster in Training
perniciosus is offline Offline
78 posts
since Nov 2005
Dec 10th, 2005
0

Re: complex declarations & other doubts

>2) Is there any special reason of- Why most C compilers include
>most of the header files implicitly, but in C++ we have to include them explicitly?
A correct and portable program must include the headers it uses explicitly or otherwise provide a compatible declaration for everything it uses from that header. However, you may have just gotten lucky because traditionally, if a declaration isn't specified in C, a default declaration is used. That default declaration is often compatible enough to avoid irritating the compiler and then the linker gives you the correct function. A lot of people consider this to be a misfeature (it was removed in the latest standard) and if you rely on it, you're treading on very thin ice. This is one of those areas where if you choose to abuse it, you'd better know damn well how it works in all cases.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to use wifstream to read a unicode file..
Next Thread in C++ Forum Timeline: Doubts in i/o and others in 'C'





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC