0% found this document useful (0 votes)
62 views49 pages

Chapter - 11

This document discusses pointers in three chapters. Chapter 11 introduces pointers, explaining that they contain memory addresses and allow indirect referencing of values. It covers declaring and initializing pointer variables, and accessing variables through pointers. Chapter 11 also discusses pointer expressions, pointers and arrays, pointers as function arguments, and pointers with structures.

Uploaded by

Arya Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views49 pages

Chapter - 11

This document discusses pointers in three chapters. Chapter 11 introduces pointers, explaining that they contain memory addresses and allow indirect referencing of values. It covers declaring and initializing pointer variables, and accessing variables through pointers. Chapter 11 also discusses pointer expressions, pointers and arrays, pointers as function arguments, and pointers with structures.

Uploaded by

Arya Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

CE141: COMPUTER CONCEPTS & PROGRAMMING

Chapter – 11

Pointers

Devang Patel Institute of Advance Technology and Research


Objectives
 To be able to know the concepts of Pointers
 To be able to know how pointer variables are used in a
program
 To be able to explain the chain of pointers
 To be able to know the Pointer Expression
 To be able to describe Pointers and Arrays

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Introduction
 In this chapter, we will discuss
 Understanding Pointers

 Underlying Concepts of Pointers

 Accessing the concepts of a variable

 Declaring Pointer Variable

 Initialization of Pointer Variable

 Accessing a Variable through its Pointer

 Chain of Pointers

 Pointer Expression

 Pointer increments and scale factor

 Pointers and character string

 Array of Pointers

 Pointers as Function Arguments

 Function Returning Pointers

 Pointers to Function

 Pointers and Structures

 Trouble with Pointers


Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Understanding Pointers
 The computer memory is the sequential collection of
storage cells
 Each cell known as byte, has a number called address
associated with it.
 Address starts from zero. Last address depends on
memory size.
 Computer having 64K memory will have its last address
65,535.
 At the declaration time systems allocates, some
appropriate location to hold the value of the variable

Variables that hold memory addresses are called pointer variables.

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Introduction
 Pointers are variables that contain memory addresses as their
values.
 A variable name directly references a value.
 A pointer indirectly references a value. Referencing a value
through a pointer is called indirection.
 A pointer variable must be declared before it can be used.

Memory address: 1020 1024 1032

… … 100 … 1024 …
intege pointer
r
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Introduction

Memory Cell Address


0

. Variable: x
65,535
Value: 7
Address: 1000
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Underlying Concepts of Pointers
• Memory address within the computer
• We can not change them
• We can only use them to store data
values
Pointer
Pointers

Constants • Can’t save the value of memory


address directly
• Can only obtain the value
Pointer Values through the variable stored there
using the address operator (&)

• Once we have a pointer value, it


Pointer Variables can be stored into another
variable
• The variable that contains a
pointer value is called as pointer
variable.

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Accessing the concepts of a variable
 Actual location of a variable is system dependent

 & immediately proceeding a variable returns the address of the


variable. For example, p=&a.

 & operator can be used with simple variable or array element.

 Following are illegal use:


 &125 (Pointing at constants)

 int x[10];

&x (Pointing at array names)

 &(x+y) (Pointing at expression)

 If x is an array, then expression such as &x[0] and x[i+3]

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Accessing the concepts of a variable
#include<stdio.h>
#include<conio.h>
void main()
{
char a='A';
int b=5;
float c=10.2, d=11.544;
clrscr();

printf("a=%c &a=%u\n",a,&a);
printf("b=%d &b=%u\n",b,&b);
printf("c=%f &c=%u\n",c,&c);
printf("d=%f &d=%u\n",d,&d);

getch();
}
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Declaring Pointer Variable
 In c, variable must be declared.
data_type *pt_name;
This tells the compiler three things about the variable pt_name.
 * tells that pt_name is pointer variable
 pt_name needs memory location.

 pt_name points to a variable of type data-type.

For example, int *a; /* integer pointer */


 Declares the variable a as a pointer variable that points to an
