My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
Paid Surveys
|
C QUESTION ON DECLARATION(SECTION-9)
Posted Date: 27 Apr 2008 Resource Type: Articles/Knowledge Sharing Category: Jobs & Interviews
|
Posted By: umang garg Member Level: Gold Rating: Points: 5
|
|
|
|
64. How do you decide which integer type to use?
A: If you might need large values (above 32767 or below -32767), use long. Otherwise, if space is very important (there are large arrays or many structures), use short. Otherwise, use int. If well-defined overflow characteristics are important and/or negative values are not, use the corresponding unsigned types. (But beware mixtures of signed and unsigned.)
Similar arguments apply when deciding between float and double. Exceptions apply if the address of a variable is taken and must have a particular type.
Although char or unsigned char can be used as a "tiny" int type, doing so is often more trouble than it is worth.
65. I can't seem to define a linked list successfully. I tried
typedef struct { char *item; NODEPTR next; } *NODEPTR;
but the compiler gave me error messages. Can't a struct in C contain a pointer to itself?
A: Structs in C can certainly contain pointers to themselves; the discussion and example in section 6.5 of K&R make this clear. The problem with this example is that the NODEPTR typedef is not complete at the point where the "next" field is declared. To fix it, first give the structure a tag ("struct node"). Then, declare the "next" field as "struct node next;", and/or move the typedef declaration wholly before or wholly after the struct declaration. One fixed version would be
struct node { char *item; struct node *next; };
typedef struct node *NODEPTR;
, and there at least three other equivalently correct ways of arranging it.
A similar problem, with a similar solution, can arise when attempting to declare a pair of typedef'ed mutually recursive structures.
References: K&R I Sec. 6.5 p. 101; K&R II Sec. 6.5 p. 139; H&S Sec. 5.6.1 p. 102; ANSI Sec. 3.5.2.3.
66. How do I declare an array of pointers to functions returning pointers to functions returning pointers to characters?
A: This question can be answered in at least three ways (all assume the hypothetical array is to have 5 elements):
1. char *(*(*a[5])())();
2. Build the declaration up in stages, using typedefs:
typedef char *pc; /* pointer to char */ typedef pc fpc(); /* function returning pointer to char */ typedef fpc *pfpc; /* pointer to above */ typedef pfpc fpfpc(); /* function returning... */ typedef fpfpc *pfpfpc; /* pointer to... */ pfpfpc a[5]; /* array of... */
3. Use the cdecl program, which turns English into C and vice versa:
cdecl> declare a as array 5 of pointer to function returning pointer to function returning pointer to char char *(*(*a[5])())()
cdecl can also explain complicated declarations, help with casts, and indicate which set of parentheses the arguments go in (for complicated function definitions, like the above).
Any good book on C should explain how to read these complicated C declarations "inside out" to understand them ("declaration mimics use").
Reference: H&S Sec. 5.10.1 p. 116.
67. So where can I get cdecl?
A: Several public-domain versions are available. One is in volume 14 of comp.sources.unix . (See question 94.)
Reference: K&R II Sec. 5.12 .
68. I finally figured out the syntax for declaring pointers to functions, but now how do I initialize one?
A: Use something like
extern int func(); int (*fp)() = func;
When the name of a function appears in an expression but is not being called (i.e. is not followed by a "("), it "decays" into a pointer (i.e. it has its address implicitly taken), much as an array name does.
An explicit extern declaration for the function is normally needed, since implicit external function declaration does not happen in this case (again, because the function name is not followed by a "(").
69. I've seen different methods used for calling through pointers to functions. What's the story?
A: Originally, a pointer to a function had to be "turned into" a "real" function, with the * operator (and an extra pair of parentheses, to keep the precedence straight), before calling:
int r, f(), (*fp)() = f; r = (*fp)();
Another analysis holds that functions are always called through pointers, but that "real" functions decay implicitly into pointers (in expressions, as they do in initializations) and so cause no trouble. This reasoning, which was adopted in the ANSI standard, means that
r = fp();
is legal and works correctly, whether fp is a function or a pointer to one. (The usage has always been unambiguous; there is nothing you ever could have done with a function pointer followed by an argument list except call through it.) An explicit * is harmless, and still allowed (and recommended, if portability to older compilers is important).
References: ANSI Sec. 3.3.2.2 p. 41, Rationale p. 41.
|
Responses
|
| Author: umang garg 27 Apr 2008 | Member Level: Gold Points : 2 | THESE ARE THE QUESTION WHICH REALLY MAKES YOU UNDERSTAND WHOLE THING ABOUT DECLARATION IN C.
|
|
|