SlideShare a Scribd company logo
1 of 34
Workshop India
    Wilson Wingston Sharon
wingston.sharon@gmail.com
#include <stdio.h>

                                            int main()
   A variable is something with a          {
    name.                                   printf("size of a short is
                                            %dn", sizeof(short));
   The value of that variable is not       printf("size of a int is
    fixed.                                  %dn", sizeof(int));
                                            printf("size of a long is
                                            %dn", sizeof(long));
   Size of the variable depends on the     }
    datatype.

   The program on the right will tell
    you the size of the variables in your
    system.
   int k;

   What does the above statement mean?
     Int means the compiler needs to set aside 4 bytes of
      memory to hold this particular value k.
     This variable k is going to be stored in the computers
      RAM.
       What is the value of k at this time?
   k = 2;
     Now the value 2 is going to be placed in the memory
      location for k.
     So if we check the place in RAM where the variable is
      stored, we will find 2 there.
Address | Value
 RAM  is the storage area for the   x000101|   0
  computer.                          x000102|   0       k
                                     x000103|   0
 It remembers things as long as     x000104|   2
  the computer is on.                x000105|   Value
                                     x000106|   Value
 The RAM is like a register book.   x000107|   Value
 Each address location is a byte.


 Soif k begins at 0x000101, it
 continues till 0x000104.
 If you take an = operator
 Lvalue = Rvalue


 These Values have their own rules.
 Lvalue must be a named region of storage.
 Rvalue must be an expression that returns some
  value.
 k = 2;
 j = 3;
 k = j;


   2 = k;   makes no sense.
k   = 2;
    Lvalue is variable K
    Rvalue is 2
    2 is stored in variable k

k   = j;
    Lvalue is variable k
    Rvalue is value stored in variable j
    Value of variable j is now copied to k.

2   = k;
    Really? Can this be possible?
k

k  = 2;                     2

 Name is k             0x001

 Value is 2
 Starting address is
  0x001
                        c
                              ?
 char c = „b‟;             0x00A
 Value = ?
 Address = ?
 Pointers are variables who‟s values is the address
  of another memory location on the RAM.
 Pointers are variables like any other data type.


   The value they contain are special – they store an
    address.

   int *ptr;
       This statement informs the compiler that we want a
        variable balled ptr that will hold the address of another
        integer variable.
       What does ptr at this point?
k

& is an operator that         2
 returns the address of   0x001
 the variable.

 printf(“%d”, k );
                          c
 printf(“%d”, &k );
                               „c‟
                              0x00A
 printf(“%c”, c );
 printf(“%d”, &c );
 intk;
                    var       k
 k = 2;
                          ?         2
                    0x00A      0x001
 int *ptr;
 ptr = &k;
                              ptr
                                    ?
 int   *var = k;
                              0x00A
 * called a deferefencer.
 It is applicable only for a pointer data type and it
  goes to memory location of that pointer.

   &k
       Return the address of the variable k in RAM
       Can only be Rvalue.
   k
       Return that value that k has in RAM
       Can be both Rvalue and Lvalue
   *k
     Go to the memory address in k and lets work with that
      value.
     Can be both Rvalue and Lvalue
 printf(“%d”,   k );
                           var     k
                           0x001         2
 printf(“%d”,   ptr );
                           0x00A    0x001

 printf(“%d”,   *var );
                                   ptr
                                   0x001
 printf(“%d”,   &var);
                                   0x00A


 printf(“%d”,   *ptr);
 printf(“%d”,   k );
     2                    var     k

 printf(“%d”,   ptr );    0x001         2

     0x001                0x00A    0x001

 printf(“%d”,   *var );
     2                            ptr

 printf(“%d”,   &var);            0x001

     0x00A                        0x00A

 printf(“%d”,   *ptr);
     2
A  variable is declared by giving it a type and a
  name (e.g. int k;)
 A pointer variable is declared by giving it a
  type and a name (e.g. int *ptr) where the
  asterisk tells the compiler that the variable
  named ptr is a pointer variable and the type
  tells the compiler what type the pointer is to
  point to (integer in this case).
 Once a variable is declared, we can get its
  address by preceding its name with the
  unary &operator, as in &k.
 Time:   15 minutes

 Write
      3 different programs that uses the
 concepts you just learnt

 Familiarize   yourselves with pointers.
 If*ptr is a pointer pointing to a variable,
 ptr + 1 doesn‟t simply increment the pointer
  by 1

 The    compiler changes the expression to
      ptr + 1 * sizeof(<datatype>)


 Ifptr was an integer of size()=4, then the
  next integer will be 4 memory blocks away.
 int k = 2;                           k
                         ptr                2
 int *ptr = &k;
                               ?       0x001
 int *v1 = ptr + 2;
                         0x00A              7
 int *v2 = v + 1;
                         v1            0x005

                                            29
 printf(“%d”, *ptr );         ?
                         0x00B         0x001
 printf(“%d”, *v1 );
                                            -2
 printf(“%d”, *v2 );    v2
                                           0x001
 printf(“%d”, v2 );               ?

 printf(“%d”, &v2 );     0x00C
   int   k = 2;                    k
   int   *ptr = &k;
                           ptr           2
   int   *v1 = ptr + 2;
   int   *v2 = v + 1;     0x001    0x001
                           0x01A         7
   printf(“%d”, *ptr );
       2                           0x005
                           v1
   printf(“%d”, *v1 );
       29                 0x009         29
   printf(“%d”, *v2 );    0x01E    0x009
       -2
   printf(“%d”, v2 );                   -2
                           v2
       0x00D                           0x00D
   printf(“%d”, &v2 );     0x00D
       0x03F               0x03F
   int array[] = {1,23,4,5,-100};

 The variable is a pointer pointing to the first
  element of the array .
 All other arrays are stored sequentially after that.


 array[1] = ?
 *(arr+1) = ?
 *arr + 1 = ?


 arr[3] = ?
 *(arr+3) = ?
 *arr + 3 = ?
   int array[] = {1,23,4,5,-100};

 The variable is a pointer pointing to the first
  element of the array .
 All other arrays are stored sequentially after that.


 array[1] = 23
 *(arr+1) = 23
 *arr + 1 = 24


 arr[3] = 5
 *(arr+3) = 5
 *arr + 3 = 8
 int array[] = {1,23,7,5,10,78,9,87};
 int *ptr = array + 2;


 array[1] =?
 *(ptr+1) = ?
 *array + 3 = ?
 array[4] = ?
 *ptr + 1 = ?
 &array[0] = ?
 int array[] = {1,23,7,5,10,78,9,87};
 int *ptr = array + 2;


 array[1] = 23
 *(ptr+1) = 5
 *array + 3 = 5
 array[4] = 10
 *ptr + 1 = 8
 &array[0] = array
 Most people will say the name of an array is a
  pointer.

 No – the name of an array is a constant
  pointer to the first element of the array.

 i.e.   int array[5];
     Is array[1] an Lvalue or Rvalue?
     Is array an Lvalue or Rvalue?
     is *array an Lvalue or Rvalue?
 int   arr[] = {1,2,3,4,5,6,7};

 arr[2]   =?

 *(arr   + 2) = ?

 *(2   + arr) = ?

 2[arr]   =?
 int   arr[] = {1,2,3,4,5,6,7};

 arr[2]   =3

 *(arr   + 2) = 3

 *(2   + arr) = 3

 2[arr]   =3
 The   best teacher is yourself.

 Make   an array.
 Decide what output you want.
 Write code to check that output.
 If not, call us and we‟ll help you.


 Ifyou consistently get the output the same as
  what you predicted. Congratulations – you are
  learning!
 Used   for packaging variables.

 Arrays
       are collection of lots of variables of
 same type under one name.

 Structsare collections of a number of
 variables of different types under one name.

 Structure   definition and variable creation are
 different.
struct person
                                    {
 The  following code makes a                char *name;
  struct called person.                      int marks;
                                             float phonenum;
 It defines a string pointer for            person *sibling;
  the name, an integer field for    };
  marks
 A float field for phone number


 And a pointer to another
 person only to say that they‟re
 a sibling.
struct person
 Once  the definition is set, we            {
                                                      char name[20];
  make variables or objects based                     int marks;
  on this template.                                   float phonenum;
                                                      person *sibling;
 Now “person” is a valid data type.         };
 struct person ram;
     Creates an object called ram of type
      person.
 Strcpy(ram.name,      “Hello”);
 ram.marks= “5”;
 ram.phonenum = 4374389;
     Is how you acces elements inside a
      structure
struct person
                                              {
 struct
      person ali = {“Ali”, 78,                         char name[20];
 893043, NULL};                                        int marks;
                                                       float phonenum;
     Creates new object of type person                person *sibling;
      with the above values.                  };

                                              struct person ali = {“Ali”,
 Lookat mike definition. The                 78, 893043, NULL};
 person* field has been given &ali.           struct person mike=
     So mikes sibling field points to ali!   {“mike”, 78, 893043,
                                              &ali};
struct person
 The . (dot) operator lets us        {
                                               char name[20];
  access the structure elements.               int marks;
                                               float phonenum;
                                               person *sibling;
 Ifyou are pointing to a structure   };
  you have to use. (->)
                                      struct person ali = {“Ali”,
                                      78, 893043, NULL};
 structperson *ptr = ali;
                                      struct person mike=
 ptr->marks;                         {“mike”, 78, 893043,
      ?                              &ali};

 ptr->sibling;
      ?
struct person
 The . (dot) operator lets us        {
                                               char name[20];
  access the structure elements.               int marks;
                                               float phonenum;
                                               person *sibling;
 Ifyou are pointing to a structure   };
  you have to use. (->)
                                      struct person ali = {“Ali”,
                                      78, 893043, NULL};
 structperson *ptr = ali;
                                      struct person mike=
 ptr->marks;                         {“mike”, 78, 893043,
      78                             &ali};

 ptr->sibling;
      NULL
struct person
 we see the object mike of type            {
  person.                                            char name[20];
                                                     int marks;
 Its sibling field has been set to point
                                                     float phonenum;
  to ali.                                            person *sibling;
                                            };
   mike.name
                                            struct person ali = {“Ali”,
       ?                                   78, 893043, NULL};
   mike.sibling
       ?                                   struct person mike=
   mike.sibling->name                      {“mike”, 78, 893043,
                                            &ali};
       ?
   Mike.sibling->sibling
       ?
struct person
 we see the object mike of type            {
  person.                                            char name[20];
                                                     int marks;
 Its sibling field has been set to point
                                                     float phonenum;
  to ali.                                            person *sibling;
                                            };
   mike.name
                                            struct person ali = {“Ali”,
       mike                                78, 893043, NULL};
   mike.sibling
       //memory address of object ali      struct person mike=
   mike.sibling->name                      {“mike”, 78, 893043,
                                            &ali};
       ali
   Mike.sibling->sibling
       NULL

More Related Content

What's hot

Pointers in c
Pointers in cPointers in c
Pointers in c
Mohd Arif
 
structure and union
structure and unionstructure and union
structure and union
student
 

What's hot (20)

Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Pointers in c++
Pointers in c++Pointers 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...
 
Strings in c
Strings in cStrings in c
Strings in c
 
pointers
pointerspointers
pointers
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
C pointers
C pointersC pointers
C pointers
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
structure and union
structure and unionstructure and union
structure and union
 
Functions in C
Functions in CFunctions in C
Functions in C
 

Viewers also liked

C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
vinay arora
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
tech4us
 

Viewers also liked (20)

Pointers
PointersPointers
Pointers
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Ponters
PontersPonters
Ponters
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
CSE240 Pointers
CSE240 PointersCSE240 Pointers
CSE240 Pointers
 
More Pointers and Arrays
More Pointers and ArraysMore Pointers and Arrays
More Pointers and Arrays
 
Pointers - DataStructures
Pointers - DataStructuresPointers - DataStructures
Pointers - DataStructures
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Structure c
Structure cStructure c
Structure c
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
intro to pointer C++
intro to  pointer C++intro to  pointer C++
intro to pointer C++
 
Structure in C
Structure in CStructure in C
Structure in C
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
 
Structure in c
Structure in cStructure in c
Structure in c
 
Structure in C
Structure in CStructure in C
Structure in C
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 

Similar to C programming - Pointers

1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
hara69
 

Similar to C programming - Pointers (20)

Pointers.pdf
Pointers.pdfPointers.pdf
Pointers.pdf
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
C pointers and references
C pointers and referencesC pointers and references
C pointers and references
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointer
PointerPointer
Pointer
 
Pointers
PointersPointers
Pointers
 
Ch 7-pointers
Ch 7-pointersCh 7-pointers
Ch 7-pointers
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Pointer basics.pptx
Pointer basics.pptxPointer basics.pptx
Pointer basics.pptx
 
C tutorial
C tutorialC tutorial
C tutorial
 

More from Wingston

03 layouts & ui design - Android
03   layouts & ui design - Android03   layouts & ui design - Android
03 layouts & ui design - Android
Wingston
 
Linux – an introduction
Linux – an introductionLinux – an introduction
Linux – an introduction
Wingston
 
Introduction to the Arduino
Introduction to the ArduinoIntroduction to the Arduino
Introduction to the Arduino
Wingston
 
4.content mgmt
4.content mgmt4.content mgmt
4.content mgmt
Wingston
 
8 Web Practices for Drupal
8  Web Practices for Drupal8  Web Practices for Drupal
8 Web Practices for Drupal
Wingston
 
7 Theming in Drupal
7 Theming in Drupal7 Theming in Drupal
7 Theming in Drupal
Wingston
 
6 Special Howtos for Drupal
6 Special Howtos for Drupal6 Special Howtos for Drupal
6 Special Howtos for Drupal
Wingston
 

More from Wingston (20)

OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012
 
05 content providers - Android
05   content providers - Android05   content providers - Android
05 content providers - Android
 
04 activities - Android
04   activities - Android04   activities - Android
04 activities - Android
 
03 layouts & ui design - Android
03   layouts & ui design - Android03   layouts & ui design - Android
03 layouts & ui design - Android
 
02 hello world - Android
02   hello world - Android02   hello world - Android
02 hello world - Android
 
01 introduction & setup - Android
01   introduction & setup - Android01   introduction & setup - Android
01 introduction & setup - Android
 
OpenCV with android
OpenCV with androidOpenCV with android
OpenCV with android
 
C game programming - SDL
C game programming - SDLC game programming - SDL
C game programming - SDL
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Linux – an introduction
Linux – an introductionLinux – an introduction
Linux – an introduction
 
Embedded linux
Embedded linuxEmbedded linux
Embedded linux
 
04 Arduino Peripheral Interfacing
04   Arduino Peripheral Interfacing04   Arduino Peripheral Interfacing
04 Arduino Peripheral Interfacing
 
03 analogue anrduino fundamentals
03   analogue anrduino fundamentals03   analogue anrduino fundamentals
03 analogue anrduino fundamentals
 
02 General Purpose Input - Output on the Arduino
02   General Purpose Input -  Output on the Arduino02   General Purpose Input -  Output on the Arduino
02 General Purpose Input - Output on the Arduino
 
Introduction to the Arduino
Introduction to the ArduinoIntroduction to the Arduino
Introduction to the Arduino
 
4.content mgmt
4.content mgmt4.content mgmt
4.content mgmt
 
8 Web Practices for Drupal
8  Web Practices for Drupal8  Web Practices for Drupal
8 Web Practices for Drupal
 
7 Theming in Drupal
7 Theming in Drupal7 Theming in Drupal
7 Theming in Drupal
 
6 Special Howtos for Drupal
6 Special Howtos for Drupal6 Special Howtos for Drupal
6 Special Howtos for Drupal
 

Recently uploaded

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

C programming - Pointers

  • 1. Workshop India Wilson Wingston Sharon wingston.sharon@gmail.com
  • 2. #include <stdio.h> int main()  A variable is something with a { name. printf("size of a short is %dn", sizeof(short));  The value of that variable is not printf("size of a int is fixed. %dn", sizeof(int)); printf("size of a long is %dn", sizeof(long));  Size of the variable depends on the } datatype.  The program on the right will tell you the size of the variables in your system.
  • 3. int k;  What does the above statement mean?  Int means the compiler needs to set aside 4 bytes of memory to hold this particular value k.  This variable k is going to be stored in the computers RAM.  What is the value of k at this time?  k = 2;  Now the value 2 is going to be placed in the memory location for k.  So if we check the place in RAM where the variable is stored, we will find 2 there.
  • 4. Address | Value  RAM is the storage area for the x000101| 0 computer. x000102| 0 k x000103| 0  It remembers things as long as x000104| 2 the computer is on. x000105| Value x000106| Value  The RAM is like a register book. x000107| Value  Each address location is a byte.  Soif k begins at 0x000101, it continues till 0x000104.
  • 5.  If you take an = operator  Lvalue = Rvalue  These Values have their own rules.  Lvalue must be a named region of storage.  Rvalue must be an expression that returns some value.  k = 2;  j = 3;  k = j;  2 = k; makes no sense.
  • 6. k = 2;  Lvalue is variable K  Rvalue is 2  2 is stored in variable k k = j;  Lvalue is variable k  Rvalue is value stored in variable j  Value of variable j is now copied to k. 2 = k;  Really? Can this be possible?
  • 7. k k = 2; 2  Name is k 0x001  Value is 2  Starting address is 0x001 c ?  char c = „b‟; 0x00A  Value = ?  Address = ?
  • 8.  Pointers are variables who‟s values is the address of another memory location on the RAM.  Pointers are variables like any other data type.  The value they contain are special – they store an address.  int *ptr;  This statement informs the compiler that we want a variable balled ptr that will hold the address of another integer variable.  What does ptr at this point?
  • 9. k & is an operator that 2 returns the address of 0x001 the variable.  printf(“%d”, k ); c  printf(“%d”, &k ); „c‟ 0x00A  printf(“%c”, c );  printf(“%d”, &c );
  • 10.  intk; var k  k = 2; ? 2 0x00A 0x001  int *ptr;  ptr = &k; ptr ?  int *var = k; 0x00A
  • 11.  * called a deferefencer.  It is applicable only for a pointer data type and it goes to memory location of that pointer.  &k  Return the address of the variable k in RAM  Can only be Rvalue.  k  Return that value that k has in RAM  Can be both Rvalue and Lvalue  *k  Go to the memory address in k and lets work with that value.  Can be both Rvalue and Lvalue
  • 12.  printf(“%d”, k ); var k 0x001 2  printf(“%d”, ptr ); 0x00A 0x001  printf(“%d”, *var ); ptr 0x001  printf(“%d”, &var); 0x00A  printf(“%d”, *ptr);
  • 13.  printf(“%d”, k );  2 var k  printf(“%d”, ptr ); 0x001 2  0x001 0x00A 0x001  printf(“%d”, *var );  2 ptr  printf(“%d”, &var); 0x001  0x00A 0x00A  printf(“%d”, *ptr);  2
  • 14. A variable is declared by giving it a type and a name (e.g. int k;)  A pointer variable is declared by giving it a type and a name (e.g. int *ptr) where the asterisk tells the compiler that the variable named ptr is a pointer variable and the type tells the compiler what type the pointer is to point to (integer in this case).  Once a variable is declared, we can get its address by preceding its name with the unary &operator, as in &k.
  • 15.  Time: 15 minutes  Write 3 different programs that uses the concepts you just learnt  Familiarize yourselves with pointers.
  • 16.  If*ptr is a pointer pointing to a variable,  ptr + 1 doesn‟t simply increment the pointer by 1  The compiler changes the expression to  ptr + 1 * sizeof(<datatype>)  Ifptr was an integer of size()=4, then the next integer will be 4 memory blocks away.
  • 17.  int k = 2; k ptr 2  int *ptr = &k; ? 0x001  int *v1 = ptr + 2; 0x00A 7  int *v2 = v + 1; v1 0x005 29  printf(“%d”, *ptr ); ? 0x00B 0x001  printf(“%d”, *v1 ); -2  printf(“%d”, *v2 ); v2 0x001  printf(“%d”, v2 ); ?  printf(“%d”, &v2 ); 0x00C
  • 18. int k = 2; k  int *ptr = &k; ptr 2  int *v1 = ptr + 2;  int *v2 = v + 1; 0x001 0x001 0x01A 7  printf(“%d”, *ptr );  2 0x005 v1  printf(“%d”, *v1 );  29 0x009 29  printf(“%d”, *v2 ); 0x01E 0x009  -2  printf(“%d”, v2 ); -2 v2  0x00D 0x00D  printf(“%d”, &v2 ); 0x00D  0x03F 0x03F
  • 19. int array[] = {1,23,4,5,-100};  The variable is a pointer pointing to the first element of the array .  All other arrays are stored sequentially after that.  array[1] = ?  *(arr+1) = ?  *arr + 1 = ?  arr[3] = ?  *(arr+3) = ?  *arr + 3 = ?
  • 20. int array[] = {1,23,4,5,-100};  The variable is a pointer pointing to the first element of the array .  All other arrays are stored sequentially after that.  array[1] = 23  *(arr+1) = 23  *arr + 1 = 24  arr[3] = 5  *(arr+3) = 5  *arr + 3 = 8
  • 21.  int array[] = {1,23,7,5,10,78,9,87};  int *ptr = array + 2;  array[1] =?  *(ptr+1) = ?  *array + 3 = ?  array[4] = ?  *ptr + 1 = ?  &array[0] = ?
  • 22.  int array[] = {1,23,7,5,10,78,9,87};  int *ptr = array + 2;  array[1] = 23  *(ptr+1) = 5  *array + 3 = 5  array[4] = 10  *ptr + 1 = 8  &array[0] = array
  • 23.  Most people will say the name of an array is a pointer.  No – the name of an array is a constant pointer to the first element of the array.  i.e. int array[5];  Is array[1] an Lvalue or Rvalue?  Is array an Lvalue or Rvalue?  is *array an Lvalue or Rvalue?
  • 24.  int arr[] = {1,2,3,4,5,6,7};  arr[2] =?  *(arr + 2) = ?  *(2 + arr) = ?  2[arr] =?
  • 25.  int arr[] = {1,2,3,4,5,6,7};  arr[2] =3  *(arr + 2) = 3  *(2 + arr) = 3  2[arr] =3
  • 26.  The best teacher is yourself.  Make an array.  Decide what output you want.  Write code to check that output.  If not, call us and we‟ll help you.  Ifyou consistently get the output the same as what you predicted. Congratulations – you are learning!
  • 27.  Used for packaging variables.  Arrays are collection of lots of variables of same type under one name.  Structsare collections of a number of variables of different types under one name.  Structure definition and variable creation are different.
  • 28. struct person {  The following code makes a char *name; struct called person. int marks; float phonenum;  It defines a string pointer for person *sibling; the name, an integer field for }; marks  A float field for phone number  And a pointer to another person only to say that they‟re a sibling.
  • 29. struct person  Once the definition is set, we { char name[20]; make variables or objects based int marks; on this template. float phonenum; person *sibling;  Now “person” is a valid data type. };  struct person ram;  Creates an object called ram of type person.  Strcpy(ram.name, “Hello”);  ram.marks= “5”;  ram.phonenum = 4374389;  Is how you acces elements inside a structure
  • 30. struct person {  struct person ali = {“Ali”, 78, char name[20]; 893043, NULL}; int marks; float phonenum;  Creates new object of type person person *sibling; with the above values. }; struct person ali = {“Ali”,  Lookat mike definition. The 78, 893043, NULL}; person* field has been given &ali. struct person mike=  So mikes sibling field points to ali! {“mike”, 78, 893043, &ali};
  • 31. struct person  The . (dot) operator lets us { char name[20]; access the structure elements. int marks; float phonenum; person *sibling;  Ifyou are pointing to a structure }; you have to use. (->) struct person ali = {“Ali”, 78, 893043, NULL};  structperson *ptr = ali; struct person mike=  ptr->marks; {“mike”, 78, 893043,  ? &ali};  ptr->sibling;  ?
  • 32. struct person  The . (dot) operator lets us { char name[20]; access the structure elements. int marks; float phonenum; person *sibling;  Ifyou are pointing to a structure }; you have to use. (->) struct person ali = {“Ali”, 78, 893043, NULL};  structperson *ptr = ali; struct person mike=  ptr->marks; {“mike”, 78, 893043,  78 &ali};  ptr->sibling;  NULL
  • 33. struct person  we see the object mike of type { person. char name[20]; int marks;  Its sibling field has been set to point float phonenum; to ali. person *sibling; };  mike.name struct person ali = {“Ali”,  ? 78, 893043, NULL};  mike.sibling  ? struct person mike=  mike.sibling->name {“mike”, 78, 893043, &ali};  ?  Mike.sibling->sibling  ?
  • 34. struct person  we see the object mike of type { person. char name[20]; int marks;  Its sibling field has been set to point float phonenum; to ali. person *sibling; };  mike.name struct person ali = {“Ali”,  mike 78, 893043, NULL};  mike.sibling  //memory address of object ali struct person mike=  mike.sibling->name {“mike”, 78, 893043, &ali};  ali  Mike.sibling->sibling  NULL