c Programming 20 21 22
c Programming 20 21 22
Syntax-
Here * before pointer indicate the compiler that variable declared as a pointer.
e.g.
When pointer declared, it contains garbage value i.e. it may point any value in the
memory.
Two operators are used in the pointer i.e. address operator(&) and indirection
operator or dereference operator (*).
Example:
void main()
int i=105;
int *p;
p=&i;
printf(“value of i=%d”,*p);
printf(“value of i=%d”,*/&i);
printf(“address of i=%d”,&i);
printf(“address of i=%d”,p);
printf(“address of p=%u”,&p);
Pointer Expression
Pointer assignment
int i=10;
P++;
printf(“value of p=%d”);
We can assign value of 1 pointer variable to other when their base type and data
type is same or both the pointer points to the same variable as in the array.
Int *p1,*p2;
P1=&a[1];
P2=&a[3];
We can assign constant 0 to a pointer of any type for that symbolic constant
‘NULL’ is used such as
*p=NULL;
Pointer Arithmetic
Example:-
Note:-
Ex:-
void main( )
int *p,*p1;
P=&a[1];
P1=&a[6];
printf(“%d”,*p1-*p);
printf(“%d”,p1-p);
Example
int i=100;
int *p;
p=&i;
p=p+2;
p=p+3;
p=p+9;
Ex:-
int i=22;
*p1=&a;
p1=p1-10;
p1=p1-2;
iii- Subtraction of one pointer to another is possible when pointer variable point to
an element of same type such as an array.
Ex:-
in tar[ ]={2,3,4,5,6,7};
int *ptr1,*ptr1;
ptr1=&a[3]; //2000+4
ptr2=&a[6]; //2000+6
Lecture Note: 21
Precedence of dereference (*) Operator and increment operator and
decrement operator
Example :-
int x=25;
int *p=&x;
Equivalent to *(p++)
Since the operator associate from right to left, increment operator will applied to
the pointer p.
p =p++ or p=p+1
ii) *++p;→*(++p)→p=p+1
y=*p
equivalent to ++(*p)
p=p+1 then *p
y=*p then
P=p+1 ;
Pointer Comparison
Pointer variable can be compared when both variable, object of same data type
and it is useful when both pointers variable points to element of same array.
Moreover pointer variable are compared with zero which is usually expressed as
null, so several operators are used for comparison like the relational operator.
==,!=,<=,<,>,>=, can be used with pointer. Equal and not equal operators used to
compare two pointer should finding whether they contain same address or not and
they will equal only if are null or contains address of same variable.
Ex:-
void main()
int *x,*y;
x=&a[5];
y=&(a+5);
if(x==y)
printf(“same”);
else
printf(“not”);
Lecture Note: 22
Pointer to pointer
Or
Syntax:-
int x=22;
int *p=&x;
int **p1=&p;
printf(“value of x=%d”,x);
printf(“value of x=%d”,*p);
printf(“value of x=%d”,*&x);
printf(“value of x=%d”,**p1);
printf(“value of p=%u”,&p);
printf(“address of p=%u”,p1);
printf(“address of x=%u”,p);
printf(“address of p1=%u”,&p1);
printf(“value of p=%u”,p);
printf(“value of p=%u”,&x);
P 2000
X 1000
p1 22
96 *Under revision
3000
Pointer vs array
Example :-
void main()
char*p=”Rama”;
In the above example, at the first time printf( ), print the same value array and
pointer.
97 *Under revision
Sructure
Structure declaration-
struct tagname
………
………
};
OR
struct
98 *Under revision
Data type member3;
………
………
};
OR
struct tagname
struct element 1;
struct element 2;
struct element 3;
………
………
struct element n;
};
struct student
int age;
char name[20];
char branch[20];
99 *Under revision
}; struct student s;
Like primary variables structure variables can also be initialized when they are
declared. Structure templates can be defined locally or globally. If it is local it can
be used within that function. If it is global it can be used by all other functions of
the program.
struct student
int age=20;
char name[20]=”sona”;
}s1;
struct student
int age,roll;
char name[20];
If initialiser is less than no.of structure variable, automatically rest values are taken
as zero.
Dot operator is used to access the structure elements. Its associativety is from left
to right.
structure variable ;
s1.name[];
s1.roll;
s1.age;
Example:
#include<stdio.h>
#include<conio.h>
void main()
char branch;
} s1,s2;
s2.roll=s1.roll;
printf(“%d”, s2.roll);