SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Downloaden Sie, um offline zu lesen
C             Programming
                       Language
               By:
    Yogendra Pal
yogendra@learnbywatch.com
  Dedicated to My mother and Father
t
                                                               y
         Keep your notebook with you.

Write important point and questions that comes in your mind

     Solve Mind band exercise.


                                                               C
                                       Rewind when not clear


              Ask Questions by call or SMS or by mail


Keep Watching Keep Learning

THIS IS POINTERS


                                   2
Pointer
• Pointer variable
  – Contains memory address as their values.
  – Normal variable contains a specific value.
  – Pointers contain address of a variable.

           name
            ptr                           name
                                            a

            •
          address
           1090
                                           9
                                         address
                                          1004


                            3
Pointer…
• Pointer definitions
  – * used with pointer variables
          int *ptr;
  – Defines a pointer to an int (pointer of type int *).
  – Can define pointers to any data type.
  – Initialize pointers to 0, NULL, or an address
     • 0 or NULL – points to nothing (NULL preferred)
• & (address operator)
  – Return address of operand
                             4
Pointer…
• Initialize a pointer variable with the address of
  a variable.
             int *ptr , x = 10;
             ptr = &x;
      ptr                                   x

      •
      1090
                                           10
                                           1000

 Data type of pointer variable & variable must be same.

                                  5
Pointer…
• * (indirection/dereferencing) operator
  – Returns the value of the corresponding memory
    address.
• *ptr and ptr are different variable and hence
  has different value.
  – ptr is the address of x (1000).
  – *ptr is the value at 1000 memory address.
* and & are inverse
                           6
*ptr
        ptr                     x

        •
       1090
                               9
                               10
                               1000


              &ptr       ptr

*ptr = 9;                        y

y = *ptr;                       9
                               1098



                     7
Pointer Operators…
Operators                                     Associativity        Type
()    []                                     left to right    highest
+     -     ++   --   !             (type)   right to left    unary
                           *    &
*     /     %                                left to right    multiplicative
+     -                                      left to right    additive
<     <=    >    >=                          left to right    relational
==    !=                                     left to right    equality
&&                                           left to right    logical and
||                                           left to right    logical or
?:                                           right to left    conditional
=     +=    -=   *=   /=   %=                right to left    assignment
,                                            left to right    Comma

                                         8
Pointer and Function
• Call by value
     – Pass the value of the variable to the function.
void main()                 void print(int x)
{                           {
                                                    10
  int x = 10;                 x += 10;
  print(x);                   printf(“%d”,x);
  printf(“n%d”,x);         }
                                                         20
                                                         10
}
20
10

                               9
Pointer and Function
• Do not pass a variable.
         print(x);
• Pass address of variable using & operator.
         print(&x);
• Allows you to change the value directly at memory
  location.
• Arrays are not passed with & because the array name
  is already a pointer.


                            10
Pointer and Function
• An address can handle by a pointer only.
• Use * operator for variable inside of function.
        print(int *x);
• *x is used as an alias for the variable x.




                             11
Pointer and Function
• Call by reference
     – Pass the address of the variable to the function.
void main()                 void print(int *y)
{                           {
                                                   20
                                                   10
  int x = 10;                 *y += 10;
  print(&x);                  printf(“%d”,x);
  printf(“n%d”,x);         }
                                                        &x
}
20
20

                               12
Pointer Arithmetic
• ++ or -- : Increment or Decrement
• +, +=,-, -= : Add or subtract an integer from pointer.
• Operations meaningless unless performed on
  an array.
  int a[5]    a       a+1    a+2        a+3    a+4

             1000    1004   1008        1012   1016



                                   13
Pointer Arithmetic
• Subtracting pointers
  – Returns number of elements.
         int x[10], *a, *b;
         a = &x[4];
         b = &x[2];
         printf(“%d”,a-b);

• Pointer comparison ( <, == , > )
  – Check which pointer points to the higher numbered
    array element.

                              14
Pointer and Array
• Array name is a pointer to the first element of
  the array.
         int a[10];


    a          0      1   2   3    4   5   6   7   8   9

• a = &a[0];
• a[0] = *a;

                              15
Pointer and Array
       0   1    2   3   4        5   6   7   8   9


    a a+1a+2a+3a+4a+5a+6a+7 a+8 a+9
• Using + we can access the next element or the
  next memory address.
