SlideShare a Scribd company logo
1 of 9
Download to read offline
Saurav Mukherjee
https://www.linkedin.com/in/sauravmukherjee
June 2005
Complex C - declarations &
‘typedef’
1
Introduction
C is an immensely powerful programming language. It may appear as both a high-level and a low-
level programming language at the same time. It offers wide range of facilities /options to its
programmer. But, at times, it can get cryptic and its expressions can become mind-boggling. All of us
(i.e., C-programmers), at some time or other, got awe-inspired by the complexity of a C-declaration. In
this paper, I try to decipher some of those much-revered C-declarations and then utilize typedef
keyword of C language to simplify them. Along the way, I would enumerate few simple thumb rules,
which can enable us to read those declarations easily.
Reading Declarations in C
Let us begin with some straightforward declarations, e.g.,
int i; // ‘i’ is an integer
float *f; // ‘f’ is a pointer to a float
We can go beyond the basic types and see some user defined ones.
double *some_func (struct my_struct *st);
/* This is a function declaration. A function called some_func is being declared. It takes a pointer to a
structure of type my_struct and returns a pointer to variable of type double. */
But to really understand these, we need to understand how the compiler sees them. And before
getting into that, I would like to put down some examples, which I am going to use through out this
paper.
1. char * arr [25]; // arr is an array of 25 pointers to char OR is a pointer to an array of 25 chars?
2. long (* ptr_fn) (void);
3. int (*(* cryptic) (long)) [10];
4. float *(*(* r_u_mad) (int)) (double);
2
Thumb Rules
We have come across some thumb rules which makes life easier in understanding C-declarations.
Here they go:
1. First, read the variable that is being declared.
2. With the variable at the center, start spiraling out in anti-clockwise direction.
3. Use the following clues:
a. * means “pointer to”
b. [] means “array of”
c. () means “function taking … and returning …”
d. Read structure, union or enumeration at a single go. E.g., ‘struct st’, ‘union u’ or
‘enum en’.
e. Read adjacent collection of []’s (e.g., [][] - multi-dimensional arrays) all at once.
3
Examples Explained
With these thumb-rules, let us see what the examples have in store for us:
Example 1
char *arr [25];
1. The name of the variable being declared is “arr”.
2. Moving right from here (i.e., spiraling out in anti-clockwise direction), we get [25]; hence, we
say “array of 25”.
3. Spiraling forward anti-clockwise, we find *. Hence, “pointer(s) to”.
4. Finally, between ‘]’ and ‘;’, we get ‘char’, i.e. “character”.
Putting the pieces together, we get the following:
“arr is an array of 25 pointers to character”, OR “arr is an array containing 25 elements, each
element being a pointer to a character”.
Example 2
Moving on to the next example…
long (* ptr_fn) (void);
1. The variable is “ptr_fn”.
2. Spiraling inside the parenthesis, “pointer to”.
3. Next is … “function taking no parameters and returning”.
4. Lastly, “long”.
Hence, “ptr_fn is a pointer to a function taking no parameters and returning a long”.
Example 3
Let’s go to the next example…
4
int (*(* cryptic) (long)) [10];
1. “cryptic” is the variable being declared here.
2. It is a “pointer to”
3. “function taking a long and returning”
4. “pointer to”
5. “an array of 10”
6. “int”.
Hence, “cryptic is a pointer to a function taking a long and returning a pointer to an array of 10
integers”.
Example 4
The last example….
float *(*(* r_u_mad) (int)) (double);
1. “r_u_mad”
2. “pointer to”
3. “function taking an int and returning”
4. “pointer to”
5. “function taking a double and returning”
6. “pointer to”
7. “float”
Hence, “r_u_mad is a pointer to a function taking an integer and returning a pointer to a function
taking a double and returning a pointer to a float”.
Finally, we are thorough!! But things do not have to be this difficult! C is powerful enough to make
these declarations simple. We just need to remember the thumb-rules, which we formed to
circumvent the eccentricity of some whiz kid and apply a few more to use typedef for the same
purpose. Although these examples still inspire the same awe and reverence as it was doing when we
were at on page 1 in ‘Introduction’ section, let us move forward and simply them.
5
On the way to Simplification
typedef is C’s boon to us when we are caught in these kinds of circumstances. To avoid writing those
complex and circuitous declarations, we can leverage this keyword. I am going to use the same
examples as above to show how we can use typedef effectively. Steps to simplification are easy.
Apply the ‘divide & conquer’ rule to break the declaration into smaller steps and give them to the
compiler for creating easy-to-grasp types.
An interesting way to understand typedef may be to ignore it! Read it as if a variable is being
declared - just remember that once the variable is completely read, the complier declares a type
rather than a variable. It becomes a completely new compiler type and as valid as any basic data
type.
6
Examples Revisited
Let us go to the examples:
Simplify Example 1
char *arr [25];
“arr is an array of 25 pointers to character”
Starting from the end, let us create a new type, “pointer to character”. If we write,
char * pt_ch;
we declare a variable ‘pt_ch’ of type “pointer to character”. Putting a typedef before it, we get the
new type “pointer to character”.
typedef char * pt_ch;
Before proceeding, let us take a quick look at another benefit that we can derive from this - relieving
us from the dilemma of where to put ‘*’ while declaring a pointer.
Both the following declarations will declare a pointer to a character.
char* ch1;
char *ch2;
But if we need to declare them together, will the following be enough?
char* ch1, ch2;
Answer is a ‘no’. ‘ch1’ is a pointer to a character, but ‘ch2’ is just a character. But if we use the new
type, which we have just created, the confusion will not arise.
pt_ch ch1, ch2;
The above statement declares both ‘ch1’ and ‘ch2’ as pointers to character.
Coming back to the original problem, creating an “array of 25 pointers to character” is now just like
declaring a variable of any basic type.
pt_ch arr[25];
Let us move on to the next example.
7
Simplify Example 2
long (* ptr_fn) (void);
“ptr_fn is a pointer to a function taking no parameters and returning a long”.
Attacking the last portion first, we create a new type, “function taking no parameters and returning a
long” as follows:
typedef long fn_type (void);
The function prototype is preceded by a typedef to create the new type. Now we can declare ptr_fn
as easily as below.
fn_type *ptr_fn;
/* One more level of typedef is also possible, but do we really need that? */
Let us have a look at the 3rd example.
Simplify Example 3
int (*(* cryptic) (long)) [10];
“cryptic is a pointer to a function taking a long and returning a pointer to an array
of 10 integers”.
As usual we begin with the last part, which is “pointer to an array of 10 integers”. Dividing it in even
smaller steps, we generate the types as following:
typedef int arr_of_10_int[10];
typedef arr_of_10_int *p_arr_10_int;
Now, for a function taking a long as parameter and returning p_arr_10_int, we have,
typedef p_arr_10_int fn_t(long);
Now, just create ‘cryptic’ as following:
fn_t *cryptic;
8
The last example
Simplify Example 4
float *(*(* r_u_mad) (int)) (double);
“r_u_mad is a pointer to a function taking an integer and returning a pointer to a
function taking a double and returning a pointer to a float”.
Let us approach this awesome example also in the same way as we did before.
We start with the last part, i.e., “pointer to float”.
typedef float *ptf;
Now we find “function taking a double and returning ptf”. Hence, we write,
typedef ptf fn_dble(double);
Moving forward, we get “pointer to fn_dble”. Therefore,
typedef fn_dble *p_fn_dble;
Finally, “function taking an integer and returning p_fn_dble” -
typedef p_fn_dble maddening(int);
Hence, the declaration now becomes
maddening r_u_mad;
This brings us to an end of my venture of putting down some tips on how to simplify complex
declarations in C.

