954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

complex declarations & other doubts

1) Plz explain these complex declarations(from K&R).--

char(* (*f())[]) ()           // f : a function returning pointer to array[] of pointer to function returning char.
     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?

akshayabc
Newbie Poster
15 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 
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.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.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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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).

perniciosus
Junior Poster in Training
78 posts since Nov 2005
Reputation Points: 29
Solved Threads: 4
 

>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. ;)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You