• Use * operator to access the value.
  – *(a+2) = a[2]
  – a[i] = *(a+i)

                            16
Pass Array to a Function
• Pass name and size of the array to the
  function.
• Problem: write a function (printa()) that print
  all elements of an array.
• Write a function (scana()) that scan all
  elements of an array.
• Put above functions in a file named “astdio.h”
  & create main function in other file.
                        17
Character Pointer
• A character pointer points to a character
  variable.
       char *ptr;
       ptr = “hello”;


                                         h e l
                                         l o 0

                        18
Arrays of Pointers
• Arrays can contain pointers
• For example: an array of strings
   char *name[3] = { “rajan”, “sonu”, “hari”};
• name array has a fixed size, but strings can be
  of any size.
       name[0]    •              ‘r’   ‘a’   ‘j’   ‘a’   ‘n’    ‘0’
       name[1]
                  •              ‘s’   ‘o’   ‘n’   ‘u’   ‘0’
       name[2]
                  •              ‘h’   ‘a’   ‘r’   ‘i’   ‘0’


                            19
Pointer to Pointers
• A pointer that point to another pointer.



• Use to read:
  – Character array.
  – Two dimensional array.



                             20
Pointer to Pointers
• Declaration
         char **reader;

• This can be used to read a character array.
         char *name[3] = {“sonu”, “raju”, “hari”};
         reader = name;




                              21
Two Dimensional Array
•   name[5][6];
•   name is the address of first element.
•   We can say it array of pointers.
•   name[0] points to first row.
•   name[1] points to second row.
•   name[0] is equivalent to name.
•   name[1] is equivalent to (name+1).
•   ith element can be access by *(name + i).
                          22
Two Dimensional Array
•   To access the jth element of ith row.
•   name [i][j]
•   *(name[i] + j)
•   *(*(name + i) + j)




                            23
Mind Bend
• Write a program which performs the following
  tasks:
  – Initialize an integer array of 10 elements in main().
  – Pass the entire array to a function modify().
  – In modify() multiply each elements of array by 3.
  – Return the control to main() and print the new array
    elements in main().



                           24
To get complete benefit of this tutorial solve all the quiz on
                       www.learnbywatch.com

              For any problem in this tutorial mail me at
                    yogendra@learnbywatch.com
                        with the subject “C”

                     For Other information mail at
                       info@learnbywatch.com


Keep Watching Keep Learning

NEXT IS STRUCTURES

                                    25

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
C pointer
C pointerC pointer
C pointer
 
C pointers
C pointersC pointers
C pointers
 
Pointers
PointersPointers
Pointers
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
ppt on pointers
ppt on pointersppt on pointers
ppt on pointers
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
C programming - Pointer and DMA
C programming - Pointer and DMAC programming - Pointer and DMA
C programming - Pointer and DMA
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
 

Ähnlich wie Pointer (20)

l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
oop Lecture 17
oop Lecture 17oop Lecture 17
oop Lecture 17
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
Pointers
PointersPointers
Pointers
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
Pointers
PointersPointers
Pointers
 
Pointers.pdf
Pointers.pdfPointers.pdf
Pointers.pdf
 
Pointers
PointersPointers
Pointers
 
Pointers
PointersPointers
Pointers
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Programming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explainedProgramming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explained
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
C Programming - Refresher - Part III
C Programming - Refresher - Part IIIC Programming - Refresher - Part III
C Programming - Refresher - Part III
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
20.C++Pointer.pptx
20.C++Pointer.pptx20.C++Pointer.pptx
20.C++Pointer.pptx
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
 

Mehr von Learn By Watch

Demodulation of fm pll detector
Demodulation of fm pll detectorDemodulation of fm pll detector
Demodulation of fm pll detectorLearn By Watch
 
Demodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorDemodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorLearn By Watch
 
In direct method of fm generation armstrong method
In direct method of fm generation armstrong methodIn direct method of fm generation armstrong method
In direct method of fm generation armstrong methodLearn By Watch
 
Direct method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodDirect method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodLearn By Watch
 
Spectrum and power of wbfm
Spectrum and power of wbfmSpectrum and power of wbfm
Spectrum and power of wbfmLearn By Watch
 
Narrow band frequency modulation nbfm
Narrow band frequency modulation nbfmNarrow band frequency modulation nbfm
Narrow band frequency modulation nbfmLearn By Watch
 