More Related Content

What's hot

Lecture 16 memory bounded search
Lecture 16 memory bounded searchLecture 16 memory bounded search
Lecture 16 memory bounded searchHema Kashyap
 
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...Ashish Duggal
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler DesignKuppusamy P
 
PWL Seattle #23 - A Few Useful Things to Know About Machine Learning
PWL Seattle #23 - A Few Useful Things to Know About Machine LearningPWL Seattle #23 - A Few Useful Things to Know About Machine Learning
PWL Seattle #23 - A Few Useful Things to Know About Machine LearningTristan Penman
 
K-MEDOIDS CLUSTERING USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...
K-MEDOIDS CLUSTERING  USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...K-MEDOIDS CLUSTERING  USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...
K-MEDOIDS CLUSTERING USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...ijscmc
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Iffat Anjum
 
knowledge representation using rules
knowledge representation using rulesknowledge representation using rules
knowledge representation using rulesHarini Balamurugan
 
Counting Elements in Streams
Counting Elements in StreamsCounting Elements in Streams
Counting Elements in StreamsJamie Grier
 
9: Record to Report Business Process
9: Record to Report Business Process9: Record to Report Business Process
9: Record to Report Business ProcessAbdullah Galal
 
Matching techniques
Matching techniquesMatching techniques
Matching techniquesNagpalkirti
 
