Pci Unit6
Pci Unit6
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.
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.
#include<stdio.h>
#include<conio.h>
int main(){
int range,i=0,j=1,k,f;
int *p,*q,*r;
p=⦥
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