integer datatype.

The type int refers to the data type of the variable


being pointed to by a and not the type of the value of
the pointer

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Declaring Pointer Variable- Pointer
declaration style
 Pointers variables are declared similarly as normal
variables except for the addition of the unary operator.

int* p; int *p;

Style-1 Style-2
int *p, x,*q; int x, *p, y;
x=10;
p=&x;
y=*p; /*Accessing x through p */
*p=20; /*Accessing 20 to x */

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Initialization of Pointer Variable
 Process of assigning the address of a variable to a pointer
variable is known as initialization

 Once pointer variable has been declared we can use the


assignment operator to initialize the variable

int quantity;

int *p; /*declaration*/

p=&quantity; /*initialization*/

or

int *p=&quantity

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Initialization of Pointer Variable
 Pointer variables always point to the corresponding type of data

 For example,

float a,b;

int x,*p;

p=&a; /*Wrong*/

b=*p;

Will result in erroneous output because we are trying to assign the address of
a float variable to an integer pointer

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Initialization of Pointer Variable
 We could define a pointer variable with an initial
value of NULL or 0.
int *p=NULL;
int *p=0;
 With the exception of NULL and 0, no other
constant value can be assigned to a pointer
variable.
 For example, following is wrong:
int *p=5360; /*absolute address */

– 12 :User-Defined
ChapterChapter – 11: Pointers’
Functions
Initialization of Pointer Variable- Pointer Flexibility
 Same Pointer to different data variables in different
statement.
int x,y,z,*p; X Y Z
………….
p=&x;
………….
p=&x;
…………. P
p=&z;
………….
 Different Pointers to point to the same data variable.
int x;
X Y Z
int *p1=&x;
int *p2=&x;
int *p3=&x;
……………….. P
………………..

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Accessing a variable through its
Pointer
 How to access the value of a variable using
pointer?
int q,*p, n; //Declaration of q, n and pointer p
q=10; //Initialization of q
p=&q // Initialization of the address of the p
n=*p; //returns the value of a variable of which
address is stored

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
void main()
{
int x,y;
int *p;
x=10;
p=&x;
y=*p;
clrscr();
printf("Value of x is %d\n\n", x);
printf("%d is stored at address %u\n",x, &x);
printf("%d is stored at address %u\n", *&x, &x);
printf("%d is stored at address %u\n", *p, p);
printf("%u is stored at address %u\n", p, &p);
printf("%d is stored at address %u\n", y, &y);
*p=25;
printf("\nNow x=%d\n", x);
getch();
}

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
x y p
Declaration
65524 65522 65520 Address

X=10 10
65524 65522 65520 Address
p=&x 10 65524
65524 65522 65520 Address

y=*p 10 10 65524

65524 65522 65520 Address

Pointer to x

*p=25 25 10 65524 Address


65524 65522 65520

Illustration of Pointer Assignments


Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Chain of Pointers
 Possible to make a pointer to another pointer, thus
creating a chain of pointer as shown
Address 2 Address 1 Value

p2 p1 Variable
 Pointer p2 contains the address of pointer variable
p1, which points to the location that contains the
desired value
 Declaration of Pointer to Pointer: int **p2

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
void main()
{
int x,y;
int *p,**z;
x=10;
p=&x;
y=*p;
z=&p;
clrscr();
printf("Value of x is %d\n\n",x);
printf("x=%d is stored at address &x=%u\n",x,&x);
printf("*&x=%d is stored at address &x=%u\n",*&x,&x);
printf("*p=%d is stored at address p=%u\n",*p,p);
printf("p=%u is stored at address &p=%u\n",p,&p);
printf("y=%d is stored at address &y=%u\n",y,&y);
*p=25;
printf("\nNow x=%d\n",x);
printf("z=%u is stored at address &z=%u\n **z=%u",z,&z,**z);
getch();
}
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointer Expressions

 Pointer variables can be used in expressions


 For example, p1 and p2 are declared and
