Upcoming SlideShare
×

# C Prog - Pointers

• 848 views

• Comment goes here.
Are you sure you want to
Be the first to comment

Total Views
848
On Slideshare
0
From Embeds
0
Number of Embeds
3

Shares
80
0
Likes
1

No embeds

### Report content

No notes for slide

### Transcript

• 1. C Programming - PointersOrganized By: Vinay Arora Assistant Professor, CSED Thapar University, Patiala
• 2. Program - 1 #include<stdio.h> void swapr(int x, int y) #include<conio.h> { void swapr(int,int); int t; int main() t=x; { x=y; int a=10,b=20; y=t; clrscr(); } printf("Value of variable a=%d and b=%d",a,b); swapr(a,b); printf("nValue of variable a=%d and b=%d",a,b); getch(); } Vinay Arora CSED
• 3. Program – 1 (output) Vinay Arora CSED
• 4. Program - 2 #include<stdio.h> void swapr(int *x, int *y) #include<conio.h> { void swapr(int *, int *); int t; int main() t=*x; { *x=*y; int a=10,b=20; *y=t; clrscr(); } printf("Value of variable a=%d and b=%d",a,b); swapr(&a,&b); printf("nValue of variable a=%d and b=%d",a,b); getch(); } Vinay Arora CSED
• 5. Program – 2 (output) Vinay Arora CSED
• 6. Program - 3 #include<stdio.h> void areaperi(int r, float *a, float *p) #include<conio.h> { void areaperi(int,float *,float *); *a=3.14*r*r; int main() *p=2*3.14*r; { int radius; } float area, perimeter; clrscr(); printf("Enter radius of circle"); scanf("%d",&radius); areaperi(radius,&area,&perimeter); printf("Area=%fn",area); printf("Perimeter=%fn",perimeter); return 0; } Vinay Arora CSED
• 7. Program – 3 (output) Vinay Arora CSED
• 8. Passing Array Value (CBVal) Vinay Arora CSED
• 9. Passing Array Reference (CBRef) Vinay Arora CSED
• 10. Vinay Arora CSED
• 11. Program - 4 #include<stdio.h> #include<conio.h> void main() { int i, *x; clrscr(); printf("Enter any Integer Valuet"); scanf("%d",&i); x=&i; printf("nx=Address value of in"); printf("nValue of x = %u",x); x++; printf("nAfter Increment in Pointern"); printf("Value of x = %u",x); getch(); } Vinay Arora CSED
• 12. Program – 4 (output) Vinay Arora CSED
• 13. Program - 5 #include<stdio.h> #include<conio.h> void main() { int a=10, b=20, *p, *j; clrscr(); p=&a; j=&b; printf("nAddition *p + b = %d", *p + b); printf("nAddition *p + *j = %d", *p + *j); printf("nAddition *(p) + *(j) = %d", *(p) + *(j)); printf("nAddition *(&a) + *(&b) = %d", *(&a) + *(&b)); getch(); } Vinay Arora CSED
• 14. Program – 5 (output) Vinay Arora CSED
• 15. Program - 6 #include<stdio.h> #include<conio.h> printf("nj - i = %d",j-i); printf("n*j - *i =%d",*j - *i); void main() { getch(); int arr[]={10,20,30,45,67,56,74}; } int *i,*j; int x; clrscr(); printf("Array Elements are"); for(x=0;x<=6;x++) { printf("n Value at arr[%d] is %d",x,arr[x]); } i=&arr[1]; j=&arr[5]; Vinay Arora CSED
• 16. Program – 6 (output) Vinay Arora CSED
• 17. Program - 7 #include<stdio.h> for(i=0;i<=5;i++) #include<conio.h> { printf("nAddress = %u",j); void main() printf("nElement = %d",*j); { j++; int num[]={24,34,12,44,56,17}; } int i,*j; getch(); clrscr(); } printf("Array Elements are"); for(i=0;i<=5;i++) { printf("n Value at arr[%d] is %d",i,num[i]); } j=&num[0]; Vinay Arora CSED
• 18. Program – 7 (output) Vinay Arora CSED
• 19. Program – 8 (Diff. notations for accessing Array) #include<stdio.h> getch(); #include<conio.h> } void main() { int num[]={24,34,12}; int i; clrscr(); printf("Array Elements are"); for(i=0;i<=2;i++) { printf("n Value at arr[%d] is %d",i,num[i]); printf("n Value at arr[%d] is %d",i,i[num]); printf("n Value at arr[%d] is %d",i,*(num+i)); printf("n Value at arr[%d] is %d",i,*(i+num)); printf("n"); } Vinay Arora CSED
• 20. Program – 8 (output) Vinay Arora CSED
• 21. Program - 9 #include<stdio.h> #include<conio.h> void main() { int s[3][2]={ {10,15}, {20,25}, {30,35} }; int i; clrscr(); for(i=0;i<=2;i++) printf("Address of %dth 1-D array = %un",i,s[i]); getch(); } Vinay Arora CSED
• 22. Program – 9 (output) Vinay Arora CSED
• 23. Program – 10 (Conti…) #include<stdio.h> printf("Notation Used is s[i][j])n"); #include<conio.h> for(i=0;i<=2;i++) void main() { { for(j=0;j<=1;j++) int s[3][2]={ { {10,15}, printf("t%d",s[i][j]); {20,25}, } {30,35} printf("n"); }; } int i,j; clrscr(); getch(); printf("Array Elements aren"); } Vinay Arora CSED
• 24. Program – 10 (Conti…) #include<stdio.h> printf("nAccessing 2-D Arrayn"); #include<conio.h> printf("Notation Used is *(s[i]+j)n"); void main() for(i=0;i<=2;i++) { { int s[3][2]={ for(j=0;j<=1;j++) {10,15}, { {20,25}, printf("t%d",*(s[i]+j)); {30,35} } }; printf("n"); int i,j; } clrscr(); getch(); } Vinay Arora CSED
• 25. Program – 10 (Conti…) #include<stdio.h> printf("nAccessing 2-D Arrayn"); #include<conio.h> printf("Notation Used is void main() *(*(s+i)+j)n"); { for(i=0;i<=2;i++) int s[3][2]={ { {10,15}, for(j=0;j<=1;j++) {20,25}, { {30,35} printf("t%d",*(*(s+i)+j)); }; } int i,j; printf("n"); clrscr(); } getch(); } Vinay Arora CSED
• 26. Program – 10 (output) Vinay Arora CSED
• 27. Program – 11 (Array of Pointer) #include<stdio.h> printf("Address of variable a,b,cn"); #include<conio.h> printf("%un",a); printf("%un",b); void main() printf("%un",c); { int *arr[3]; printf("Value Present in Arrayn"); int i=10, j=20, k=30, m; for(m=0;m<=2;m++) int *a,*b,*c; { printf("n%u",arr[m]); clrscr(); } printf("n"); printf("n"); arr[0] = &i; for(m=0;m<=2;m++) arr[1] = &j; for(m=0;m<=2;m++) { { arr[2] = &k; printf("n%d",*(arr[m])); printf("n%u",&arr[m]); } } a=&i; getch(); b=&j; } c=&k; Vinay Arora CSED
• 28. Program – 11 (output) Vinay Arora CSED
• 29. Thnx… Vinay Arora CSED