Complicated Declarations In C
By Rahul Budholiya
Founder Lunaplena Apps
rahulbudholiya.blogspot.com
www.linkedin.com/pub/r...
Declaration And Definition
Definition:
Function:
int add(int a,int b)
{
retrun a+b;
}
Variable:
int a;
User Defined data
t...
Declaration of Pointers.
General Syntax:
[name of data type] *<variable_name>;
Here [name of data type] is data type of va...
Declaration of Pointers.
There are pointer type in c, like int * is a type of int
pointer.
If we assume int * is a type, a...
Keywords and operators that
affects declarations
Const Phenomenon
Const is a keyword which defines a read only variable.
Variable declared with const keyword can only be a...
Const Phenomenon
What is the difference between following two declarations ?
const int *p;
int const *p;
Answer:
There is ...
Const Phenomenon
What is the difference between following two declarations ?
const int *p;
int * const p;
Answer:
First is...
near,far and huge
near,far and huge pointers
near,far and huge are used with pointers.
•These keywords were introduced to support memory
seg...
near,far and huge pointers
Which is the following is a near pointer ?
char near * far *ptr;
char near * huge *ptr2;
char f...
Using () operator in declarations.
Using () operator in declarations.
Like any other instruction of c, part of declarative
instruction contained by () operat...
Using [] operator in declarations
[] operator in a declaration define size of memory by
multiplying size of data type spec...
Using [] operator in declarations
Observe following two declarations.
int *p[10];
int (*p)[10];
First one allocates 20 byt...
Use of () And [] in function pointer declarations.
Use of () And [] in function pointer declarations.
We can have pointers to functions in c.
Read following declaration.
int...
Use of () and [] in function pointer declarations.
Now, how we can declare a array of pointers of following function
int *...
Can you read following declaration ?
void (*f())(void(*)(int *,void**),int(*)
(void**,int*));Answer:
Yes
Rules to read declaration in c
1. Start with the inner most () containing variable
name.
2. Read the declarations clock wi...
Rules to read declarations in c.
Char ( * ( * f () ) [] ) ();
1
2
3
4
5
6
Rules to read declarations in c.
Char ( * ( * f () ) [] ) ();
F is function which returns a pointer to an array of pointer...
Can you read following declaration ?
void (*f())(void(*)(int *,void**),int(*)
(void**,int*));Answer:
Yes
In above declarat...
Can you declare a array of pointers of a
function which receives four
parameters all of following type and
returns followi...
Now typedef comes in picture.
How typedef works.
What are the types of var1,var2,var3 and var4 in following program ?
#include<stdio.h>
#define characte...
Will following code compile?
typedef struct
{
int data;
NODEPTR next;
} *NODEPTR;
Answer: No
A typedef declaration can not...
Any variable declaration can be
converted by typedef.
typedef int arr[10];
arr a;
typedef int (*p)(int,int);
p func();
str...
Can you declare a array of pointers of a function
which receives four parameters all of following
type and returns followi...
Questions
What will be the output of
following program ?
#include<stdio.h>
int main()
{
char near * near *ptr1;
char near * far *ptr...
Will following code compile
without error ?
#include<stdio.h>
structr student;
int main()
{
struct student a;
return 0;
}
...
Will following code compile
without error ?
#include<stdio.h>
structr student;
int main()
{
struct student a;
a.roll=10;
P...
Read following declaration.
char (*(*x[3])())[5];
Answer:
x is an array of 3 function pointers, where each pointer points ...
Read following declaration.
void (*f[10])(int,int);
Answer:
f is an array of 10 function pointers, where each pointer poin...
Read following declaration.
int (*ftable[])(void) ={fadd,fsub,fmul,fdiv};
Answer:
Ftable is an array of 5 function pointer...
Read following declaration.
int ** (*f)(int **,int **(*)(int **,int **));
Answer:
f is a pointer to a function which retur...
What do the following declaration means ?
Typedef char *pc;
Typedef pc fpc();
Typedef fpc *pfpc;
Typedef pfpc fpfpc();
Typ...
All The Best!!!
For more stuff visit
rahulbudholiya.blogspot.com
Upcoming SlideShare
Loading in …5
×

Complicated declarations in c

1,706 views

Published on

Many times you might have wondered about all the complex and strange syntaxes of functions, variables and pointers in c. But these declarations are not as complex as it looks at first. When you know where to look and how to look, reading these syntaxes would become a piece of cake. Just see this presentation and you will find it very easy to read all the complexities in c.

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,706
On SlideShare
0
From Embeds
0
Number of Embeds
188
Actions
Shares
0
Downloads
56
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Complicated declarations in c

  1. 1. Complicated Declarations In C By Rahul Budholiya Founder Lunaplena Apps rahulbudholiya.blogspot.com www.linkedin.com/pub/rahul-budholiya/34/53/b35 www.facebook.com/rahul.budholiya.dev
  2. 2. Declaration And Definition Definition: Function: int add(int a,int b) { retrun a+b; } Variable: int a; User Defined data type: union student { int a; char c[2]; }; Declaration: Function: int add(int,int); extern int add(int,int); Variable: extern int a; User Defined data type: union student; extern union student;
  3. 3. Declaration of Pointers. General Syntax: [name of data type] *<variable_name>; Here [name of data type] is data type of variable, address of which this pointer can contain. But it is not as simple as, above syntax is seems like. All the keywords placed before ‘*’ does not affect properties (they define properties of variable which can be pointed by this pointer) of pointer variable. If we need to affect properties of pointer variable all keywords must be placed after * sign(but there are exceptions like far,huge and near).
  4. 4. Declaration of Pointers. There are pointer type in c, like int * is a type of int pointer. If we assume int * is a type, and we know that we can create pointers of any type by writing ‘[type name] * ‘. So if we write int* * then it will create pointer of a int pointer. We can define a pointer to any other pointer in c.
  5. 5. Keywords and operators that affects declarations
  6. 6. Const Phenomenon Const is a keyword which defines a read only variable. Variable declared with const keyword can only be assigned a value once in lifetime. Syntax: const type <variable_name>; Type const <variable_name>; Ex:- const int a; int const a;
  7. 7. Const Phenomenon What is the difference between following two declarations ? const int *p; int const *p; Answer: There is no difference, both refers to same meaning.
  8. 8. Const Phenomenon What is the difference between following two declarations ? const int *p; int * const p; Answer: First is a pointer to a constant int(*p=10 is wrong). And second is a constant pointer to int.(p=10 is wrong).
  9. 9. near,far and huge
  10. 10. near,far and huge pointers near,far and huge are used with pointers. •These keywords were introduced to support memory segmentation. •Size of a near pointer is 2 bytes, and size of huge and far pointers is 4 bytes. •Near far and huge pointers only supported by Borland tc under dos, 32 bit platforms like window, linux and unix does not support these keywords, there are all pointers are 32 bit.
  11. 11. near,far and huge pointers Which is the following is a near pointer ? char near * far *ptr; char near * huge *ptr2; char far * huge* ptr3; Answer: No one is near pointer;
  12. 12. Using () operator in declarations.
  13. 13. Using () operator in declarations. Like any other instruction of c, part of declarative instruction contained by () operator executes separately, and () operator defined by inner most ‘()’(level) executes first and then one who has higher level of nesting. Syntex: Exp1(Exp2….(ExpN <variable_name>)); Exp1 ,Exp…ExpN are all sub expressions of a declarative statement.
  14. 14. Using [] operator in declarations [] operator in a declaration define size of memory by multiplying size of data type specified in statement with the size specified in [] operator. But what will happen with this memory size it depends on declaration.
  15. 15. Using [] operator in declarations Observe following two declarations. int *p[10]; int (*p)[10]; First one allocates 20 bytes and second one allocates two bytes. First is a array of 10 int pointers. And second is a pointer to a block which has size of 20 bytes. In first statement [10] will going to be read first by compiler. So compiler thought p as a array of 10 elements and then it looks up for type of these elements which it finds int *. In Second statement () will executes first by compiler, so compiler thought p as a pointer and then it looks up for type of this pointer which it finds int [10].
  16. 16. Use of () And [] in function pointer declarations.
  17. 17. Use of () And [] in function pointer declarations. We can have pointers to functions in c. Read following declaration. int *p(int,int); Above is a declaration. Of a function named p with two int parameters and int * as return type. How can we declare a pointer to this function ? Answer: int * (*p)(int,int); Now p is a pointer to a function which has two int parameters and int *as return type.
  18. 18. Use of () and [] in function pointer declarations. Now, how we can declare a array of pointers of following function int *p(int,int); int (*(p[10]))(int,int); In above declaration p is a array of 10 pointers of a function which has two int parameters and int * as return type. Now you can see, above is not the general declaration syntax of pointers, as we saw earlier [name of data type] *<variable_name>; Now finally we came to some real complicated declarations.
  19. 19. Can you read following declaration ? void (*f())(void(*)(int *,void**),int(*) (void**,int*));Answer: Yes
  20. 20. Rules to read declaration in c 1. Start with the inner most () containing variable name. 2. Read the declarations clock wise. Read left first in the same () and then right. 3. Read from inner most () to outer most().
  21. 21. Rules to read declarations in c. Char ( * ( * f () ) [] ) (); 1 2 3 4 5 6
  22. 22. Rules to read declarations in c. Char ( * ( * f () ) [] ) (); F is function which returns a pointer to an array of pointers to a function which returns an char.
  23. 23. Can you read following declaration ? void (*f())(void(*)(int *,void**),int(*) (void**,int*));Answer: Yes In above declaration f is a function which returns a pointer to a function which returns nothing and receives two parameters. First is a pointer to a function which returns nothing but receives two parameters first is pointer to an int and second is a pointer to a void pointer. The second parameter is a pointer to a function which retruns an int an receives two parameters first is pointer to a void pointer and second is a pointer to a int.
  24. 24. Can you declare a array of pointers of a function which receives four parameters all of following type and returns following pointer. void (*f())(void(*)(int *,void**),int(*) (void**,int*)); Answer: Do I Look like a fool ?
  25. 25. Now typedef comes in picture.
  26. 26. How typedef works. What are the types of var1,var2,var3 and var4 in following program ? #include<stdio.h> #define character char * Void main() { Typedef char * CHARAT character var1,var2; CHARAT var3,var4; } Answer: In above code var1,var3,var4 are pointers to char and var2 is char variable.
  27. 27. Will following code compile? typedef struct { int data; NODEPTR next; } *NODEPTR; Answer: No A typedef declaration can not be used until it is defined, and in following example it not defined when it is used.
  28. 28. Any variable declaration can be converted by typedef. typedef int arr[10]; arr a; typedef int (*p)(int,int); p func(); struct student Stu1,Stu2; Stu1 g; Stu2 b;
  29. 29. Can you declare a array of pointers of a function which receives four parameters all of following type and returns following pointer. void (*f())(void(*)(int *,void**),int(*) (void**,int*)); Answer: typedef void (*f())(void(*)(int *,void**),int(*)(void**,int*)); f(*g[10])(f,f,f,f);
  30. 30. Questions
  31. 31. What will be the output of following program ? #include<stdio.h> int main() { char near * near *ptr1; char near * far *ptr2; char near * huge *ptr3; printf(“%d %d %d”,sizeof(ptr1),sizeof(ptr2),sizeof(ptr3)); return 0; } Answer: 2,4,4
  32. 32. Will following code compile without error ? #include<stdio.h> structr student; int main() { struct student a; return 0; } struct student { int roll; }; Answer: Above code will compile without any error.
  33. 33. Will following code compile without error ? #include<stdio.h> structr student; int main() { struct student a; a.roll=10; Printf(“%d”); return 0; } struct student { int roll; }; Answer: above code will not compile.
  34. 34. Read following declaration. char (*(*x[3])())[5]; Answer: x is an array of 3 function pointers, where each pointer points to a function that returns a pointer to array of 5 chars.
  35. 35. Read following declaration. void (*f[10])(int,int); Answer: f is an array of 10 function pointers, where each pointer points to a function that receives two ints and returns nothing.
  36. 36. Read following declaration. int (*ftable[])(void) ={fadd,fsub,fmul,fdiv}; Answer: Ftable is an array of 5 function pointers which points to the function fadd(),fsub(),fmul(),fdiv()
  37. 37. Read following declaration. int ** (*f)(int **,int **(*)(int **,int **)); Answer: f is a pointer to a function which returns a pointer to int pointer and receives two parameters . First is a pointer of pointer of int. And second is a pointer to a function which receives two pointers of int pointer as parameters and returns a pointer to int pointer.
  38. 38. What do the following declaration means ? Typedef char *pc; Typedef pc fpc(); Typedef fpc *pfpc; Typedef pfpc fpfpc(); Typedef fpfpc *pfpfpc; Pfpfpc a[10]; pc is a pointer to char. fpc is a function which returns a pointer to char. pfpc is a pointer to a function returning pointer to a char. fpfpc is a function returning pointer to a function returning pointer to a char. pfpfpc is a pointer to function returning pointer to a function returning pointer to char. pfpfpc a[10] is an array of 10 pointers to a function returning pointers to functions returning pointer to characters.
  39. 39. All The Best!!! For more stuff visit rahulbudholiya.blogspot.com

×