Simplification of cfg ppt
Simplification of cfg pptSimplification of cfg ppt
Simplification of cfg pptShiela Rani
 
Configure automatic postings
Configure automatic postingsConfigure automatic postings
Configure automatic postingsarmindajack
 

What's hot (20)

Lecture 16 memory bounded search
Lecture 16 memory bounded searchLecture 16 memory bounded search
Lecture 16 memory bounded search
 
Int306 02
Int306 02Int306 02
Int306 02
 
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
PWL Seattle #23 - A Few Useful Things to Know About Machine Learning
PWL Seattle #23 - A Few Useful Things to Know About Machine LearningPWL Seattle #23 - A Few Useful Things to Know About Machine Learning
PWL Seattle #23 - A Few Useful Things to Know About Machine Learning
 
Combined COPA in SAP
Combined COPA in SAP  Combined COPA in SAP
Combined COPA in SAP
 
K-MEDOIDS CLUSTERING USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...
K-MEDOIDS CLUSTERING  USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...K-MEDOIDS CLUSTERING  USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...
K-MEDOIDS CLUSTERING USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...
 
weak slot and filler
weak slot and fillerweak slot and filler
weak slot and filler
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Reporting vs. Analytics
Reporting vs. AnalyticsReporting vs. Analytics
Reporting vs. Analytics
 
knowledge representation using rules
knowledge representation using rulesknowledge representation using rules
knowledge representation using rules
 
Counting Elements in Streams
Counting Elements in StreamsCounting Elements in Streams
Counting Elements in Streams
 
9: Record to Report Business Process
9: Record to Report Business Process9: Record to Report Business Process
9: Record to Report Business Process
 
Matching techniques
Matching techniquesMatching techniques
Matching techniques
 
Problems, Problem spaces and Search
Problems, Problem spaces and SearchProblems, Problem spaces and Search
Problems, Problem spaces and Search
 
Simplification of cfg ppt
Simplification of cfg pptSimplification of cfg ppt
Simplification of cfg ppt
 
Configure automatic postings
Configure automatic postingsConfigure automatic postings
Configure automatic postings
 

Similar to Complex C-declarations & typedef

Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxCatherineVania1
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2ndConnex
 
Under standing pointers in c 3rd edition
Under standing pointers in c 3rd editionUnder standing pointers in c 3rd edition
Under standing pointers in c 3rd editionMusa Gürbüz
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxmonicafrancis71118
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)SURBHI SAROHA
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5thConnex
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScriptAleš Najmann
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpprajshreemuthiah
 
(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-stringsNico Ludwig
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)Shoaib Ghachi
 
Lecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageLecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageSURAJ KUMAR
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02CIMAP
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docxJoeyDelaCruz22
 

Similar to Complex C-declarations & typedef (20)

Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptx
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
Under standing pointers in c 3rd edition
Under standing pointers in c 3rd editionUnder standing pointers in c 3rd edition
Under standing pointers in c 3rd edition
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
C operators
C operatorsC operators
C operators
 
(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings
 
C –FAQ:
C –FAQ:C –FAQ:
C –FAQ:
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)
 
Lecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageLecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming Language
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
GE3151 UNIT II Study material .pdf
GE3151 UNIT II Study material .pdfGE3151 UNIT II Study material .pdf
GE3151 UNIT II Study material .pdf
 
C# overview part 1
C# overview part 1C# overview part 1
C# overview part 1
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
 

More from Saurav Mukherjee

Enterprise Data Management - Data Lake - A Perspective
Enterprise Data Management - Data Lake - A PerspectiveEnterprise Data Management - Data Lake - A Perspective
Enterprise Data Management - Data Lake - A PerspectiveSaurav Mukherjee
 
Tire Pressure Monitoring System (TPMS) - An Introduction
Tire Pressure Monitoring System (TPMS) - An IntroductionTire Pressure Monitoring System (TPMS) - An Introduction
Tire Pressure Monitoring System (TPMS) - An IntroductionSaurav Mukherjee
 
