C Programming : Pointer and Structure (BIM-TU)

By Ashesh Neupane

This article presents a few examples of using pointers and structure. You will learn how to use pointers, how to store data using structures.

1. Write a program to find the greater between two numbers by the use of pointer.

#include<stdio.h>
int main()
{
    int a,b;
    int *p=&a,*q=&b;
    printf("Enter any two numbers:");
    scanf("%d%d",p,q);
    if(*p>*q)
    printf("%d is greater",*p);
    else
    printf("%d is greater",*q);
    return 0;
}
Enter any two numbers:2 5
5 is greater

2. WAP to show both pass by value and pass by reference.

#include<stdio.h>
int test(int a,int *p);
void main()
{
    int a=5,b=10;
    printf("Before calling test,a=%d b=%d\n",a,b);
    test(a,&b);
    printf("After calling test,a=%d b=%d\n",a,b);
}
int test(int a,int *p)
{
    a=a+5;
    *p=*p+5;
    printf("Inside test, a=%d b=%d\n",a,*p);
}
Before calling test,a=5 b=10
Inside test, a=10 b=15
After calling test,a=5 b=15

3. Create a structure to store name, address, salary of a person and display the person details.

#include<stdio.h>
#include<string.h>
struct person
{
    char name[20],address[20];
    int salary;
};
void main()
{
    struct person p;
    printf("Enter the name, address and salary:");
    scanf("%s%s%d",p.name,p.address,&p.salary);
    printf("\nName:%s\t Address:%s\t Salary:%d",p.name,p.address,p.salary);
}
Enter the name, address and salary:Raghu Kathmandu 28000

Name:Raghu	 Address:Kathmandu	 Salary:28000

4. Create an array of structure to store name, address, salary of 10 persons and display the details of highest paid person.

#include<stdio.h>
#include<string.h>
struct person
{
    char name[20],address[20];
    int salary;
};
void main()
{
    struct person p[10];
    int highest,i;
    printf("Enter name, address and salary:");
    for(i=0;i<10;i++)
    {
        scanf("%s%s%d",p[i].name,p[i].address,&p[i].salary);
    }
    highest=p[0].salary;
    for(i=0;i<10;i++)
    {
        if(p[i].salary>highest)
        highest=p[i].salary;
    }
    for(i=0;i<10;i++)
    {
        if(p[i].salary==highest)
        printf("\n Highest Salary Paid Employee Details:\n Name : %s Address : %s Salary : %d",p[i].name,p[i].address,p[i].salary);
    }
}
Enter name, address and salary:
Aashutosh KTM 25000
Parbat Jhapa 27000
Ashesh KTM 75000
Nishant Pokhara 25000
Abhishek Bhojpur 50000
Nikesh Birgunj 30000
Maneek Gulmi 32000
Raghu Butwal 32500
Dibinsh KTM 35000
Mandeep KTM 32000

 Highest Salary Paid Employee Details:
 Name : Ashesh Address : KTM Salary : 75000

5. Create an array of structure to store name, address, salary of 5 persons and sort the records in ascending order.

#include<stdio.h>
#include<string.h>
struct person
{
    char name[20],address[20];
    int salary;
};
void main()
{
    struct person p[5],temp;
    int i,j;
    printf("Enter name, address and salary:");
    for(i=0;i<5;i++)
    {
        scanf("%s%s%d",p[i].name,p[i].address,&p[i].salary);
    }
    for(i=0;i<5;i++)
    {
        for(j=i+1;j<5;j++)
        {
            if(strcmpi(p[i].name,p[j].name)>0)
            {
                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    printf("Person Detail After Sorting:\n");
    for(i=0;i<5;i++)
    {
        printf("%s %s %d\n",p[i].name,p[i].address,p[i].salary);
    }
}
Enter name, address and salary:
Nishant Pokhara 20000
Raghu KTM 25000
Prashant BKT 20000
Ashesh KTM 30000
Parbat Jhapa 15000
Person Detail After Sorting:
Ashesh KTM 30000
Nishant Pokhara 20000
Parbat Jhapa 15000
Prashant BKT 20000
Raghu KTM 25000

Ashesh Neupane is the Co-Founder and Former Admin of HighApproach. He is also a student of Bachelor of Information Management (BIM) at Tribhuvan University.

Leave a Comment

Slide to prove you're not a bot/spammer *