0% found this document useful (0 votes)
30 views25 pages

Pci Unit6

The document discusses pointers in C programming. It defines pointers as variables that store memory addresses and explains that pointers "point" to locations in memory. The summary discusses how pointers are declared with a data type and asterisk, and how they are used to access the value at a memory address. Examples are provided to demonstrate defining and accessing variables using pointers, as well as pointer arithmetic operations. Common applications of pointers discussed include dynamic memory allocation, arrays, functions, and structures.

Uploaded by

kenemep346
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)
30 views25 pages

Pci Unit6

The document discusses pointers in C programming. It defines pointers as variables that store memory addresses and explains that pointers "point" to locations in memory. The summary discusses how pointers are declared with a data type and asterisk, and how they are used to access the value at a memory address. Examples are provided to demonstrate defining and accessing variables using pointers, as well as pointer arithmetic operations. Common applications of pointers discussed include dynamic memory allocation, arrays, functions, and structures.

Uploaded by

kenemep346
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/ 25

Programming in ‘C’

By
Mr. Parag R. Sali
Lecturer
Department of Computer Technology
SNJB’s Shri. Hiralal Hastimal ( Jain Brothers)
Polytechnic, Chandwad
Program Name: Computer Engineering Group
Program Code : CO/CM/IF/CW
Semester : Second
Course Title : Programming in ‘C’
Course Code : 22226

Pointers
POINTER
A pointer is a variable that store memory address or that contains address of
another variable. It is called pointer because it points to a particular location in
memory by storing address of that location.
Syntax-
Data type *pointer name;
Here * before pointer indicate the compiler that variable declared as a pointer.
e.g.
int *p1; //pointer to integer type
float *p2; //pointer to float type
char *p3; //pointer to character type

When pointer declared, it contains garbage value i.e. it may point any value in
the memory.
The actual data type of the value of all pointers, whether integer, float,
character, or otherwise, is the same, a long hexadecimal number that
represents a memory address. The only difference between pointers of
different data types is the data type of the variable or constant that the pointer
points to.

How to Use Pointers?


There are a few important operations, which we will do with the help of
pointers very frequently.
(a) We define a pointer variable,
(b) assign the address of a variable to a pointer and
(c) finally access the value at the address available in the pointer variable.

This is done by using unary operator * that returns the value of the variable
located at the address specified by its operand.
The following example makes use of these operations −

#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: %x\n", &var );
printf("Address stored in ip variable: %x\n", ip ); // address stored in pointer variable
printf("Value of *ip variable: %d\n", *ip ); // access the value using the pointer
return 0;
}
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
Pointers variables are also known as address data types because they are
used to store the address of another variable. The address is the memory
location that is assigned to the variable. It doesn’t store any value.
Hence, there are only a few operations that are allowed to perform on
pointers .The operations are slightly different from the ones that we generally
use for mathematical calculations. The operations are:

1. Increment/Decrement of a Pointer
2. Addition of integer to a pointer
3. Subtraction of integer to a pointer
4. Subtracting two pointers of the same type
#include<stdio.h>
#include<conio.h>
void main()
{
int a=7;
int *ptr;
clrscr();
printf("value of a is :%d ",a);
printf("\nsize of a is :%d",sizeof(a));
printf("\naddress of a :%x",&a);
ptr=&a;
printf("\naddress of a with ptr is :%p",ptr);
printf("\nvalue of a with ptr is :%p",*ptr);
getch();
}
value of a is :7
size of a is :2
address of a :fff4
address of a with ptr is :FFF4
value of a with ptr is :0007
#include<stdio.h>
#include<conio.h>
void main()
{
int a=7;
char s;
int *ptr;
char *ptr2;
clrscr();
ptr =&a ;
printf("\naddress of a :%x",ptr);
ptr2 = &s;
printf("\naddress of s is:%x",ptr2);
ptr++;
ptr2++;
printf("\naddress of a with increment is :%x",ptr);
printf("\naddress of s with increment is :%x",ptr2);
getch();
}
address of a :fff4
address of s is:fff3
address of a with increment is :fff6
address of s with increment is :fff4
#include<stdio.h>
#include<conio.h>
void main()
{
int a=7;
int *ptr;
clrscr();
ptr =&a ;
printf("\naddress of a :%x",ptr);
ptr++;
printf("\naddress of a with increment is :%x",ptr);
ptr--;
printf("\naddress of a with decrement is :%x",ptr);
ptr+4;
printf("\naddress of a by adding 4 is :%x",ptr);
ptr-2;
printf("\naddress of a by substracting 2 is :%x",ptr);
getch();
}
address of a :fff4
address of a with increment is :fff6
address of a with decrement is :fff4
address of a by adding 4 is :fff4
address of a by substracting 2 is :fff4
#include<stdio.h>
#include<conio.h>

void main()
{
int a=7;
int *ptr;
clrscr();
ptr =&a ;
printf("\naddress of a :%x",ptr);
ptr = ptr +20;
printf("\naddress of a with increment is
:%x",ptr);
getch();
}
address of a :fff4
address of a with increment is :1c
#include<stdio.h>
#include<conio.h>

void main()
{
int i,x[5], sum=0;
int *ptr;
clrscr();
printf("Enter 5 numbers:");
for(i=0;i<5;i++)
{
scanf("%d",x+i); //scanf("%d",&x[i]);
sum += *(x+i); //sum = sum +
}
printf("\nSum is :%d",sum);
getch();

}
Pointers
 Pointers are the heart of C programming. It is the most distinct feature of C, which provides
power and flexibility to C.

 Pointers separates C from other programming languages.

 C programmers make extensive use of pointers, because of their numerous benefits.


Below are some advantages of pointers.
 Pointers are more efficient in handling arrays and structures.
 Pointers are used to return multiple values from a function.
 We use pointers to get reference of a variable or function.
 Pointer allows dynamic memory allocation (creation of variables at runtime) in C. Which
undoubtedly is the biggest advantage of pointers.
 Pointers increases execution speed of program.
 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.
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.

 int *a; //pointer to int

 char *c; //pointer to char


MSBTE Examples
Q. WAP to find product of two numbers using pointer.
#include<stdio.h>
int main(){
int num1,num2;
int *p,*q;
printf(“Enter first number:”);
scanf(“%d”, & num1);
printf(“Enter second number:”);
scanf(“%d”, & num2);
p=&num1;
q=&num2;
printf(“\n Product of two numbers: %d *%d= %d ”, *p,*q,(*p)*(*q));
return 1;
}
MSBTE Examples
Q.WAP to print terms of Fibonacci series using pointers.

#include<stdio.h>
#include<conio.h>
int main(){

int range,i=0,j=1,k,f;
int *p,*q,*r;

p=&range;
q=&i;
r=&j;
printf(“Enter the number range of Fibonacci series:\t”);
printf(“\n FIBINACCI SERIES:”);
printf(“%d%d”,i,j);
for(k=2;k<*p;k++)
{
f=*q+*r;
printf(“%d”,f);
*q=*r;
*r=f;
}
return 1;
}
MSBTE Examples
Write the value of d in the following component of code
int a=20;
int *b=&a
int *c=*b
int d=*b+*c;
Ans.
Errors For statement 2 & 3 expected ; at the end
Code after correcting the errors :
int a= 20;
int *b= &a;
int *c= *b;
int d=*b+*c;
Output
d=20+20= 40

You might also like