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

PDF
C Pointers
omukhtar
 
PPTX
Presentation on pointer.
Md. Afif Al Mamun
 
PPTX
Pointers in c++
sai tarlekar
 
PPT
Pointers in C
guestdc3f16
 
PPTX
Pointer in c program
Rumman Ansari
 
PPTX
Pointers in C Language
madan reddy
 
PPT
Structure in C
Fazle Rabbi Ador
 
PPTX
Structure & union
Rupesh Mishra
 
C Pointers
omukhtar
 
Presentation on pointer.
Md. Afif Al Mamun
 
Pointers in c++
sai tarlekar
 
Pointers in C
guestdc3f16
 
Pointer in c program
Rumman Ansari
 
Pointers in C Language
madan reddy
 
Structure in C
Fazle Rabbi Ador
 
Structure & union
Rupesh Mishra
 

What's hot (20)

PPTX
Function Pointer
Dr-Dipali Meher
 
PPT
pointers
teach4uin
 
PPTX
Pointers in C
Vijayananda Ratnam Ch
 
PPTX
Python comments and variables.pptx
adityakumawat625
 
PDF
Pointers in C
Monishkanungo
 
PPTX
Pointer in C
bipchulabmki
 
PPTX
Pointer in c
Imamul Kadir
 
PPTX
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Meghaj Mallick
 
PPTX
Pointers in c - Mohammad Salman
MohammadSalman129
 
PDF
Character Array and String
Tasnima Hamid
 
PPTX
Presentation on c structures
topu93
 
PPT
C++ Pointers And References
verisan
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPTX
Object Oriented Programming using C++(UNIT 1)
Dr. SURBHI SAROHA
 
PPTX
Array in c++
Mahesha Mano
 
PPTX
Union in C programming
Kamal Acharya
 
PPT
Pointers C programming
Appili Vamsi Krishna
 
PPTX
Dynamic memory allocation in c
lavanya marichamy
 
PPT
C++ Arrays
أحمد محمد
 
PPT
Pointers in C
Prabhu Govind
 
Function Pointer
Dr-Dipali Meher
 
pointers
teach4uin
 
Pointers in C
Vijayananda Ratnam Ch
 
Python comments and variables.pptx
adityakumawat625
 
Pointers in C
Monishkanungo
 
Pointer in C
bipchulabmki
 
Pointer in c
Imamul Kadir
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Meghaj Mallick
 
Pointers in c - Mohammad Salman
MohammadSalman129
 
Character Array and String
Tasnima Hamid
 
Presentation on c structures
topu93
 
C++ Pointers And References
verisan
 
Function in C program
Nurul Zakiah Zamri Tan
 
Object Oriented Programming using C++(UNIT 1)
Dr. SURBHI SAROHA
 
Array in c++
Mahesha Mano
 
Union in C programming
Kamal Acharya
 
Pointers C programming
Appili Vamsi Krishna
 
Dynamic memory allocation in c
lavanya marichamy
 
C++ Arrays
أحمد محمد
 
Pointers in C
Prabhu Govind
 
Ad

Viewers also liked (20)

PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PPT
Pointers in c
Mohd Arif
 
PDF
Pointers
sarith divakar
 
PPT
Unit 6 pointers
George Erfesoglou
 
PPT
Ponters
Mukund Trivedi
 
PPTX
C++ Pointers
Chaand Sheikh
 
PPTX
C pointer
University of Potsdam
 
PPT
C pointers
Aravind Mohan
 
PDF
C Prog - Pointers
vinay arora
 
PPTX
CSE240 Pointers
Garrett Gutierrez
 
PPT
More Pointers and Arrays
emartinez.romero
 
PPT
Pointers - DataStructures
Omair Imtiaz Ansari
 
PDF
Pointers in c
Saket Pathak
 
PDF
Arrays in C++
Ilio Catallo
 
PPT
Structure c
thirumalaikumar3
 
PPT
Pointers (Pp Tminimizer)
tech4us
 
PPTX
intro to pointer C++
Ahmed Farag
 
PPSX
C programming pointer
argusacademy
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Pointers in c
Mohd Arif
 
Pointers
sarith divakar
 
Unit 6 pointers
George Erfesoglou
 
C++ Pointers
Chaand Sheikh
 
C pointers
Aravind Mohan
 
C Prog - Pointers
vinay arora
 
CSE240 Pointers
Garrett Gutierrez
 
More Pointers and Arrays
emartinez.romero
 
Pointers - DataStructures
Omair Imtiaz Ansari
 
