Pointers
Topics
 Pointer and Reason of using pointer
 Declaring and initializing pointer
 Calling function
Pointer
 Pointer: Pointer is a special type of variable
holding the address of other variable.
 Reasons of using pointers:
1. A pointer enables us to access a variable that is
defined outside the function.
2. Pointers are more efficient in handling data
tables.
3. Pointers reduce the length and complexity of a
program.
4. They increase the execution speed.
5. The use of pointer array to character strings
results in saving of data storage space in memory.
Declaring and Initializing Pointers
 The declaration of pointer variable:
data_type *pointer_name;
 Ex: int *p; declares the variable p as a pointer
variable that points to an integer data type.
 After declaring pointer variable, it can be made to
point to a variable using an assignment statement
and it is known as pointer initialization. Before
initialization, it should not use.
 Ex: p = &x;
 A pointer variable can be initialized in the time of
declaration.
int x, *p=&x;
Accessing a Variable through its Pointer
 Once a pointer has been assigned the
address of the variable, the value of the
variable is accessed by * (asterisk), usually
known as indirection operator.
 Ex: int x=200, *p,n;
p = &x;
n = *p;
*p returns the value of the variable x, because p is
the address of x. The * can be remembered as
‘value at address’.
Pointers and Arrays
 When an array is declared, the compiler allocates
a base address and sufficient amount of storage to
contain all the elements of the array in contiguous
memory locations. The base address is the
location of the first element of the array.
 Suppose we declare an array x as follows:
static int x[5]={20, 30, 50, 40,60};
If the base address is 1000 and p is an integer
pointer, then we can use p to point to the array x
by p=x;
We can access every value of x using p++ to move
from one element to another.
Pointers in one-dimensional array
 main()
{
int *p, sum=0, k=0;
static int x[5]={10,20,30,40,50};
p=x;
while(k<5)
{
sum=sum+*p;
p++;k++;
}
printf(“Summation is = %d”,sum);
}
Pointers and 2D Array
main()
{
int *p;
static int x[3][2]={1,2,3,4,5,6};
p=&x[0][0];
for(int i=0;i<3;i++)
for(int j=0;j<2;j++)
printf(“%d ”,*(p+3*i+j));
getch();
}
Pointers and Character strings
 Like one dimensional array, we can use pointer to
access the individual characters in a string.
 main()
{
char *name;
char *str=name;
name = “CSE”;
while(*str !=‘0’)
str++;
printf(“Length of the string:: %d”,str-name);
}
Continue…
 One important use of pointers is in handling
of a table of strings. Consider the following
array of strings:
char name[4][25]; //allocate 100 bytes
 We can make it a pointer to a string of
varying length. For example-
static char *name[4]={“CSE”, ”ECE”, ”URP”,
”ARCH”}; // 16 bytes
So, name[0] means CSE, name[2] means
URP and so one.
Pointers and Structures
 Consider the statement:
struct student
{
char name[20];
int roll;
float gpa;
}stu[10], *ptr;
 The assignement ptr = stu; would assign
the address of the zeroth element of stu to
ptr. That is, the pointer ptr will now point to
stu[0].
Continue…
 Its members can be accessed using the
statement:
ptr->name
ptr->roll
ptr->gpa
Pointers to Structure variables
struct student
{
char name[20];
int roll;
float gpa;
};
main()
{
struct student stu[5], *ptr;
for(int i=0;i<5;i++)
scanf(“%s%d%f”,stu[i].name, &stu[i].roll,&stu[i].gpa);
ptr = stu;
while(ptr<stu+5)
{
printf(“%s %d %.2f”,ptr->name,ptr->roll,ptr->gpa);
ptr++;
}
}

Chap 11(pointers)

  • 1.
    Pointers Topics  Pointer andReason of using pointer  Declaring and initializing pointer  Calling function
  • 2.
    Pointer  Pointer: Pointeris a special type of variable holding the address of other variable.  Reasons of using pointers: 1. A pointer enables us to access a variable that is defined outside the function. 2. Pointers are more efficient in handling data tables. 3. Pointers reduce the length and complexity of a program. 4. They increase the execution speed. 5. The use of pointer array to character strings results in saving of data storage space in memory.
  • 3.
    Declaring and InitializingPointers  The declaration of pointer variable: data_type *pointer_name;  Ex: int *p; declares the variable p as a pointer variable that points to an integer data type.  After declaring pointer variable, it can be made to point to a variable using an assignment statement and it is known as pointer initialization. Before initialization, it should not use.  Ex: p = &x;  A pointer variable can be initialized in the time of declaration. int x, *p=&x;
  • 4.
    Accessing a Variablethrough its Pointer  Once a pointer has been assigned the address of the variable, the value of the variable is accessed by * (asterisk), usually known as indirection operator.  Ex: int x=200, *p,n; p = &x; n = *p; *p returns the value of the variable x, because p is the address of x. The * can be remembered as ‘value at address’.
  • 5.
    Pointers and Arrays When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain all the elements of the array in contiguous memory locations. The base address is the location of the first element of the array.  Suppose we declare an array x as follows: static int x[5]={20, 30, 50, 40,60}; If the base address is 1000 and p is an integer pointer, then we can use p to point to the array x by p=x; We can access every value of x using p++ to move from one element to another.
  • 6.
    Pointers in one-dimensionalarray  main() { int *p, sum=0, k=0; static int x[5]={10,20,30,40,50}; p=x; while(k<5) { sum=sum+*p; p++;k++; } printf(“Summation is = %d”,sum); }
  • 7.
    Pointers and 2DArray main() { int *p; static int x[3][2]={1,2,3,4,5,6}; p=&x[0][0]; for(int i=0;i<3;i++) for(int j=0;j<2;j++) printf(“%d ”,*(p+3*i+j)); getch(); }
  • 8.
    Pointers and Characterstrings  Like one dimensional array, we can use pointer to access the individual characters in a string.  main() { char *name; char *str=name; name = “CSE”; while(*str !=‘0’) str++; printf(“Length of the string:: %d”,str-name); }
  • 9.
    Continue…  One importantuse of pointers is in handling of a table of strings. Consider the following array of strings: char name[4][25]; //allocate 100 bytes  We can make it a pointer to a string of varying length. For example- static char *name[4]={“CSE”, ”ECE”, ”URP”, ”ARCH”}; // 16 bytes So, name[0] means CSE, name[2] means URP and so one.
  • 10.
    Pointers and Structures Consider the statement: struct student { char name[20]; int roll; float gpa; }stu[10], *ptr;  The assignement ptr = stu; would assign the address of the zeroth element of stu to ptr. That is, the pointer ptr will now point to stu[0].
  • 11.
    Continue…  Its memberscan be accessed using the statement: ptr->name ptr->roll ptr->gpa
  • 12.
    Pointers to Structurevariables struct student { char name[20]; int roll; float gpa; }; main() { struct student stu[5], *ptr; for(int i=0;i<5;i++) scanf(“%s%d%f”,stu[i].name, &stu[i].roll,&stu[i].gpa); ptr = stu; while(ptr<stu+5) { printf(“%s %d %.2f”,ptr->name,ptr->roll,ptr->gpa); ptr++; } }