SlideShare a Scribd company logo
Chapter 6 – POINTERS Pointer Pointers Declaration & Initialization Pointer Operators Calling Function using Pointers Array of Pointers Array of string
Objectives To have acquired the ability to design and manipulate pointer variables. To be able to design and use user-defined data types- structures
6.1  Pointers A variable has a name, an address, a type and a value Pointers are language constructs that allow programmers to directly manipulate the address of variables. Simulate call-by-reference Close relationship with arrays and strings
6.1.1 Declaration & Initialization Pointer variable Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain address of a variable that has a specific value (indirect reference) Indirection – referencing a pointer value
6.1.1 Declaration & Initialization(Cont..) Pointer declarations *  used with pointer variables int  * myPtr; Declares a pointer to an  int  (pointer of type  int  * ) Multiple pointers require using a  *   before each variable declaration int  * myPtr1,  * myPtr2; Can declare pointers to any data type Initialize pointers to  0 ,  NULL , or an address 0  or  NULL  – points to nothing ( NULL  preferred)
6.1.1 Declaration & Initialization (Cont..) Pointer Initializations 2 ways :- i) Assigning address of other variable ii) assigning value of other variable directly Method (i)   Example :  int val = 30;   int *value =&val; Method (ii)   Example : int *value ;   *value = 30;
6.1 2 Pointer Operators
6.1.2 Pointer Operators (Cont..)
6.1.2 Pointer Operators (Cont..) Short Notes : i) int *px;  px = pointer to an integer  ii)  int x;   x  is an integer iii) px = &x;  px gets the address of x or px point to x iv)  y = *px;  y gets the content of whatever    px points to
Example 1 int age, *ag;  …(i) age = 30;  ….(ii) ag =&age;  ….(iii) Situation in Computer Memory . After (i) age  1002 ag  1004 B)  After (ii) age  1002 ag  1004 ? ? 30 ? C)  After (iiI) age  1002 ag  1004 30 1002 Assume that the following codes are added after (iii). Predict the output values ? printf(“%d\n”, age); printf(“%d\n”, ag); printf(“%d\n”, *ag);
Example 2
Example (Cont..) Refer to Add Note : Ex. 1 & 2
6.1.3 Calling Function using Pointers Refer to Add Note : Ex. 3
6.1.4 Arrays of Pointers
Example char *name[5] = { { “Ahmad”},  {“Maria”},  {“Halim”},  {“Nora”}, {“Lina”} }; To print 1 st  String printf(“%s”, *name); To printf 2 nd  String printf(“%s”, (*name+1));
Example Program /* Example : Arrays of pointers*/ /*Increments a pointer through an integer array*/ #include<stdio.h> main( ) { int number[] = { 10,20,30,40,50}; int *p = number;  /* The pointers points to the start of the array*/ printf(“%d \n”, *p); p++; printf(“%d \n”, *p); p++; printf(“%d \n”, *p); p++; printf(“%d \n”, *p); p++; printf(“%d \n”, *p); }
Exercise Write a C statement to accomplish the following tasks. Assume that the floating point variable number1 has been declared. Define the variable fPtr to be a pointer to an object of type float. Assign the address of variable number1 to pointer variable fPtr. Print the value of the object pointed to by fPtr.
Exercise Write a C statement to accomplish the following tasks. Assume that the floating point variable number1 has been  declared. Define the variable fPtr to be a pointer to an object of type float. Assign the address of variable number1 to pointer variable fPtr. Print the value of the object pointed to by fPtr. Answers : float *fPtr; fptr = &number1; printf(“The value of object pointed is %d”, *fPtr);

More Related Content

PPT
Ch5 array nota
PPT
Ch7 structures
PPT
structure and union
PPT
Structures
PPT
Ch2 introduction to c
PPTX
C programming - Pointers
PDF
Resource wrappers in C++
PDF
Ch5 array nota
Ch7 structures
structure and union
Structures
Ch2 introduction to c
C programming - Pointers
Resource wrappers in C++

What's hot (20)

