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

Unit 6 C Programming

Uploaded by

tejas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Unit 6 C Programming

Uploaded by

tejas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit 6

Pointers
________________________________________________________________

Contents
• Concept of Pointer

• Pointer Arithmetic

• Array with Pointer

• Functions with pointer

• Structure with pointer


Pointer

The pointer in C language is a variable which stores the address of another


variable. This variable can be of type int, char, array, function, or any other
pointer. Pointers are used to access memory and manipulate the address.

Address in C

Whenever a variable is defined in C language, a memory location is assigned


for it, in which it's value will be stored. We can easily check this memory address,
using the & symbol.

If var is the name of the variable, then &var will give its address.

// Program to understand working of & operator


#include<stdio.h>
void main()
{
int var = 7;
printf("Value of the variable var is: %d\n", var);
printf("Memory address of the variable var is: %x\n", &var);
}

Declaring a pointer

The pointer in c language can be declared using * (asterisk symbol). It is


also known as indirection pointer used to dereference a pointer.

Syntax & Example

int *a;//pointer to int


datatype *pointer_name;
char *c;//pointer to char

Assigning memory address to pointer

1. Pointer address operator is denoted by ‘&’ symbol


2. When we use ampersand symbol as a prefix to a variable name ‘&’, it gives
the address of that variable.

&n - It gives an address on variable n

#include<stdio.h>
int main()
{
int a;
int *ptr;
ptr=&a;
printf("Address of a is
%u",ptr);
}
#include<stdio.h>
int main()
{
float a;
float *ptr;

char b;
char *p;

ptr=&a;
printf("Address of float a is %u\n",ptr);

p=&b;
printf("Address of char a is %u",p);
}

Accessing value of a variable by Pointer

Asterisk(*) indirection operator is used along with pointer variable while


Dereferencing the pointer variable. Asterisk Operator is also called as value at
operator
When used with Pointer variable, it refers to variable being pointed to, this
is called as Dereferencing of Pointers. By the help of * (indirection operator),
we can print the value of pointer variable p.
#include<stdio.h>
int main(){

int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return 0;
}

Advantage of pointer

1) Pointer reduces the code and improves the performance, it is used to retrieving
strings, trees, etc. and used with arrays, structures, and functions.

2) We can return multiple values from a function using the pointer.

3) It makes you able to access any memory location in the computer's memory.
Usage of pointer

There are many applications of pointers in c language.

1) Dynamic memory allocation

In c language, we can dynamically allocate memory using malloc() and


calloc() functions where the pointer is used.

2) Arrays, Functions, and Structures

Pointers in c language are widely used in arrays, functions, and structures.


It reduces the code and improves the performance.

Pointer Arithmetic-

We can perform arithmetic operations on the pointers like addition,


subtraction, etc. However, as we know that pointer contains the address, the
result of an arithmetic operation performed on the pointer will also be a pointer if
the other operand is of type integer. In pointer-from-pointer subtraction, the
result will be an integer value. Following arithmetic operations are possible on the
pointer in C language:

• Increment
• Decrement
• Addition
• Subtraction

Incrementing Pointer in C

If we increment a pointer by 1, the pointer will start pointing to the


immediate next location.

Decrementing Pointer in C

Like increment, we can decrement a pointer variable. If we decrement a


pointer, it will start pointing to the previous location.

C Pointer Addition

We can add a value to the pointer variable. The formula of adding value to
pointer is given below:

C Pointer Subtraction

Like pointer addition, we can subtract a value from the pointer variable.
Subtracting any number from a pointer will give an address.
Program-

#include<stdio.h>
int main()
{
int number=50;
int *p;//pointer to int

p=&number;//stores the address of number variable


printf("Address of p variable is %u \n",p);

p=p-3; //subtracting 3 from pointer variable


printf("After Substraction: Address of p variable is %u \n",p);

p++;
printf("After Increament: Address of p variable is %u \n",p);

p=p+2;
printf("After Addition: Address of p variable is %u \n",p);

return 0;
}

We can also perform arithmetic operation by using pointer variable as follows.

#include<stdio.h>
int main(){
int a,b,c;
int *p,*q;
printf("Enter a and b\n");
scanf("%d%d",&a,&b);

p=&a;
q=&b;

c= *p + *q; printf("\nAddition is %d ",c);

c=*p - *q; printf("\nSubstraction is %d ",c);

c=(*p) * (*q); printf("\nMultiplication is %d ",c);

c=*p/ *q; printf("\nDivision is %d ",c);

printf("\nAddress of a before inc is %d ",p);


p++;
printf("\nAddress of a after inc is %d ",p);
}
Pointer to Arrays

We can use pointer with arrays. Pointers with arrays used to insert
elements into array and also to retrieve array elements. In case of pointer to
arrays pointer holds the base address of array. To move the pointer to next
position the pointer needs to be incremented.

Example-

int rollno[5]={12,8,16,7,45};//array declaration and initialization

int *ptr; // pointer declaration

ptr = &rollno[0]; // assigning base address to pointer

index 0 1 2 3 4

rollno 12 8 16 7 45

address 3422 3424 3426 3428 3430

ptr++

ptr = &rollno[0]; // address 3422 will be assigned to ptr

The pointer needs to be incremented as ptr++. Otherwise the pointer will


point to same element and will not move to next element.

#include<stdio.h>
int main()
{
short int a[5]={10,7,6,9,87};

int i;
short int *ptr;
ptr= &a[0];
for(i=0;i<5;i++)
{
printf("%d\n",ptr);
ptr++;
}
}

Pointers to Functions (Call by reference)

When address are passed to the function instead of actual values then it is
called as call by reference. While passing parameter using call by address scheme,
we are passing the actual address of the variable to the called function.
Any updates made inside the called function will modify the original
copy since we are directly modifying the content of the exact memory location.
#include <stdio.h>
void increment(int *var)
{
*var = *var+1;
}
int main()
{
int num=20;

increment(&num);
printf("Value of num is: %d", num);
return 0;
}

Pointer with Structure-

The Structure members can be accessed by using -> operator. The


members can also be accessed by using pointer.

• Steps to use Pointer with Structure.


• Create a Structure variable.
• Create a pointer of that structure.
• Assign the address of structure variable to pointer.
• Access the members of Structure using pointer.

#include<stdio.h>
struct Player{
int mat;
char name[20];
float avg;
};
void main()
{
struct Player *v; // pointer to structure
struct Player B; // pointer variable

v=&B; // assigning address

printf("Enter matched played\n");


scanf("%d",&v->mat);

printf("Enter Name of player\n");


scanf("%s",v->name);

printf("Enter batting Average\n");


scanf("%f",&v->avg) ;

printf("Matches Played %d\n",v->mat);


printf("NAME is %s\n",v->name);
printf("Average of Player is %f\n",v->avg);
}

Array of pointer
There could be a situation when we want to maintain an array, which can
store pointers to an int or char or any other data type available. Following is the
declaration of an array of pointers to an integer.

#include<stdio.h>

int main()
{
int i;

char *arr[4] = {"C","C++","Java","DS"};


char *(*ptr)[4] = &arr;

for(i=0;i<4;i++)
printf("\nString %s :",arr[i]);

return 0;
}

You might also like