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

PPTX
Pointer in c
PPTX
Pointer in c
PPT
Memory allocation in c
PPTX
Pointer in c program
PPT
pointers
PPTX
Pointers in C Programming
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PPTX
Function in c
Pointer in c
Pointer in c
Memory allocation in c
Pointer in c program
pointers
Pointers in C Programming
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Function in c

What's hot (20)

PPTX
C Programming : Pointers and Strings
PPT
Pointers in c
PPTX
Pointers in c - Mohammad Salman
PPT
Pointers C programming
PPTX
Pointer in C
PPT
PPTX
Pointers in c language
PPTX
Unit 9. Structure and Unions
PDF
Pointers in C
PPTX
arrays and pointers
PPTX
Types of function call
PDF
Types of pointer in C
PPTX
PPTX
C Structures and Unions
PPTX
User defined functions
PPTX
STRINGS IN PYTHON
PPT
Structure in c
PPT
detailed information about Pointers in c language
PPT
Pointers in C
PPTX
Structure in C language
C Programming : Pointers and Strings
Pointers in c
Pointers in c - Mohammad Salman
Pointers C programming
Pointer in C
Pointers in c language
Unit 9. Structure and Unions
Pointers in C
arrays and pointers
Types of function call
Types of pointer in C
C Structures and Unions
User defined functions
STRINGS IN PYTHON
Structure in c
detailed information about Pointers in c language
Pointers in C
Structure in C language
Ad

Viewers also liked (20)

PDF
C Pointers
PDF
Pointers
PPT
Unit 6 pointers
PPTX
C++ Pointers
PPT
C pointers
PDF
C Prog - Pointers
PPTX
CSE240 Pointers
PPT
More Pointers and Arrays
PPT
Pointers - DataStructures
PDF
Pointers in c
PDF
Arrays in C++
PPT
Structure c
PPT
Pointers (Pp Tminimizer)
PPTX
intro to pointer C++
PPT
Structure in C
PPSX
C programming pointer
PPTX
Structure in C
PPT
structure and union
C Pointers
Pointers
Unit 6 pointers
C++ Pointers
C pointers
C Prog - Pointers
CSE240 Pointers
More Pointers and Arrays
Pointers - DataStructures
Pointers in c
Arrays in C++
Structure c
Pointers (Pp Tminimizer)
intro to pointer C++
Structure in C
C programming pointer
Structure in C
structure and union
Ad

Similar to C programming - Pointers (20)

PPTX
PDF
Pointers c imp
PDF
Pointers
PDF
Arrays and pointers
PDF
A TUTORIAL ON POINTERS AND ARRAYS IN C
PDF
Pointers In C
PDF
Pointers In C
PPT
l7-pointers.ppt
PDF
Chapter 13.1.8
PPT
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
PPT
pointers CP Lecture.ppt
PPT
C tutorial
PPTX
C language
DOCX
C programming perso notes
PPTX
Lec 02 Introduction to C Programming.pptx
PDF
07 -pointers_and_memory_alloc
PPT
C the basic concepts
Pointers c imp
Pointers
Arrays and pointers
A TUTORIAL ON POINTERS AND ARRAYS IN C
Pointers In C
Pointers In C
l7-pointers.ppt
Chapter 13.1.8
UNIT 4 POINTERS.pptx pointers pptx for basic c language
pointers CP Lecture.ppt
C tutorial
C language
C programming perso notes
Lec 02 Introduction to C Programming.pptx
07 -pointers_and_memory_alloc
C the basic concepts

More from Wingston (20)

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

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Business Ethics Teaching Materials for college
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Cell Structure & Organelles in detailed.
STATICS OF THE RIGID BODIES Hibbelers.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
TR - Agricultural Crops Production NC III.pdf
Pre independence Education in Inndia.pdf
Cardiovascular Pharmacology for pharmacy students.pptx
Open folder Downloads.pdf yes yes ges yes
Introduction and Scope of Bichemistry.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Business Ethics Teaching Materials for college
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...

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