Competitive positioning and routes to market for a high-technology innovation...
Competitive positioning and routes to market for a high-technology innovation...Competitive positioning and routes to market for a high-technology innovation...
Competitive positioning and routes to market for a high-technology innovation...Saurav Mukherjee
 

More from Saurav Mukherjee (6)

Linear regression theory
Linear regression theoryLinear regression theory
Linear regression theory
 
Presentation Skills
Presentation SkillsPresentation Skills
Presentation Skills
 
Enterprise Agile Adoption
Enterprise Agile AdoptionEnterprise Agile Adoption
Enterprise Agile Adoption
 
Enterprise Data Management - Data Lake - A Perspective
Enterprise Data Management - Data Lake - A PerspectiveEnterprise Data Management - Data Lake - A Perspective
Enterprise Data Management - Data Lake - A Perspective
 
Tire Pressure Monitoring System (TPMS) - An Introduction
Tire Pressure Monitoring System (TPMS) - An IntroductionTire Pressure Monitoring System (TPMS) - An Introduction
Tire Pressure Monitoring System (TPMS) - An Introduction
 
Competitive positioning and routes to market for a high-technology innovation...
Competitive positioning and routes to market for a high-technology innovation...Competitive positioning and routes to market for a high-technology innovation...
Competitive positioning and routes to market for a high-technology innovation...
 

Recently uploaded

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 

Recently uploaded (20)

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 