General expression of fm signal
General expression of fm signalGeneral expression of fm signal
General expression of fm signalLearn By Watch
 
Frequency division multiplexing
Frequency division multiplexingFrequency division multiplexing
Frequency division multiplexingLearn By Watch
 
Demodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorDemodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorLearn By Watch
 
Generarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodGenerarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodLearn By Watch
 
Generarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodGenerarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodLearn By Watch
 
Demodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverDemodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverLearn By Watch
 
Quadrature carrier multiplexing qam
Quadrature carrier multiplexing qamQuadrature carrier multiplexing qam
Quadrature carrier multiplexing qamLearn By Watch
 
Demodulation of am synchronous detector
Demodulation of am synchronous detectorDemodulation of am synchronous detector
Demodulation of am synchronous detectorLearn By Watch
 

Mehr von Learn By Watch (20)

Tutorial 9 fm
Tutorial 9 fmTutorial 9 fm
Tutorial 9 fm
 
Phase modulation
Phase modulationPhase modulation
Phase modulation
 
Demodulation of fm pll detector
Demodulation of fm pll detectorDemodulation of fm pll detector
Demodulation of fm pll detector
 
Demodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorDemodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detector
 
In direct method of fm generation armstrong method
In direct method of fm generation armstrong methodIn direct method of fm generation armstrong method
In direct method of fm generation armstrong method
 
Direct method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodDirect method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator method
 
Carson's rule
Carson's ruleCarson's rule
Carson's rule
 
Spectrum and power of wbfm
Spectrum and power of wbfmSpectrum and power of wbfm
Spectrum and power of wbfm
 
Narrow band frequency modulation nbfm
Narrow band frequency modulation nbfmNarrow band frequency modulation nbfm
Narrow band frequency modulation nbfm
 
General expression of fm signal
General expression of fm signalGeneral expression of fm signal
General expression of fm signal
 
Angle modulation
Angle modulationAngle modulation
Angle modulation
 
Frequency division multiplexing
Frequency division multiplexingFrequency division multiplexing
Frequency division multiplexing
 
Vsb modulation
Vsb modulationVsb modulation
Vsb modulation
 
Demodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorDemodulation of ssb synchronous detector
Demodulation of ssb synchronous detector
 
Generarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodGenerarion of ssb phase discrimination method
Generarion of ssb phase discrimination method
 
Generarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodGenerarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination method
 
Ssb modulation
Ssb modulationSsb modulation
Ssb modulation
 
Demodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverDemodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiver
 
Quadrature carrier multiplexing qam
Quadrature carrier multiplexing qamQuadrature carrier multiplexing qam
Quadrature carrier multiplexing qam
 
Demodulation of am synchronous detector
Demodulation of am synchronous detectorDemodulation of am synchronous detector
Demodulation of am synchronous detector
 