PPT
Advanced Programming C++
PPT
Pointer in C
PPTX
COM1407: Working with Pointers
PDF
C Prog - Pointers
PPT
pointers
PPT
Pointers in C
PPTX
PPT
C pointers
PPTX
Pointer in c
PPTX
Pointers in C Programming
PPTX
Pointer in C++
PDF
Pointers & References in C++
PPTX
Pointer in C
PPT
Pointers+(2)
PPS
C programming session 04
PPT
PPTX
C programming - Pointer and DMA
PDF
PPS
C programming session 02
Advanced Programming C++
Pointer in C
COM1407: Working with Pointers
C Prog - Pointers
pointers
Pointers in C
C pointers
Pointer in c
Pointers in C Programming
Pointer in C++
Pointers & References in C++
Pointer in C
Pointers+(2)
C programming session 04
C programming - Pointer and DMA
C programming session 02
Ad

Viewers also liked (20)

PPT
Ch3 repetition
PPT
PPT
Ch3 selection
PPT
Ch8 file processing
PPT
10 high speedla-ns
PPT
Ch4 functions
PPT
PPT
6 data linkcontrol
PPT
Ch1 principles of software development
PPT
8 spread spectrum
PPT
5 digital datacomm
PPT
12 wireless la-ns
DOCX
AM Receivers
PPT
11 circuit-packet
PPTX
Types of AM Receiver
PPT
Chapter 3 am receivers
PPT
4 signal encodingtechniques
PPT
7 multiplexing
PPT
Chapter 2 amplitude_modulation
PPT
Chapter 4 frequency modulation
Ch3 repetition
Ch3 selection
Ch8 file processing
10 high speedla-ns
Ch4 functions
6 data linkcontrol
Ch1 principles of software development
8 spread spectrum
5 digital datacomm
12 wireless la-ns
AM Receivers
11 circuit-packet
Types of AM Receiver
Chapter 3 am receivers
4 signal encodingtechniques
7 multiplexing
Chapter 2 amplitude_modulation
Chapter 4 frequency modulation
Ad

Similar to Ch6 pointers (latest) (20)

PPTX
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
PPTX
Pointers and Dynamic Memory Allocation
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
PPTX
Pointers in C Language
PPT
c program.ppt
PPT
presentation_pointers_1444076066_140676 (1).ppt
PDF
The best every notes on c language is here check it out
PPTX
Pointer.pptx
PDF
Pointers.pdf
DOCX
08-Pointers.docx An array is a linear data structure
PDF
Pointers are one of the core components of the C programming language.
PPTX
C Programming Unit-4
PPTX
4 Pointers.pptx
PPTX
Pointers in c - Mohammad Salman
PDF
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
PPTX
Algoritmos e Estruturas de Dados - Pointers
PPTX
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
PPT
Lecture 18 - Pointers
PPTX
Pointers
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Pointers and Dynamic Memory Allocation
UNIT 4 POINTERS.pptx pointers pptx for basic c language
Pointers in C Language
c program.ppt
presentation_pointers_1444076066_140676 (1).ppt
The best every notes on c language is here check it out
Pointer.pptx
Pointers.pdf
08-Pointers.docx An array is a linear data structure
Pointers are one of the core components of the C programming language.
C Programming Unit-4
4 Pointers.pptx
Pointers in c - Mohammad Salman
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
Algoritmos e Estruturas de Dados - Pointers
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
Lecture 18 - Pointers
Pointers
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...

More from Hattori Sidek (9)

PPT
3. transmission media
PPT
2[1].1 data transmission
PPT
14 congestionin datanetworks
PPT
01 pengenalan
PPT
01 berkenalan
PPT
Comm introduction
PPT
Chapter5 dek3133
PPT
Chapter 6 edit
PPT
Chapter 6 dc motor speed control
3. transmission media
2[1].1 data transmission
14 congestionin datanetworks
01 pengenalan
01 berkenalan
Comm introduction
Chapter5 dek3133
Chapter 6 edit
Chapter 6 dc motor speed control

Recently uploaded (20)

PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
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 Đ...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
master seminar digital applications in india
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Institutional Correction lecture only . . .
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
human mycosis Human fungal infections are called human mycosis..pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Abdominal Access Techniques with Prof. Dr. R K Mishra
Anesthesia in Laparoscopic Surgery in India
master seminar digital applications in india
Insiders guide to clinical Medicine.pdf
Institutional Correction lecture only . . .
O7-L3 Supply Chain Operations - ICLT Program
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Basic Mud Logging Guide for educational purpose
Microbial diseases, their pathogenesis and prophylaxis
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
01-Introduction-to-Information-Management.pdf
Supply Chain Operations Speaking Notes -ICLT Program

Ch6 pointers (latest)

  • 1. Chapter 6 – POINTERS Pointer Pointers Declaration & Initialization Pointer Operators Calling Function using Pointers Array of Pointers Array of string
  • 2. Objectives To have acquired the ability to design and manipulate pointer variables. To be able to design and use user-defined data types- structures
  • 3. 6.1 Pointers A variable has a name, an address, a type and a value Pointers are language constructs that allow programmers to directly manipulate the address of variables. Simulate call-by-reference Close relationship with arrays and strings
  • 4. 6.1.1 Declaration & Initialization Pointer variable Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain address of a variable that has a specific value (indirect reference) Indirection – referencing a pointer value
  • 5. 6.1.1 Declaration & Initialization(Cont..) Pointer declarations * used with pointer variables int * myPtr; Declares a pointer to an int (pointer of type int * ) Multiple pointers require using a * before each variable declaration int * myPtr1, * myPtr2; Can declare pointers to any data type Initialize pointers to 0 , NULL , or an address 0 or NULL – points to nothing ( NULL preferred)
  • 6. 6.1.1 Declaration & Initialization (Cont..) Pointer Initializations 2 ways :- i) Assigning address of other variable ii) assigning value of other variable directly Method (i) Example : int val = 30; int *value =&val; Method (ii) Example : int *value ; *value = 30;
  • 7. 6.1 2 Pointer Operators
  • 9. 6.1.2 Pointer Operators (Cont..) Short Notes : i) int *px; px = pointer to an integer ii) int x; x is an integer iii) px = &x; px gets the address of x or px point to x iv) y = *px; y gets the content of whatever px points to
  • 10. Example 1 int age, *ag; …(i) age = 30; ….(ii) ag =&age; ….(iii) Situation in Computer Memory . After (i) age 1002 ag 1004 B) After (ii) age 1002 ag 1004 ? ? 30 ? C) After (iiI) age 1002 ag 1004 30 1002 Assume that the following codes are added after (iii). Predict the output values ? printf(“%d\n”, age); printf(“%d\n”, ag); printf(“%d\n”, *ag);
  • 12. Example (Cont..) Refer to Add Note : Ex. 1 & 2
  • 13. 6.1.3 Calling Function using Pointers Refer to Add Note : Ex. 3
  • 14. 6.1.4 Arrays of Pointers
  • 15. Example char *name[5] = { { “Ahmad”}, {“Maria”}, {“Halim”}, {“Nora”}, {“Lina”} }; To print 1 st String printf(“%s”, *name); To printf 2 nd String printf(“%s”, (*name+1));
  • 16. Example Program /* Example : Arrays of pointers*/ /*Increments a pointer through an integer array*/ #include<stdio.h> main( ) { int number[] = { 10,20,30,40,50}; int *p = number; /* The pointers points to the start of the array*/ printf(“%d \n”, *p); p++; printf(“%d \n”, *p); p++; printf(“%d \n”, *p); p++; printf(“%d \n”, *p); p++; printf(“%d \n”, *p); }
  • 17. Exercise Write a C statement to accomplish the following tasks. Assume that the floating point variable number1 has been declared. Define the variable fPtr to be a pointer to an object of type float. Assign the address of variable number1 to pointer variable fPtr. Print the value of the object pointed to by fPtr.
  • 18. Exercise Write a C statement to accomplish the following tasks. Assume that the floating point variable number1 has been declared. Define the variable fPtr to be a pointer to an object of type float. Assign the address of variable number1 to pointer variable fPtr. Print the value of the object pointed to by fPtr. Answers : float *fPtr; fptr = &number1; printf(“The value of object pointed is %d”, *fPtr);