1. Program to accept two numbers and print its address along with the numbers
# include <stdio.h> # include <conio.h> // Linux user - Remove this int main( ) { // Declaring variable a, b = two numbers int a, b; // Inputing two numbers printf("Enter first number : "); scanf("%d",&a); printf("Enter first number : "); scanf("%d",&b); // Printing address along with number printf("First number : %d, Address : %d\n",a, &a); printf("First number : %d, Address : %d\n",*(&a), &a); printf("Second number : %d, Address : %d\n",b, &b); printf("Second number : %d, Address : %d",*(&b), &b); getch(); // Linux user - Remove this return 0; }
2. Program to accept two numbers and print the sum of given two numbers by using pointers
# include <stdio.h> # include <conio.h> // Linux user - Remove this int main( ) { // Declaring variable a, b = two numbers int a, b; // Declaring variale s=sum int s=0; // Inputing two numbers printf("Enter first number : "); scanf("%d",&a); // Inputing second number printf("Enter first number : "); scanf("%d",&b); // Determining sum s=*(&a)+*(&b); // Printing sum printf("Sum = %d",s); getch( ); // Linux user - Remove this return 0; }
3. Program to interchange two values using pointers
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring variable a, b for two values, swap to interchange value int a, b, swap; // Declaring pointer that holds address of two values int *c, *d; // Storing address c=&a; d=&b; // Inputing values printf("Enter first value a : "); scanf("%d",&a); printf("Enter second value b : "); scanf("%d",&b); // Printing original value printf("Original value : a = %d, b = %d\n",a, b); // Interchanging value swap=*c; *c=*d; *d=swap; // Printing interchanged value printf("Interchanged value : a = %d, b = %d ",a, b); getch(); // Linux user - Remove this return 0; }
4. Implement a function interchange() to interchange two values using pointers
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring variable a, b for two values int a,b; // Inputing values printf("Enter first value a : "); scanf("%d",&a); printf("Enter second value b : "); scanf("%d",&b); // Printing original value printf("Original value : a = %d, b = %d\n",a, b); // Calling function interchange interchange(&a, &b); getch(); // Linux user - Remove this return 0; } void interchange(int *x, int *y) { int swap; // Interchanging value swap=*x; *x=*y; *y=swap; // Printing interchanged value printf("Interchanged value : a = %d, b = %d ",*x, *y); }
5. Program to sum all the elements of an array using pointer
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring array variable "a" of size 10 and initializing it int a[10]={21, -2, 4, 6, 9, 12, 43, 22, 4, 8}, sum=0; // Declaring pointer type variable pointing to the the base address i.e ( a[0] ) of array 'a' int *ptr=a; // It is equivalent to : int *ptr; ptr=&a; // Declaring variable ptre = stores address of 11th element int *ptre; ptre = ptr+10; // Calculating sum while(ptr<ptre) { sum+=*ptr++; // sum=sum + *ptr++ } // Displaying calculated sum printf("Sum of all elements : %d ",sum); getch(); // Linux user - Remove this return 0; }