Pointer

  • 1. C Programming Language By: Yogendra Pal yogendra@learnbywatch.com Dedicated to My mother and Father
  • 2. t y Keep your notebook with you. Write important point and questions that comes in your mind Solve Mind band exercise. C Rewind when not clear Ask Questions by call or SMS or by mail Keep Watching Keep Learning THIS IS POINTERS 2
  • 3. Pointer • Pointer variable – Contains memory address as their values. – Normal variable contains a specific value. – Pointers contain address of a variable. name ptr name a • address 1090 9 address 1004 3
  • 4. Pointer… • Pointer definitions – * used with pointer variables int *ptr; – Defines a pointer to an int (pointer of type int *). – Can define pointers to any data type. – Initialize pointers to 0, NULL, or an address • 0 or NULL – points to nothing (NULL preferred) • & (address operator) – Return address of operand 4
  • 5. Pointer… • Initialize a pointer variable with the address of a variable. int *ptr , x = 10; ptr = &x; ptr x • 1090 10 1000 Data type of pointer variable & variable must be same. 5
  • 6. Pointer… • * (indirection/dereferencing) operator – Returns the value of the corresponding memory address. • *ptr and ptr are different variable and hence has different value. – ptr is the address of x (1000). – *ptr is the value at 1000 memory address. * and & are inverse 6
  • 7. *ptr ptr x • 1090 9 10 1000 &ptr ptr *ptr = 9; y y = *ptr; 9 1098 7
  • 8. Pointer Operators… Operators Associativity Type () [] left to right highest + - ++ -- ! (type) right to left unary * & * / % left to right multiplicative + - left to right additive < <= > >= left to right relational == != left to right equality && left to right logical and || left to right logical or ?: right to left conditional = += -= *= /= %= right to left assignment , left to right Comma 8
  • 9. Pointer and Function • Call by value – Pass the value of the variable to the function. void main() void print(int x) { { 10 int x = 10; x += 10; print(x); printf(“%d”,x); printf(“n%d”,x); } 20 10 } 20 10 9
  • 10. Pointer and Function • Do not pass a variable. print(x); • Pass address of variable using & operator. print(&x); • Allows you to change the value directly at memory location. • Arrays are not passed with & because the array name is already a pointer. 10
  • 11. Pointer and Function • An address can handle by a pointer only. • Use * operator for variable inside of function. print(int *x); • *x is used as an alias for the variable x. 11
  • 12. Pointer and Function • Call by reference – Pass the address of the variable to the function. void main() void print(int *y) { { 20 10 int x = 10; *y += 10; print(&x); printf(“%d”,x); printf(“n%d”,x); } &x } 20 20 12
  • 13. Pointer Arithmetic • ++ or -- : Increment or Decrement • +, +=,-, -= : Add or subtract an integer from pointer. • Operations meaningless unless performed on an array. int a[5] a a+1 a+2 a+3 a+4 1000 1004 1008 1012 1016 13
  • 14. Pointer Arithmetic • Subtracting pointers – Returns number of elements. int x[10], *a, *b; a = &x[4]; b = &x[2]; printf(“%d”,a-b); • Pointer comparison ( <, == , > ) – Check which pointer points to the higher numbered array element. 14
  • 15. Pointer and Array • Array name is a pointer to the first element of the array. int a[10]; a 0 1 2 3 4 5 6 7 8 9 • a = &a[0]; • a[0] = *a; 15
  • 16. Pointer and Array 0 1 2 3 4 5 6 7 8 9 a a+1a+2a+3a+4a+5a+6a+7 a+8 a+9 • Using + we can access the next element or the next memory address. • Use * operator to access the value. – *(a+2) = a[2] – a[i] = *(a+i) 16
  • 17. Pass Array to a Function • Pass name and size of the array to the function. • Problem: write a function (printa()) that print all elements of an array. • Write a function (scana()) that scan all elements of an array. • Put above functions in a file named “astdio.h” & create main function in other file. 17
  • 18. Character Pointer • A character pointer points to a character variable. char *ptr; ptr = “hello”; h e l l o 0 18
  • 19. Arrays of Pointers • Arrays can contain pointers • For example: an array of strings char *name[3] = { “rajan”, “sonu”, “hari”}; • name array has a fixed size, but strings can be of any size. name[0] • ‘r’ ‘a’ ‘j’ ‘a’ ‘n’ ‘0’ name[1] • ‘s’ ‘o’ ‘n’ ‘u’ ‘0’ name[2] • ‘h’ ‘a’ ‘r’ ‘i’ ‘0’ 19
  • 20. Pointer to Pointers • A pointer that point to another pointer. • Use to read: – Character array. – Two dimensional array. 20
  • 21. Pointer to Pointers • Declaration char **reader; • This can be used to read a character array. char *name[3] = {“sonu”, “raju”, “hari”}; reader = name; 21
  • 22. Two Dimensional Array • name[5][6]; • name is the address of first element. • We can say it array of pointers. • name[0] points to first row. • name[1] points to second row. • name[0] is equivalent to name. • name[1] is equivalent to (name+1). • ith element can be access by *(name + i). 22
  • 23. Two Dimensional Array • To access the jth element of ith row. • name [i][j] • *(name[i] + j) • *(*(name + i) + j) 23
  • 24. Mind Bend • Write a program which performs the following tasks: – Initialize an integer array of 10 elements in main(). – Pass the entire array to a function modify(). – In modify() multiply each elements of array by 3. – Return the control to main() and print the new array elements in main(). 24
  • 25. To get complete benefit of this tutorial solve all the quiz on www.learnbywatch.com For any problem in this tutorial mail me at yogendra@learnbywatch.com with the subject “C” For Other information mail at info@learnbywatch.com Keep Watching Keep Learning NEXT IS STRUCTURES 25