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 (20)

Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
C Pointers
C PointersC Pointers
C Pointers
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C pointer
C pointerC pointer
C pointer
 
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...
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
C pointers
C pointersC pointers
C pointers
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Computer Science:Pointers in C
Computer Science:Pointers in CComputer Science:Pointers in C
Computer Science:Pointers in C
 
Pointers In C
Pointers In CPointers In C
Pointers In C
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
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
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 

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
 
structure and union
structure and unionstructure and union
structure and union
 

Similar to C programming - Pointers

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

OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012Wingston
 
05 content providers - Android
05   content providers - Android05   content providers - Android
05 content providers - AndroidWingston
 
04 activities - Android
04   activities - Android04   activities - Android
04 activities - AndroidWingston
 
03 layouts & ui design - Android
03   layouts & ui design - Android03   layouts & ui design - Android
03 layouts & ui design - AndroidWingston
 
02 hello world - Android
02   hello world - Android02   hello world - Android
02 hello world - AndroidWingston
 
01 introduction & setup - Android
01   introduction & setup - Android01   introduction & setup - Android
01 introduction & setup - AndroidWingston
 
OpenCV with android
OpenCV with androidOpenCV with android
OpenCV with androidWingston
 
C game programming - SDL
C game programming - SDLC game programming - SDL
C game programming - SDLWingston
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01Wingston
 
Linux – an introduction
Linux – an introductionLinux – an introduction
Linux – an introductionWingston
 
Embedded linux
Embedded linuxEmbedded linux
Embedded linuxWingston
 
04 Arduino Peripheral Interfacing
04   Arduino Peripheral Interfacing04   Arduino Peripheral Interfacing
04 Arduino Peripheral InterfacingWingston
 
03 analogue anrduino fundamentals
03   analogue anrduino fundamentals03   analogue anrduino fundamentals
03 analogue anrduino fundamentalsWingston
 
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 ArduinoWingston
 
Introduction to the Arduino
Introduction to the ArduinoIntroduction to the Arduino
Introduction to the ArduinoWingston
 
4.content mgmt
4.content mgmt4.content mgmt
4.content mgmtWingston
 
8 Web Practices for Drupal
8  Web Practices for Drupal8  Web Practices for Drupal
8 Web Practices for DrupalWingston
 
7 Theming in Drupal
7 Theming in Drupal7 Theming in Drupal
7 Theming in DrupalWingston
 
6 Special Howtos for Drupal
6 Special Howtos for Drupal6 Special Howtos for Drupal
6 Special Howtos for DrupalWingston
 

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

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
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 Delhikauryashika82
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 

Recently uploaded (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
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
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 

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