SlideShare ist ein Scribd-Unternehmen logo
1 von 3
Downloaden Sie, um offline zu lesen
The Joy of
Programming
Understanding Pointers in C                                                                                          S.G. GaneSh

Pointers are the forte of C, are the most difficult to master, and programming with them is prone
to errors. But pointers are fun too! This month, we’ll look at some puzzles to understand some
interesting aspects of pointers in C.

In the following programs, assume that necessary header             to decide, so check the answers first:
files are included.                                                      A1. This program will run fine without an assertion failure.
     Q1. Will this program result in an assertion failure?          Sizes of all pointer types are equal! This might be surprising
                                                                    to many programmers, but it is easy to understand. Pointers
 int main() {                                                       signify an address. In general, for a given implementation,
          assert(sizeof(void *) == sizeof(int *));                  the storage space required for storing an address is the same,
          assert(sizeof(int *) == sizeof(int **));                  irrespective of the type of pointer used.
 }                                                                       A2. This program results in a compiler error for the
                                                                    expression ‘i + j’. Why? Pointers signify the address and it
     Q2. What will this program print?                              is illogical to add two addresses. However, you can add an
                                                                    integer value to an address; for example, an array-based
 int main() {                                                       address is a pointer and to locate an array element, it is
 int iarr[10];                                                      enough to simply add an integer to that address. Pointer
 int *i = &iarr[2], *j = &iarr[5];                                  subtraction is allowed; in this case, for example, ‘i – j’
 int *k = i + j;                                                    indicates the number of array elements between them, which
 int diff = j – i;                                                  is equivalent to the expression (&iarr[5] - & iarr[2]), and is
 printf(“%d”, diff);                                                always 3, irrespective of the size of int.
 }                                                                       A3. Old C compilers or modern C compilers in K&R C
                                                                    mode (which refers to pre-ANSI C -- the original C language
     Q3. Will this program work?                                    defined by Dennis M. Ritchie) do not have strong type
                                                                    checking and, hence, they will compile this fine.
 int main() {                                                            In the underlying implementation, if the size of int and
 int i = “C is often unpredictable!”;                               the size of the pointer are the same, then there is no problem
 printf(i);                                                         in storing the address of the string literal in integer i. printf
 }                                                                  is a dumb routine and it will interpret the first argument as
                                                                    a string (in fact, i has an address of a string literal). So, this
     Q4. What does the following program print?                     program might compile and print: “C is often unpredictable!”
                                                                         A4. Yes, this program will print “Joy”! Note that strncpy
 int main() {                                                       returns a char* which is the address of the copied string.
 char string[10];                                                   Here, strncpy copies three characters and returns that string.
 printf(strncpy(string ,”Joy of C”,3)[3] = ‘0’);                   Then, we do indexing on that returned char* and put the null
 }                                                                  terminator ‘0’ for that string in the index position [3]. The
                                                                    printf gets “Joy” as the argument and prints it.
     Q5. What does this following program print?                         A5) This program results in a compiler error for the
                                                                    expression ‘&&i’. The ‘&&’ operator is a logical ‘and’ operator
 int main() {                                                       and requires two operands. Ignoring this syntax issue, the
          // assume that address of i is 0x1234ABCD                 more important problem is that the attempted expression
 int i = 10;                                                        is illogical. It is possible to take ‘address of i’ with &i; but
 int * ip = &i;                                                     address of ‘address of i’ cannot exist!
 int **ipp = &&i;
                                                                     By: S G Ganesh is a research engineer at Siemens
 printf(“%x, %x, %x”, &i, ip, *ip);
                                                                     (Corporate Technology), Bangalore. His latest book is ‘60
 }                                                                   Tips on Object Oriented Programming’ published by Tata
                                                                     McGraw-Hill in December 2007. You can reach him at
     Well, they don’t seem too difficult, do they? It is too soon    sgganesh@gmail.com


                                                                         www.openITis.com   |   LINUX For YoU   |   FebrUarY 2008   127


                                                            cmyk
128   FebrUarY 2008   |   LINUX For YoU   |   www.openITis.com



                                                                 cmyk
www.openITis.com   |   LINUX For YoU   |   FebrUarY 2008   129


cmyk

Weitere ähnliche Inhalte