initialized pointer variables, then
Y=*p1+*p2
X=(*p1)*(*p2)
p1*p2\\invalid

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointer Increments and Scale Factor

 Pointers can be incremented like

p1=p1+1 or p1++

 If p1 is an int pointer with initial value , say


2800,then after the operation p1=p1+1, the
value of p1 will 2802, not 2801

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
void main()
{
int x=10,y=5,sum,sum1;
int *p,*q;

p=&x;
q=&y;
clrscr();
sum=*p+*q;
printf("sum=%d",sum);
printf("\np=%u q=%u",p,q);
//invalid pointer addition sum1=p+q;
*p=*p+2;
++*q;
printf("\nx=%d *p=%d *q=%d\np=%u q=%u",x,*p,*q,p,q);
getch();
}

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Arrays
 When an array is declared, the compiler allocates a base
address and sufficient amount storage to contain all the
array in contiguous memory location.
 The base address is the location of the first element (index
0) of the array

X[0] X[1] X[2] X[3] X[4]

1 2 3 4 5

1000 1002 1004 1006 1008

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
void main()
{
int *p,sum=0,i=0;
int x[5]={1,2,3,4,5};
p=x;
clrscr();
printf("\tElement\tvalue\tAddress\n");
while(i<5)
{
printf("\tx[%d]\t%d\t%u\n",i,*p,p);
sum=sum+*p;
i++,p++;
}
printf("sum=%d\n",sum);
printf("&x[0]=%u\n",&x[0]);
printf("p=%u\n",p) ;
getch();
If we remove p++ from the
} while loop then following
output will be generated

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Arrays
 We can access every value of x using p++ to move
one element to another.
 The relationship between p and x is shown as:

 P=&x[0] (=65514)
 P+1=&x[1] (=65516)
 P+2=&x[2] (=65518)
 P+3=&x[3] (=65520)
 P+4=&x[4] (=65522)

 Address of element is calculated as:

Address of x[3]=65514+(3*2)=65520
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Arrays

 Pointers can be used to manipulate two-D


arrays as well

 In one-D array x, the expression


 *(x+i) or *(p+i)

 Two-D array can be represented by


 *(*(a+i)+j) or *(*(p+i)j)
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers in 2-D Array
Columns
0 1 2 3 4 5

0 P
1 P+1
2
Rows 3
4 4,0 4,3 P+4
5
6 P+6
*(P+4) *(P+4)+3
P Pointer to the First Row
p+i Pointer to the ith Row
*(p+i) Pointer to first element in the ith Row

*(p+i)+j Pointer to jth element in the ith Row


*(*(p+i)+j) Value stored in the cell (i,j)

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Character strings

 C provides alternative method to create


strings by using pointer variables of type
char.
 char *str=“good”

g o o d \0

str

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Character strings

 To print the content of the string1

char *string1;

pritntf(“%s”, string1); or

puts(string1);

 Although string1 is a pointer to the string, it is also the


name of the string.

 There for we do not need to use indirect operator *.

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Character strings
void main()
{
char *name="DELHI";
char *p=name;

clrscr();
printf("%s",name);

while(*p!='\0')
{
printf("\n%c is stored at %u\n",*p,p);
p++;
}
getch();
}
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Array of Pointers
 Important use of pointer: To handle table of String
 char name [3][25];
 Following is the table named “name” which contains 3
names, each with a maximum length of 25 characters
(including null character).
Name[0]
Name[1]
Name[2]

char *name[3]={
“New Zealand”, N E W Z E A L A N D \
0 Name[0]
“Australia”, A U S T R A L I A \
Name[1]
“India”
0

I N D I A \ Name[2]
} 0

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Array of Pointers
 The following statement would print out all three names

for(i=0;i<=2;i++)

printf(“%s \n”, name[i]);

 To access jth character in the ith name, we may write as

*(name[i]+j)

 The character arrays with the rows of varying length called


“Ragged Arrays”

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Array of Pointers

Difference between *p[3] and (*p)[3]

 Since * has a lower precedence than [], *p[3]


declares p as an array of 3 pointers

 (*p)[3] declares p as a pointer to an array of


3 elements

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers as Function Arguments

 The address should pass to the function as an argument

 The parameters receiving the address should be pointers

 The process of calling a function using pointers to pass the


address of variables is known as call by reference.

 The process of passing actual value of variables is known as call


by value.

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
int swap(int*,int*);
void main()
{
int x,y;
x=100;
y=200;
printf("Before Exchange: x=%d y=%d\n\n",x,y);
swap(&x,&y);
printf("After Exchange: x=%d y=%d\n\n",x,y);
getch();
}

int swap (int *a,int *b)


{
int t;
t=*a;
*a=*b;
*b=t;
}
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers as Function Arguments

Note:
 The function parameters are declared as pointers

 The dereferenced pointers are use in the function


body

 When the function is called, the addresses are


passed as actual arguments

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Function Returning Pointers

 Function can return multiple values through


pointer parameters

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
*large();
void main()
{
int x=10,y=5,*p;
clrscr();
p=large(&x,&y);
printf("%d",*p);

getch();
}
*large(int *a,int *b)
{
if(*a>*b)
return(a);
else
return(b);
}

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointer to Function
 A function, like a variable, has a type and address location in the memory.

 It is therefore, possible to declare a pointer to a function

 We can make a function pointer to point a specific function by simply assigning


the name of the of the function to the pointer

 For example
double mul (int , int);

double (*p1)();

p1=mul;

 To call mul, we may now use the pointer p1 with the list of parameters

(*p1)(x,y);/*Function call*/ is equivalent to

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Compatibility and Casting
int x;
char *p;
p=(char *) &x;

Casting

When the variable datatype and pointer variable data


type is different then casting is necessary

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Compatibility and Casting
Void pointer:

 It is generic pointer

 All the types can be assigned to void pointer without casting.

 For example

int x;

char y;

void *v;

v=&x;

v=&y;

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Structures
struct inventory

char name[30];

int number;

float price;

}product[2], ptr*;

product: Data object, array of two elements, each of the type strut inventory

ptr: pointer to data object of struct inventory

ptr=product ;\\assignment

Would assign the address of 0th element of product to ptr.

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Structures
 The pointer ptr will now point to product[0].

 Members can accessed by:


ptr->name
Selection Operator
ptr->number

ptr->price

Or (*ptr).number

for(ptr=product; ptr<product+2; p++)


printf(“%s %d %f \n”,ptr->name, ptr->number, ptr-
>price);

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Troubles with pointers
 Assigning values to uninitialized pointers
int *p, m=100;
*p=m; /*Error*/
 Assigning value to a pointer variable

int *p,m=100;
p=m; /*Error*/
 Not dereferencing a pointer when required

int *p,x=100;
p=&x;
printf(“%d”,p); /*Error*/
 Assigning the address of an uninitialized variable

int m,*p;
p=&m;
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Previous year questions
 Differentiate: Call by value and Call by
reference [5 marks]
 Write a program which adds two integer
numbers using pointers only [3 marks]
 State true/false: Pointers reduce the length
and complexity of a program [1 mark]
 What do you mean by pointer? Explain
advantage and disadvantage of pointer?

Chapter – 11 : Pointers’
Previous year questions

 Write the output of the code: [1 marks]


#include<stdio.h>
void main(){
int i=3;
int *j;
int **k;
j=&i;
k=&j;
printf(“%d”,*(&i));
printf(“%d”,**k);
}

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Previous year questions

 Check following C code. Find runtime error


if any. Also discuss advantages of pointer to
function. [4 marks]
int *function(){
int x=10;
return(&x);
}
void main(){
printf(“%d”,*(function()));
}

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Previous year questions

 Define pointer. How we can access any


variable through its pointer?[2 marks]
 Write a program using pointers to compute
the sum of all elements stored in an array of
size 5. [4 marks]
 State ture/false: Pointer is a variable that
stores the address of the another variable.
[1 mark]

Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions

You might also like