Chap 11(pointers)

12
Pointers Topics Pointer and Reason of using pointer Declaring and initializing pointer Calling function

Transcript of Chap 11(pointers)

Page 1: Chap 11(pointers)

PointersTopics Pointer and Reason of using pointer Declaring and initializing pointer Calling function

Page 2: Chap 11(pointers)

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.

Page 3: Chap 11(pointers)

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;

Page 4: Chap 11(pointers)

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’.

Page 5: Chap 11(pointers)

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.

Page 6: Chap 11(pointers)

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

Page 7: Chap 11(pointers)

Pointers and 2D Arraymain(){

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

Page 8: Chap 11(pointers)

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

}

Page 9: Chap 11(pointers)

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.

Page 10: Chap 11(pointers)

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].

Page 11: Chap 11(pointers)

Continue… Its members can be accessed using the

statement:

ptr->name

ptr->roll

ptr->gpa

Page 12: Chap 11(pointers)

Pointers to Structure variablesstruct 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++; }}