Was ist angesagt?

Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handlingRai University
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c languagegourav kottawar
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programmingIcaii Infotech
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in CTushar B Kute
 
Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in CShivanshuVerma11
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in Crgnikate
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview QuestionsGradeup
 

Was ist angesagt? (20)

Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Pointers in C language
Pointers  in  C languagePointers  in  C language
Pointers in C language
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
C pointers
C pointersC pointers
C pointers
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in C
 
Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in C
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C Programming Assignment
C Programming AssignmentC Programming Assignment
C Programming Assignment
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
13 Jo P Jan 08
13 Jo P Jan 0813 Jo P Jan 08
13 Jo P Jan 08
 

Andere mochten auch

三人同行一人Free
三人同行一人Free三人同行一人Free
三人同行一人Freeguest38274f
 
PARA MIS HIJOS!!
PARA MIS HIJOS!!PARA MIS HIJOS!!
PARA MIS HIJOS!!flipper_ebm
 
Moneyunlimtedgurpreetji
MoneyunlimtedgurpreetjiMoneyunlimtedgurpreetji
MoneyunlimtedgurpreetjiMIK958
 
UMAP 2013 - Link, Like, Follow, Friend: The Social Element in User Modeling a...
UMAP 2013 - Link, Like, Follow, Friend: The Social Element in User Modeling a...UMAP 2013 - Link, Like, Follow, Friend: The Social Element in User Modeling a...
UMAP 2013 - Link, Like, Follow, Friend: The Social Element in User Modeling a...gjhouben
 
ICWE2013 - Discovering links between political debates and media
ICWE2013 - Discovering links between political debates and mediaICWE2013 - Discovering links between political debates and media
ICWE2013 - Discovering links between political debates and mediagjhouben
 
Polityka Dynastyczna JagiellonóW
Polityka Dynastyczna JagiellonóWPolityka Dynastyczna JagiellonóW
Polityka Dynastyczna JagiellonóWguest7b6911
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz QuestionsGanesh Samarthyam
 

Andere mochten auch (9)

Cuenta Regresiva
Cuenta RegresivaCuenta Regresiva
Cuenta Regresiva
 
三人同行一人Free
三人同行一人Free三人同行一人Free
三人同行一人Free
 
Jdj Foss Java Tools
Jdj Foss Java ToolsJdj Foss Java Tools
Jdj Foss Java Tools
 
PARA MIS HIJOS!!
PARA MIS HIJOS!!PARA MIS HIJOS!!
PARA MIS HIJOS!!
 
Moneyunlimtedgurpreetji
MoneyunlimtedgurpreetjiMoneyunlimtedgurpreetji
Moneyunlimtedgurpreetji
 
UMAP 2013 - Link, Like, Follow, Friend: The Social Element in User Modeling a...
UMAP 2013 - Link, Like, Follow, Friend: The Social Element in User Modeling a...UMAP 2013 - Link, Like, Follow, Friend: The Social Element in User Modeling a...
UMAP 2013 - Link, Like, Follow, Friend: The Social Element in User Modeling a...
 
ICWE2013 - Discovering links between political debates and media
ICWE2013 - Discovering links between political debates and mediaICWE2013 - Discovering links between political debates and media
ICWE2013 - Discovering links between political debates and media
 
Polityka Dynastyczna JagiellonóW
Polityka Dynastyczna JagiellonóWPolityka Dynastyczna JagiellonóW
Polityka Dynastyczna JagiellonóW
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 

Ähnlich wie 14 Jo P Feb 08

Ähnlich wie 14 Jo P Feb 08 (20)

25 Jo P Jan 09
25 Jo P Jan 0925 Jo P Jan 09
25 Jo P Jan 09
 
C Programming
C ProgrammingC Programming
C Programming
 
(8) cpp abstractions separated_compilation_and_binding_part_i
(8) cpp abstractions separated_compilation_and_binding_part_i(8) cpp abstractions separated_compilation_and_binding_part_i
(8) cpp abstractions separated_compilation_and_binding_part_i
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
01 Jo P Jan 07
01 Jo P Jan 0701 Jo P Jan 07
01 Jo P Jan 07
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C
CC
C
 
10 Jo P Oct 07
10 Jo P Oct 0710 Jo P Oct 07
10 Jo P Oct 07
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 
07 -pointers_and_memory_alloc
07  -pointers_and_memory_alloc07  -pointers_and_memory_alloc
07 -pointers_and_memory_alloc
 
