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);

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 haveacquired the ability to design and manipulate pointer variables. To be able to design and use user-defined data types- structures
  • 3.
    6.1 PointersA 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 PointerOperators
  • 8.
  • 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 intage, *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);
  • 11.
  • 12.
    Example (Cont..) Referto Add Note : Ex. 1 & 2
  • 13.
    6.1.3 Calling Functionusing Pointers Refer to Add Note : Ex. 3
  • 14.
  • 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 aC 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 aC 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);