SlideShare a Scribd company logo
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 in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
 
Type conversion
Type  conversionType  conversion
Type conversion
PreethaPreetha5
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
Rumman Ansari
 
C pointer
C pointerC pointer
C pointer
University of Potsdam
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Vijayananda Ratnam Ch
 
Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)
Dr. SURBHI SAROHA
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
Mauryasuraj98
 
Pointer in c
Pointer in cPointer in c
Pointer in c
Imamul Kadir
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
Sharad Dubey
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
Abu Bakr Ramadan
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
Samiksha Pun
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
Neel Mungra
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
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
gourav kottawar
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
pointers
pointerspointers
pointers
teach4uin
 

Viewers also liked (20)

Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Pointers in c
Pointers in cPointers in c
Pointers in c
Mohd Arif
 
Pointers
PointersPointers
Pointers
sarith divakar
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
Ponters
PontersPonters
Ponters
Mukund Trivedi
 
Pointers in C
Pointers in CPointers in C
Pointers in C
guestdc3f16
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
Chaand Sheikh
 
C pointers
C pointersC pointers
C pointers
Aravind Mohan
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
vinay arora
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
Software Systems and Graphic Designs
 
CSE240 Pointers
CSE240 PointersCSE240 Pointers
CSE240 Pointers
Garrett Gutierrez
 
More Pointers and Arrays
More Pointers and ArraysMore Pointers and Arrays
More Pointers and Arrays
emartinez.romero
 
Pointers - DataStructures
Pointers - DataStructuresPointers - DataStructures
Pointers - DataStructures
Omair Imtiaz Ansari
 
Pointers in c
Pointers in cPointers in c
Pointers in c
Saket Pathak
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Ilio Catallo
 
Structure c
Structure cStructure c
Structure c
thirumalaikumar3
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
tech4us
 
intro to pointer C++
intro to  pointer C++intro to  pointer C++
intro to pointer C++
Ahmed Farag
 
Structure in C
Structure in CStructure in C
Structure in C
Fazle Rabbi Ador
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
argusacademy
 
Ad

Similar to C programming - Pointers (20)

Pointers
PointersPointers
Pointers
Munazza-Mah-Jabeen
 
Pointers
PointersPointers
Pointers
shivdas kanade
 
Pointers c imp
Pointers c impPointers c imp
Pointers c imp
Neha Sharma
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Epic Lanka Technologies Pvt Ltd
 
Arrays and pointers
Arrays and pointersArrays and pointers
Arrays and pointers
Kevin Nguyen
 
A TUTORIAL ON POINTERS AND ARRAYS IN C
A TUTORIAL ON POINTERS AND ARRAYS IN CA TUTORIAL ON POINTERS AND ARRAYS IN C
A TUTORIAL ON POINTERS AND ARRAYS IN C
Joshua Gorinson
 
Pointers In C
Pointers In CPointers In C
Pointers In C
Sriram Raj
 
Pointers In C
Pointers In CPointers In C
Pointers In C
Sriram Raj
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
ShivamChaturvedi67
 
Chapter 13.1.8
Chapter 13.1.8Chapter 13.1.8
Chapter 13.1.8
patcha535
 
Session 5
Session 5Session 5
Session 5
Shailendra Mathur
 
UNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c languageUNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
pointers CP Lecture.ppt
pointers CP Lecture.pptpointers CP Lecture.ppt
pointers CP Lecture.ppt
EC42ShaikhAmaan
 
C tutorial
C tutorialC tutorial
C tutorial
Anurag Sukhija
 
C language
C languageC language
C language
Rupanshi rawat
 
C programming perso notes
C programming perso notesC programming perso notes
C programming perso notes
Melanie Tsopze
 
07 -pointers_and_memory_alloc
07  -pointers_and_memory_alloc07  -pointers_and_memory_alloc
07 -pointers_and_memory_alloc
Hector Garzo
 
C tutorial
C tutorialC tutorial
C tutorial
Patruni Chidananda Sastry
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
Abhinav Vatsa
 
Pointers
PointersPointers
Pointers
Vardhil Patel
 
Ad

More from Wingston (20)

OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012
Wingston
 
05 content providers - Android
05   content providers - Android05   content providers - Android
05 content providers - Android
Wingston
 
04 activities - Android
04   activities - Android04   activities - Android
04 activities - Android
Wingston
 
03 layouts & ui design - Android
03   layouts & ui design - Android03   layouts & ui design - Android
03 layouts & ui design - Android
Wingston
 
02 hello world - Android
02   hello world - Android02   hello world - Android
02 hello world - Android
Wingston
 