Complex C-declarations & typedef

  • 2. 1 Introduction C is an immensely powerful programming language. It may appear as both a high-level and a low- level programming language at the same time. It offers wide range of facilities /options to its programmer. But, at times, it can get cryptic and its expressions can become mind-boggling. All of us (i.e., C-programmers), at some time or other, got awe-inspired by the complexity of a C-declaration. In this paper, I try to decipher some of those much-revered C-declarations and then utilize typedef keyword of C language to simplify them. Along the way, I would enumerate few simple thumb rules, which can enable us to read those declarations easily. Reading Declarations in C Let us begin with some straightforward declarations, e.g., int i; // ‘i’ is an integer float *f; // ‘f’ is a pointer to a float We can go beyond the basic types and see some user defined ones. double *some_func (struct my_struct *st); /* This is a function declaration. A function called some_func is being declared. It takes a pointer to a structure of type my_struct and returns a pointer to variable of type double. */ But to really understand these, we need to understand how the compiler sees them. And before getting into that, I would like to put down some examples, which I am going to use through out this paper. 1. char * arr [25]; // arr is an array of 25 pointers to char OR is a pointer to an array of 25 chars? 2. long (* ptr_fn) (void); 3. int (*(* cryptic) (long)) [10]; 4. float *(*(* r_u_mad) (int)) (double);
  • 3. 2 Thumb Rules We have come across some thumb rules which makes life easier in understanding C-declarations. Here they go: 1. First, read the variable that is being declared. 2. With the variable at the center, start spiraling out in anti-clockwise direction. 3. Use the following clues: a. * means “pointer to” b. [] means “array of” c. () means “function taking … and returning …” d. Read structure, union or enumeration at a single go. E.g., ‘struct st’, ‘union u’ or ‘enum en’. e. Read adjacent collection of []’s (e.g., [][] - multi-dimensional arrays) all at once.
  • 4. 3 Examples Explained With these thumb-rules, let us see what the examples have in store for us: Example 1 char *arr [25]; 1. The name of the variable being declared is “arr”. 2. Moving right from here (i.e., spiraling out in anti-clockwise direction), we get [25]; hence, we say “array of 25”. 3. Spiraling forward anti-clockwise, we find *. Hence, “pointer(s) to”. 4. Finally, between ‘]’ and ‘;’, we get ‘char’, i.e. “character”. Putting the pieces together, we get the following: “arr is an array of 25 pointers to character”, OR “arr is an array containing 25 elements, each element being a pointer to a character”. Example 2 Moving on to the next example… long (* ptr_fn) (void); 1. The variable is “ptr_fn”. 2. Spiraling inside the parenthesis, “pointer to”. 3. Next is … “function taking no parameters and returning”. 4. Lastly, “long”. Hence, “ptr_fn is a pointer to a function taking no parameters and returning a long”. Example 3 Let’s go to the next example…
  • 5. 4 int (*(* cryptic) (long)) [10]; 1. “cryptic” is the variable being declared here. 2. It is a “pointer to” 3. “function taking a long and returning” 4. “pointer to” 5. “an array of 10” 6. “int”. Hence, “cryptic is a pointer to a function taking a long and returning a pointer to an array of 10 integers”. Example 4 The last example…. float *(*(* r_u_mad) (int)) (double); 1. “r_u_mad” 2. “pointer to” 3. “function taking an int and returning” 4. “pointer to” 5. “function taking a double and returning” 6. “pointer to” 7. “float” Hence, “r_u_mad is a pointer to a function taking an integer and returning a pointer to a function taking a double and returning a pointer to a float”. Finally, we are thorough!! But things do not have to be this difficult! C is powerful enough to make these declarations simple. We just need to remember the thumb-rules, which we formed to circumvent the eccentricity of some whiz kid and apply a few more to use typedef for the same purpose. Although these examples still inspire the same awe and reverence as it was doing when we were at on page 1 in ‘Introduction’ section, let us move forward and simply them.
  • 6. 5 On the way to Simplification typedef is C’s boon to us when we are caught in these kinds of circumstances. To avoid writing those complex and circuitous declarations, we can leverage this keyword. I am going to use the same examples as above to show how we can use typedef effectively. Steps to simplification are easy. Apply the ‘divide & conquer’ rule to break the declaration into smaller steps and give them to the compiler for creating easy-to-grasp types. An interesting way to understand typedef may be to ignore it! Read it as if a variable is being declared - just remember that once the variable is completely read, the complier declares a type rather than a variable. It becomes a completely new compiler type and as valid as any basic data type.
  • 7. 6 Examples Revisited Let us go to the examples: Simplify Example 1 char *arr [25]; “arr is an array of 25 pointers to character” Starting from the end, let us create a new type, “pointer to character”. If we write, char * pt_ch; we declare a variable ‘pt_ch’ of type “pointer to character”. Putting a typedef before it, we get the new type “pointer to character”. typedef char * pt_ch; Before proceeding, let us take a quick look at another benefit that we can derive from this - relieving us from the dilemma of where to put ‘*’ while declaring a pointer. Both the following declarations will declare a pointer to a character. char* ch1; char *ch2; But if we need to declare them together, will the following be enough? char* ch1, ch2; Answer is a ‘no’. ‘ch1’ is a pointer to a character, but ‘ch2’ is just a character. But if we use the new type, which we have just created, the confusion will not arise. pt_ch ch1, ch2; The above statement declares both ‘ch1’ and ‘ch2’ as pointers to character. Coming back to the original problem, creating an “array of 25 pointers to character” is now just like declaring a variable of any basic type. pt_ch arr[25]; Let us move on to the next example.
  • 8. 7 Simplify Example 2 long (* ptr_fn) (void); “ptr_fn is a pointer to a function taking no parameters and returning a long”. Attacking the last portion first, we create a new type, “function taking no parameters and returning a long” as follows: typedef long fn_type (void); The function prototype is preceded by a typedef to create the new type. Now we can declare ptr_fn as easily as below. fn_type *ptr_fn; /* One more level of typedef is also possible, but do we really need that? */ Let us have a look at the 3rd example. Simplify Example 3 int (*(* cryptic) (long)) [10]; “cryptic is a pointer to a function taking a long and returning a pointer to an array of 10 integers”. As usual we begin with the last part, which is “pointer to an array of 10 integers”. Dividing it in even smaller steps, we generate the types as following: typedef int arr_of_10_int[10]; typedef arr_of_10_int *p_arr_10_int; Now, for a function taking a long as parameter and returning p_arr_10_int, we have, typedef p_arr_10_int fn_t(long); Now, just create ‘cryptic’ as following: fn_t *cryptic;
  • 9. 8 The last example Simplify Example 4 float *(*(* r_u_mad) (int)) (double); “r_u_mad is a pointer to a function taking an integer and returning a pointer to a function taking a double and returning a pointer to a float”. Let us approach this awesome example also in the same way as we did before. We start with the last part, i.e., “pointer to float”. typedef float *ptf; Now we find “function taking a double and returning ptf”. Hence, we write, typedef ptf fn_dble(double); Moving forward, we get “pointer to fn_dble”. Therefore, typedef fn_dble *p_fn_dble; Finally, “function taking an integer and returning p_fn_dble” - typedef p_fn_dble maddening(int); Hence, the declaration now becomes maddening r_u_mad; This brings us to an end of my venture of putting down some tips on how to simplify complex declarations in C.