2 debugging-c
2 debugging-c2 debugging-c
2 debugging-c
 
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptxINDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
 
C language tutorial
C language tutorialC language tutorial
C language tutorial
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
علم البيانات - Data Sience
علم البيانات - Data Sience علم البيانات - Data Sience
علم البيانات - Data Sience
 

Mehr von Ganesh Samarthyam

Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeGanesh Samarthyam
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”Ganesh Samarthyam
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGanesh Samarthyam
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionGanesh Samarthyam
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationGanesh Samarthyam
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterGanesh Samarthyam
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Ganesh Samarthyam
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckGanesh Samarthyam
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageGanesh Samarthyam
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Ganesh Samarthyam
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz QuestionsGanesh Samarthyam
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizGanesh Samarthyam
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 

Mehr von Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 

Kürzlich hochgeladen

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 

Kürzlich hochgeladen (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

14 Jo P Feb 08

  • 1. The Joy of Programming Understanding Pointers in C S.G. GaneSh Pointers are the forte of C, are the most difficult to master, and programming with them is prone to errors. But pointers are fun too! This month, we’ll look at some puzzles to understand some interesting aspects of pointers in C. In the following programs, assume that necessary header to decide, so check the answers first: files are included. A1. This program will run fine without an assertion failure. Q1. Will this program result in an assertion failure? Sizes of all pointer types are equal! This might be surprising to many programmers, but it is easy to understand. Pointers int main() { signify an address. In general, for a given implementation, assert(sizeof(void *) == sizeof(int *)); the storage space required for storing an address is the same, assert(sizeof(int *) == sizeof(int **)); irrespective of the type of pointer used. } A2. This program results in a compiler error for the expression ‘i + j’. Why? Pointers signify the address and it Q2. What will this program print? is illogical to add two addresses. However, you can add an integer value to an address; for example, an array-based int main() { address is a pointer and to locate an array element, it is int iarr[10]; enough to simply add an integer to that address. Pointer int *i = &iarr[2], *j = &iarr[5]; subtraction is allowed; in this case, for example, ‘i – j’ int *k = i + j; indicates the number of array elements between them, which int diff = j – i; is equivalent to the expression (&iarr[5] - & iarr[2]), and is printf(“%d”, diff); always 3, irrespective of the size of int. } A3. Old C compilers or modern C compilers in K&R C mode (which refers to pre-ANSI C -- the original C language Q3. Will this program work? defined by Dennis M. Ritchie) do not have strong type checking and, hence, they will compile this fine. int main() { In the underlying implementation, if the size of int and int i = “C is often unpredictable!”; the size of the pointer are the same, then there is no problem printf(i); in storing the address of the string literal in integer i. printf } is a dumb routine and it will interpret the first argument as a string (in fact, i has an address of a string literal). So, this Q4. What does the following program print? program might compile and print: “C is often unpredictable!” A4. Yes, this program will print “Joy”! Note that strncpy int main() { returns a char* which is the address of the copied string. char string[10]; Here, strncpy copies three characters and returns that string. printf(strncpy(string ,”Joy of C”,3)[3] = ‘0’); Then, we do indexing on that returned char* and put the null } terminator ‘0’ for that string in the index position [3]. The printf gets “Joy” as the argument and prints it. Q5. What does this following program print? A5) This program results in a compiler error for the expression ‘&&i’. The ‘&&’ operator is a logical ‘and’ operator int main() { and requires two operands. Ignoring this syntax issue, the // assume that address of i is 0x1234ABCD more important problem is that the attempted expression int i = 10; is illogical. It is possible to take ‘address of i’ with &i; but int * ip = &i; address of ‘address of i’ cannot exist! int **ipp = &&i; By: S G Ganesh is a research engineer at Siemens printf(“%x, %x, %x”, &i, ip, *ip); (Corporate Technology), Bangalore. His latest book is ‘60 } Tips on Object Oriented Programming’ published by Tata McGraw-Hill in December 2007. You can reach him at Well, they don’t seem too difficult, do they? It is too soon sgganesh@gmail.com www.openITis.com | LINUX For YoU | FebrUarY 2008 127 cmyk
  • 2. 128 FebrUarY 2008 | LINUX For YoU | www.openITis.com cmyk
  • 3. www.openITis.com | LINUX For YoU | FebrUarY 2008 129 cmyk