Pointers in c
Saket Pathak
 
Arrays in C++
Ilio Catallo
 
Structure c
thirumalaikumar3
 
Pointers (Pp Tminimizer)
tech4us
 
intro to pointer C++
Ahmed Farag
 
C programming pointer
argusacademy
 
Ad

Similar to C programming - Pointers (20)

PPTX
Pointers
Munazza-Mah-Jabeen
 
PDF
Pointers c imp
Neha Sharma
 
PDF
Pointers
shivdas kanade
 
PDF
Arrays and pointers
Kevin Nguyen
 
PDF
A TUTORIAL ON POINTERS AND ARRAYS IN C
Joshua Gorinson
 
PDF
Pointers In C
Sriram Raj
 
PDF
Pointers In C
Sriram Raj
 
PPT
l7-pointers.ppt
ShivamChaturvedi67
 
PDF
Chapter 13.1.8
patcha535
 
PPT
Session 5
Shailendra Mathur
 
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
PPT
pointers CP Lecture.ppt
EC42ShaikhAmaan
 
PPT
C tutorial
Anurag Sukhija
 
PPTX
C language
Rupanshi rawat
 
DOCX
C programming perso notes
Melanie Tsopze
 
PPTX
Lec 02 Introduction to C Programming.pptx
warriorbotscomp
 
PDF
07 -pointers_and_memory_alloc
Hector Garzo
 
PPT
C the basic concepts
Abhinav Vatsa
 
Pointers c imp
Neha Sharma
 
Pointers
shivdas kanade
 
Arrays and pointers
Kevin Nguyen
 
A TUTORIAL ON POINTERS AND ARRAYS IN C
Joshua Gorinson
 
Pointers In C
Sriram Raj
 
Pointers In C
Sriram Raj
 
l7-pointers.ppt
ShivamChaturvedi67
 
Chapter 13.1.8
patcha535
 
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
pointers CP Lecture.ppt
EC42ShaikhAmaan
 
C tutorial
Anurag Sukhija
 
C language
Rupanshi rawat
 
C programming perso notes
Melanie Tsopze
 
Lec 02 Introduction to C Programming.pptx
warriorbotscomp
 
07 -pointers_and_memory_alloc
Hector Garzo
 
C the basic concepts
Abhinav Vatsa
 

More from Wingston (20)

PPTX
OpenCV @ Droidcon 2012
Wingston
 
PPTX
05 content providers - Android
Wingston
 
PPTX
04 activities - Android
Wingston
 
PPTX
03 layouts & ui design - Android
Wingston
 
PPTX
02 hello world - Android
Wingston
 
PPTX
01 introduction & setup - Android
Wingston
 
PPTX
OpenCV with android
Wingston
 
PPTX
C game programming - SDL
Wingston
 
PPTX
Introduction to Basic C programming 02
Wingston
 
PPT
Introduction to Basic C programming 01
Wingston
 
PPTX
Linux – an introduction
Wingston
 
PPTX
Embedded linux
Wingston
 
PPTX
04 Arduino Peripheral Interfacing
Wingston
 
PPTX
03 analogue anrduino fundamentals
Wingston
 
PPTX
02 General Purpose Input - Output on the Arduino
Wingston
 
PPTX
Introduction to the Arduino
Wingston
 
PPTX
4.content mgmt
Wingston
 
PPTX
8 Web Practices for Drupal
Wingston
 
PPTX
7 Theming in Drupal
Wingston
 
PPTX
6 Special Howtos for Drupal
Wingston
 
OpenCV @ Droidcon 2012
Wingston
 
05 content providers - Android
Wingston
 
04 activities - Android
Wingston
 
03 layouts & ui design - Android
Wingston
 
02 hello world - Android
Wingston
 
01 introduction & setup - Android
Wingston
 
OpenCV with android
Wingston
 
C game programming - SDL
Wingston
 
Introduction to Basic C programming 02
Wingston
 
Introduction to Basic C programming 01
Wingston
 
Linux – an introduction
Wingston
 
Embedded linux
Wingston
 
04 Arduino Peripheral Interfacing
Wingston
 
03 analogue anrduino fundamentals
Wingston
 
02 General Purpose Input - Output on the Arduino
Wingston
 
Introduction to the Arduino
Wingston
 
4.content mgmt
Wingston
 
8 Web Practices for Drupal
Wingston
 
7 Theming in Drupal
Wingston
 
6 Special Howtos for Drupal
Wingston
 

Recently uploaded (20)

PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 

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