Pointers




Pointer Programs
1. Write a user defined function which will swap the values of two variables declared locally in the main program.




#include<stdio.h>
void swap(int *p, int*q);
void main()
{
    int a=4,b=5;
    printf("\n Before swap %d - %d",a,b);
    swap(&a,&b);
    printf("\n After swap %d - %d",a,b);
}
void swap(int *p, int *q)
{
    int t;
    t=*p;
    *p=*q;
    *q=t;
}



2. Write a user defined function calc(), which will returns the sum, subtraction, multiplication, and division values of two variable locally declared in the main function.



#include<stdio.h>
void calc( int,int);
void main()
{
int no1,no2;
void (*p) (int, int);
printf("\n Enter no1");
scanf("%d", &no1);
printf("\n Enter no2");
scanf("%d", &no2);
p = &calc;
(*p) (no1,no2);
}
void calc(int no1,int no2)
{
int add,mul,sub,div;
add=no1+no2;
mul=no1*no2;
sub=no1-no2;
div=no1/no2;
printf("\n Addition is %d \n Mul is %d \n Subtraction is %d \n Div is %d", add,mul,sub,div);
}


3. Write a user defined function which will return the length of the string declared locally in the main function.


#include<stdio.h>
int findlength(char *p);
void main()
{
    char string[10];
    int ans;
    printf("\n Enter string");
    scanf("%s", string);
    ans=findlength(string);
    printf("\n Length of string is %d", ans);
}
int findlength(char *p)
{
    int i=0;
    while(*p !=NULL)
    {
        p++;
        i++;
    }
return(i);
}


4. Write a program, which takes a name of the user in the lowercase letters. Call a user defined function upper which will convert all lowercase letters into the uppercase letter. Finally print the string.



 #include<stdio.h>
#include<ctype.h>
void convup(char *p);
void main()
{
    char string[10];
    printf("\n Enter string");
    scanf("%s", string);
    convup(string);
}
void convup(char *p)
{
    while(*p !=NULL)
    {
        printf("%c",toupper(*p));
         p++;
    }
}


5. Write a user defined function to reverse the given string.



#include<stdio.h>
void rev(char *p);
void main()
{
    char string[10];
    printf("\n Enter string");
    scanf("%s", string);
    rev(string);
}
void rev(char *p)
{
    int i=0,j;
    while(*p !=NULL)
    {
       p++;
       i++;
    }
    p--;
    for(j=i;j>=0;j--)
    {
        printf("%c",*p);
        p--;
    }
}


6. Create a structure product with ProductCode (int), Name (char array) and Price data elements. In the main function declare p[5] of product. Do the necessary data entry for all five products. Pass the base address of an array to user defined function inc_price(), which will increase the price of all the products by 10%. Print all the products with all the details again after increasing the price.



#include<stdio.h>
struct item
{
int code;
char name[10];
int price;
};
void inc(struct item it[],int x);
void main()
  {
struct item it[5];
int i;
for(i=0;i<5;i++)
{
printf("\n enter code, name and price");
scanf("%d%s%d", &it[i].code, it[i].name, &it[i].price);
}
inc(it,5);
}
void inc(struct item it[],int x)
{
int i;
for(i=0;i<5;i++)
{
it[i].price += it[i].price*0.01;
printf("\n %d %s %d", it[i].code,it[i].name,it[i].price);
} }





7. Create a structure student with rollno (int), name (char array), marks (int), grade (char). Create an array stu[5] of type student. Take the details of students like rollno, name, and marks from the user. Call UDF prepare_result() which will store the values for grade based on marks (if marks >=75 then ‘A’, Between 60 to 75 ‘B’, Between 50 to 60 ‘C’, Between 35 to 50 ‘D’ and Below 35 ‘F’ grade). Print the details of the students with Grade.



#include<stdio.h>
struct student
{
int no;
char name[10];
int m;
char g;
};
void findg(struct student s[],int x);
void main()
  {
struct student s[5];
int i;
for(i=0;i<5;i++)
{
printf("\n enter no, name and marks");
scanf("%d%s%d", &s[i].no, s[i].name, &s[i].m);
}
findg(s,5);
}
void findg(struct student s[],int x)

{
int i;
for(i=0;i<5;i++)
{
    if(s[i].m>=75)
        s[i].g = 'A';
    else if(s[i].m>=60)
        s[i].g = 'B';
    else if(s[i].m>=50)
        s[i].g = 'C';
    else if(s[i].m>=35)
        s[i].g = 'D';
        else
            s[i].g='F';

printf("\n %d %s %d %c", s[i].no,s[i].name,s[i].m,s[i].g);
}
}