complex declarations & other doubts

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 15
Reputation: akshayabc is an unknown quantity at this point 
Solved Threads: 0
akshayabc akshayabc is offline Offline
Newbie Poster

complex declarations & other doubts

 
0
  #1
Dec 10th, 2005
1) Plz explain these complex declarations(from K&R).--
  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: complex declarations & other doubts

 
0
  #2
Dec 10th, 2005
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.

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.


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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Solved Threads: 4
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: complex declarations & other doubts

 
0
  #3
Dec 10th, 2005
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).
/pern.*/i

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,813
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: complex declarations & other doubts

 
0
  #4
Dec 10th, 2005
>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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC