0% found this document useful (0 votes)
73 views

Class 4 Notes

The document provides an introduction to pointers in C including declaring pointer variables, dereferencing pointers using the * operator, passing pointers to functions, pointer arithmetic, and pointers to arrays. Key concepts covered are that a pointer stores the address of another variable, pointers allow access and modification of variable values indirectly, and arrays can be treated similarly to pointers by name of the array being the address of the first element.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Class 4 Notes

The document provides an introduction to pointers in C including declaring pointer variables, dereferencing pointers using the * operator, passing pointers to functions, pointer arithmetic, and pointers to arrays. Key concepts covered are that a pointer stores the address of another variable, pointers allow access and modification of variable values indirectly, and arrays can be treated similarly to pointers by name of the array being the address of the first element.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Pointers : Introduction (Basic concepts), Pointers as arguments to

functions, pointer to arrays, pointer-to-pointer

A pointer is a variable which is used to store the address of another


variable. Like any variable or constant, you must declare a pointer before
using it, to store any variable address. The general form of a pointer
variable declaration is –

int *ip; /* pointer to an integer */


double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */

The above are the few examples of pointer declarations. If you need a
pointer to store the address of integer variable then the data type of
the pointer should be int. Same case is with the other data types.
& operator is also known as “Address of” Operator.

printf("Address of var is: %u", &num);

Point to note: %u or %p is used for printing variable’s address. Above


printf function display the address of a variable num but how can you
store that address in some other variable? That’s where pointer is used.

Pointer is just like another variable, the main difference is that it


stores address of another variable rather than a value.

* Operator – “Value at Address” Operator


* Operator is also known as indirection operator.

Lets take the same example with a pointer variable –


int main()
{
/*Pointer of integer type*/
int *p
int var = 10;
/*Assigning the variable address to pointer*/
p= &var;

printf("Value of var is: %d", var);


printf("Address of var is: %u", p);
return 0;
}
#include <stdio.h>

int main ()
{

int var = 20; /* actual variable declaration */


int *ip; /* pointer variable declaration */

ip = &var; /* store address of var in pointer variable*/

printf("Address of var variable: %p\n", &var );

/* address stored in pointer variable */


printf("Address stored in ip variable: %p\n", ip );

/* access the value using the pointer */


printf("Value of var variable: %d\n", *ip ); // display var value i.e. is 20

return 0;
}
Let’s assume that we need to add 1 to the variable a. We can do this with
any of the following statements, assuming that the pointer p is initialized
p=&a,

a++; a=a+1; *p=*p+1; (*p)++;

Accessing one variable with multiple pointers :

#include <stdio.h>

int main () {

int var = 20; /* actual variable declaration */


int *p,*q,*r; /* pointer variable declaration */

p=&a;
q=&a;
r=&a;

printf("a values with p %d\n", *p );


printf("a values with q %d\n", *q );
printf("a values with r %d\n", *r );

return 0;
}
Accessing multiple variable values with one pointer:

#include <stdio.h>

int main () {

int a = 5,b=10; /* actual variable declaration */


int *p; /* pointer variable declaration */

p=&a;
printf("a values with p %d\n", *p );
p=&b;
printf("b values with p %d\n", *p );
return 0;
}
Pointers as arguments to functions

We can pass data to the called function and we also can pass
address(pointers). To pass addresses the formal parameters in the called
function are defined as a pointer to variables.

Following is the program which changes value in the function using


pointer passing through the function

void chng(int *a)


{
rturn(*a+1);
}
void main()
{
int b=5;
printf(“\n b value before function call is %d”,b); // b value 5
chng(&b);
printf(“\n b value after function call is %d”,b); // ba value is 6
}
call by value and call by reference

Call by value essentially means that a copy of the variable is passed into
the function. The function does not modify the original. Pass
by reference means that essentially the variable address is passed (though
the name may change).

Example using Call by Value

#include <stdio.h>

void swapByValue(int, int); /* Prototype */

int main() /* Main function */


{
int n1 = 10, n2 = 20;

/* actual arguments will be as it is */


swapByValue(n1, n2);
printf("n1: %d, n2: %d\n", n1, n2);
}

void swapByValue(int a, int b)


{
int t;
t = a; a = b; b = t;
}

OUTPUT
======
n1: 10, n2: 20
Example using Call by Reference

#include <stdio.h>
void swapByReference(int*, int*); /* Prototype */

int main() /* Main function */


{
int n1 = 10, n2 = 20;

/* actual arguments will be altered */


swapByReference(&n1, &n2);
printf("n1: %d, n2: %d\n", n1, n2);
}

void swapByReference(int *a, int *b)


{
int t;
t = *a; *a = *b; *b = t;
}

OUTPUT
======
n1: 20, n2: 10
Function returning a pointer

int * smaller(int *a, int *b)


{
return (*a < *b?a:b);
}

void main()
{
int a,b,*ip;
scanf(“%d%d”,&a,&b);
ip=smaller(&a,&b);
printf(“\n smaller value is %d”,*ip);
}
Pointer to pointer

pointer points to an address of another pointer. Such pointers are known


as pointer to pointer
How to declare a pointer to Pointer in C?
int **pr;
Here in this example pr1 is a pointer to pointer

#include <stdio.h>
int main()
{
int num=123;

/*Pointer for num*/


int *pr2;

/*Double pointer for pr2*/


int **pr1;

/* I’m reading the address of variable num and


* storing it in pointer pr2*/
pr2 = &num;

/* storing the address of pointer pr2 into another pointer pr1*/


pr1 = &pr2;

/* Possible ways to find value of variable num*/


printf("\n Value of num is: %d", num);
printf("\n Value of num using pr2 is: %d", *pr2);
printf("\n Value of num using pr1 is: %d", **pr1);

/*Possible ways to find address of num*/


printf("\n Address of num is: %u", &num);
printf("\n Address of num using pr2 is: %u", pr2);
printf("\n Address of num using pr1 is: %u", *pr1);
/*Find value of pointer*/
printf("\n Value of Pointer pr2 is: %u", pr2);
printf("\n Value of Pointer pr2 using pr1 is: %u", *pr1);

/*Ways to find address of pointer*/


printf("\n Address of Pointer pr2 is:%u",&pr2);
printf("\n Address of Pointer pr2 using pr1 is:%u",*pr1);

/*pointer to pointer value and address*/


printf("\n Value of Pointer pr1 is:%u",pr1);
printf("\n Address of Pointer pr1 is:%u",&pr1);

return 0;
}
Arrays and pointers

There is strong relation between pointers and arrays.Array name is the


base address of the array or first element address, we can assign array
address into a pointer. Any operation by arrays can also be done with
pointers.

The following declaration defines an array of size 10, that is the block of
10 consecutive elements are a[0],a[1],a[2]...a[9]

int a[10];

Let us assume that an integer occupy 2 bytes of memory and base address
of array is 9700, so first value a[0] is at address 9700, next value a[1] is
at 9702, a[2] is at 9704 all elements of the array are located adjacent to
each other, if data elements are 5,10,15,..50 is shown as

The notation a[i] refers to the ith element,


pa is a pointer to an integer declared as int *pa;

then the assignment


pa=&a[0];

sets the pa to first element of a, that is pa contains address of a[0] base


address of a

The assignment x=*pa; will copy the content of a[0] into x;


Program to reads array elements and finds the sum using pointer

#include<stdio.h>
void main()
{
int a[10],*pa,i,n=10,sum=0;

pa=a;
for(i=o;i<n;i++)
{
scanf(“%d”,pa+i);
}
for(i=o;i<n;i++)
{
sum=sum+*(pa+i));
}
printf(“\n %d”,sum);
}
Operations on pointers

Following operations ar possible on pointer variables


a) Pointer arithmetic
b) Pointer comparision
We can perform addition and subtraction on pointers.

consider int s,*px; px=&x; x=25;

Assume the memory address of x is 5003, if px=px+3; is executed then


the px will contain 5009 is 5003+3*(size of integer) i.e. 5003+3*2=5009

i.e. base address + number * size of datatype

++*px; // increnebts the value of variable x by 1


--*px // decrement x by 1
*px +10 // add 10 to x
*px*10+x // is same as x*10+x

Two pointers can be compared with a relational operator.


int x,y,*px,*py;
px=&x;
py=&py;
x=25;
y=100;
if x is at address 8686, and y is at address 8688
px>py // is false
py>=px // true
px+1==py //true
px-py >0 //flase , its value is -1

You might also like