01 introduction & setup - Android
01   introduction & setup - Android01   introduction & setup - Android
01 introduction & setup - Android
Wingston
 
OpenCV with android
OpenCV with androidOpenCV with android
OpenCV with android
Wingston
 
C game programming - SDL
C game programming - SDLC game programming - SDL
C game programming - SDL
Wingston
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 
Linux – an introduction
Linux – an introductionLinux – an introduction
Linux – an introduction
Wingston
 
Embedded linux
Embedded linuxEmbedded linux
Embedded linux
Wingston
 
04 Arduino Peripheral Interfacing
04   Arduino Peripheral Interfacing04   Arduino Peripheral Interfacing
04 Arduino Peripheral Interfacing
Wingston
 
03 analogue anrduino fundamentals
03   analogue anrduino fundamentals03   analogue anrduino fundamentals
03 analogue anrduino fundamentals
Wingston
 
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
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
 
OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012
Wingston
 
05 content providers - Android
05   content providers - Android05   content providers - Android
05 content providers - Android
Wingston
 
04 activities - Android
04   activities - Android04   activities - Android
04 activities - Android
Wingston
 
03 layouts & ui design - Android
03   layouts & ui design - Android03   layouts & ui design - Android
03 layouts & ui design - Android
Wingston
 
02 hello world - Android
02   hello world - Android02   hello world - Android
02 hello world - Android
Wingston
 
01 introduction & setup - Android
01   introduction & setup - Android01   introduction & setup - Android
01 introduction & setup - Android
Wingston
 
OpenCV with android
OpenCV with androidOpenCV with android
OpenCV with android
Wingston
 
C game programming - SDL
C game programming - SDLC game programming - SDL
C game programming - SDL
Wingston
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 
Linux – an introduction
Linux – an introductionLinux – an introduction
Linux – an introduction
Wingston
 
Embedded linux
Embedded linuxEmbedded linux
Embedded linux
Wingston
 
04 Arduino Peripheral Interfacing
04   Arduino Peripheral Interfacing04   Arduino Peripheral Interfacing
04 Arduino Peripheral Interfacing
Wingston
 
03 analogue anrduino fundamentals
03   analogue anrduino fundamentals03   analogue anrduino fundamentals
03 analogue anrduino fundamentals
Wingston
 
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
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
 

Recently uploaded (20)

"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ..."Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
Arshad Shaikh
 
Critical Thinking and Bias with Jibi Moses
Critical Thinking and Bias with Jibi MosesCritical Thinking and Bias with Jibi Moses
Critical Thinking and Bias with Jibi Moses
Excellence Foundation for South Sudan
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 
Multicultural approach in education - B.Ed
Multicultural approach in education - B.EdMulticultural approach in education - B.Ed
Multicultural approach in education - B.Ed
prathimagowda443
 
Order Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptxOrder Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptx
Arshad Shaikh
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Order: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptxOrder: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptx
Arshad Shaikh
 
State institute of educational technology
State institute of educational technologyState institute of educational technology
State institute of educational technology
vp5806484
 
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
Sritoma Majumder
 
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGYHUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
DHARMENDRA SAHU
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 
Introduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdfIntroduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdf
CME4Life
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18
Celine George
 
Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...
EduSkills OECD
 
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
Julián Jesús Pérez Fernández
 
A Brief Introduction About Jack Lutkus
A Brief Introduction About  Jack  LutkusA Brief Introduction About  Jack  Lutkus
A Brief Introduction About Jack Lutkus
Jack Lutkus
 
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT PatnaSwachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Quiz Club, Indian Institute of Technology, Patna
 
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo SlidesHow to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
Celine George
 
K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
 
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ..."Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
Arshad Shaikh
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 
Multicultural approach in education - B.Ed
Multicultural approach in education - B.EdMulticultural approach in education - B.Ed
Multicultural approach in education - B.Ed
prathimagowda443
 
Order Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptxOrder Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptx
Arshad Shaikh
 
Order: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptxOrder: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptx
Arshad Shaikh
 
State institute of educational technology
State institute of educational technologyState institute of educational technology
State institute of educational technology
vp5806484
 
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
Sritoma Majumder
 
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGYHUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
DHARMENDRA SAHU
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 
Introduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdfIntroduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdf
CME4Life
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18
Celine George
 
Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...
EduSkills OECD
 
A Brief Introduction About Jack Lutkus
A Brief Introduction About  Jack  LutkusA Brief Introduction About  Jack  Lutkus
A Brief Introduction About Jack Lutkus
Jack Lutkus
 
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo SlidesHow to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
Celine George
 
K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
 

C programming - Pointers

  • 1. Workshop India Wilson Wingston Sharon [email protected]
  • 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