Answer: Let’s try some examples of declaration of variables in C,
int x; int *y;
Observe that x is declared to be an integer. What about *y? Expression *y is also an integer. We deduce from here that y is a pointer to integer. This assertion can be validated in C as,
int* x, y;
What are x and y, now? Let’s try them in a simple C program,
#include <stdio.h> int main(void) { int* x = 10, y; return 0; }
What happens when you compile this program, it turns out with a warning,
warning: initialization makes pointer from integer without a cast [enabled by default]
Notice here that despite the spacing, asterisk(*) is associated with x making it a pointer to int while y is an ordinary integer.
Try one more example,
int xyz(int, int);
xyz is a function which takes two integer arguments and returns an int.
Remember that rules for declaration by inference for reading declarations are the same as those for ordinary expressions.
We can try several other expressions using declaration by inference, as,
int (*f)(int, int); int (*x[5])(char, char);
Well! If declaration by inference seems to be a confusing for complex declarations, you can apply the same rules for evaluation of declarations as there are for other expressions in C. There are no separate rules or separate syntax. Let’s figure out the above two complex declarations below,
int (*f)(int, int); int (*x[5])(char, char);
Notice that in first declaration, we have two sets of parenthesis of which first one being grouping has higher precedence making f a pointer to function that takes two integers as arguments and returns an integer.
In second declaration, again grouping goes first, making x an array of pointers, then second set of parenthesis, a function call, evaluates and results x an array of pointers to functions which take two char type values and return int.
You can this way evaluate any complex declaration. Fortunately, there are programs called cdecl for c declarations and c++decl for c++ declarations, which don’t come pre-installed on Linux Machines, which convert between English and declarations. Below given link, you can use to download these programs.
http://www.ibiblio.org/pub/Linux/devel/lang/c/
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice BCA MCQs
- Buy Computer Science Books
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Apply for C Internship