Labels

Showing posts with label C PROGRAMS (Arrays Pointers and Structures). Show all posts
Showing posts with label C PROGRAMS (Arrays Pointers and Structures). Show all posts

Thursday, 7 March 2013

Program to understand how an array of structures is sent to a function

#include<stdio.h>
struct student
{
char name[20];
int rollno;
int marks;
};
void display(struct student);
void dec_marks(struct student stuarr[ ]);
main( )
{
int i;
struct student stuarr[3] =
{
{"Mary", 12, 98},
{"John", 11, 97},
{"Tom", 12, 89}
};
dec_marks(stuarr);
for(i=0; i<3; i++ )
display(stuarr[i]);
}
void dec_marks(struct student stuarr[])
{
int i;
for(i=0; i<3; i++)
stuarr[i].marks = stuarr[i].marks-10;
}
void display(struct student stu)
{
printf("Name  - %s\t", stu.name);
printf("Rollno  - %d\t", stu.rollno);
printf("Marks  - %d\n", stu.marks);
}

Program to understand how a pointer to structure is returned from a function

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
char name[20];
int rollno;
int marks;
};
void display(struct student *);
struct student *func( );
struct student *ptr;
main( )
{
struct student *stuptr;
stuptr = func( );
display(stuptr);
free(stuptr);
}
struct student *func( )
{
ptr = (struct student *) malloc(sizeof(struct student) );
strcpy( ptr->name, "Joseph");
ptr->rollno = 15;
ptr->marks = 98;
return ptr;
}
void display( struct student *stuptr)
{
printf("Name  - %s\t", stuptr->name);
printf("Rollno  - %d\t", stuptr->rollno);
printf("Marks  - %d\n", stuptr->marks);
}

Program to understand how a structure variable is returned from a function

#include<stdio.h>
struct student
{
char name[20];
int rollno;
int marks;
};
void display(struct student);
struct student change(struct student stu);
main( )
{
struct student stu1 = {"John", 12 , 87 };
struct student stu2 = {"Mary", 18, 90};
stu1 = change(stu1);
stu2 = change(stu2);
display(stu1);
display(stu2);
}
struct student change(struct student stu)
{
stu.marks = stu.marks + 5;
stu.rollno = stu.rollno - 10;
return stu;
}
void display(struct student stu)
{
printf("Name   - %s\t", stu.name);
printf("Rollno - %d\t", stu.rollno);
printf("Marks  - %d\n", stu.marks);
}

Program to understand how a pointer to structure variable is sent to a function

#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int rollno;
int marks;
};
void display(struct student *);
void inc_marks(struct student *);
main( )
{
struct student stu1 = {"John", 12, 87};
struct student stu2 = {"Mary", 18, 90};
inc_marks(&stu1);
inc_marks(&stu2);
display(&stu1);
display(&stu2);
}
void inc_marks(struct student *stuptr)
{
(stuptr->marks)++;
}   
void display(struct student *stuptr)
{
printf("Name   - %s\t", stuptr->name);
printf("Rollno - %d\t", stuptr->rollno);
printf("Marks  - %d\n", stuptr->marks);
}

Tuesday, 5 March 2013

Program to understand how a structure variable is sent to a function

#include<stdio.h>
struct student
{
char name[20];
int rollno;
int marks;
};
void display(struct student);
main( )
{
struct student stu1 = {"John", 12 , 87 };
struct student stu2 = {"Mary", 18, 90};
display(stu1);
display(stu2);
}
void display( struct student stu)
{
printf("Name   - %s\t", stu.name);
printf("Rollno - %d\t", stu.rollno);
printf("Marks  - %d\n", stu.marks);
}

Program to understand how structure members are sent to a function

#include<stdio.h>
#include<string.h>
struct student
{
char name[20];
int rollno;
int marks;
};
void display(char name[ ], int rollno, int marks);
main( )
{
struct student stu1 = {"John", 12 , 87 };
struct student stu2;
strcpy(stu2.name, "Mary");
stu2.rollno = 18;
stu2.marks = 90;
display(stu1.name, stu1.rollno, stu1.marks);
display(stu2.name, stu2.rollno, stu2.marks);
}
void display(char name[ ], int rollno, int marks)
{
printf("Name   - %s\t", name);
printf("Rollno - %d\t", rollno);
printf("Marks  - %d\n", marks);
}

Program to understand Pointers to Structures

#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int rollno;
int marks;
};
main( )
{
struct student stu = {"Mary", 25, 68};
struct student *ptr = &stu;
printf("Name  - %s\t", ptr->name);
printf("Rollno  - %d\t", ptr->rollno);
printf("Marks  - %d\n", ptr->marks);
getch();
}

Program to understand Arrays within Structures

#include<stdio.h>
struct student
{
char name[20];
int rollno;
int submarks[4];
};
main( )
{
int i, j;
struct student stuarr[3];
for(i=0; i<3; i++)
{
printf("Enter data for student %d\n", i+1);
printf("Enter name : ");
scanf("%s", stuarr[i].name );
printf("Enter roll number : ");
scanf("%d", &stuarr[i].rollno);
for(j=0; j<4; j++)
{
printf("Enter marks for subject %d : ", j+1);
scanf("%d", &stuarr[i].submarks[j] );
}
}
for(i=0; i<3; i++)
{
printf("Data of student %d\n", i+1);
printf("Name : %s, Roll number : %d\nMarks : ", stuarr[i].name, stuarr[i].rollno);
for(j=0; j<4; j++)
printf("%d   ", stuarr[i].submarks[j] );
printf("\n");
}
}

Program to understand Array of Structures

#include<stdio.h>
struct student
{
char name[20];
int rollno;
float marks;
};
main( )
{
int i;
struct student stuarr[10];
for(i=0; i<10; i++)
{
printf("Enter name, rollno and marks  : ");
scanf("%s%d%f", stuarr[i].name, &stuarr[i].rollno, &stuarr[i].marks);
}
for(i=0; i<10; i++)
printf("%s  %d   %f \n", stuarr[i].name, stuarr[i].rollno, stuarr[i].marks);
}

Program to assign a structure variable to another structure variable

#include<stdio.h>
struct student
{
char name[20];
int rollno;
float marks;
};
main( )
{
struct student stu1 = {"Oliver", 12, 98},stu2;
stu2 = stu1;
printf("stu1 : %s   %d   %.2f\n", stu1.name, stu1.rollno, stu1.marks);
printf("stu2 : %s   %d   %.2f\n", stu2.name, stu2.rollno, stu2.marks);
}
 

Program to display the values of structure members

#include<stdio.h>
#include<string.h>
struct student
{
char name[20];
int rollno;
float marks;
};
main( )
{
struct student stu1 = {"Mary", 25, 68};
struct student stu2, stu3;
strcpy(stu2.name, "John");
stu2.rollno = 26;
stu2.marks  =  98;
printf("Enter name, rollno and marks for stu3 : ");
scanf("%s %d %f", stu3.name, &stu3.rollno, &stu3.marks);
printf("stu1 : %s  %d  %.2f\n", stu1.name, stu1.rollno, stu1.marks);
printf("stu2 : %s  %d  %.2f\n", stu2.name, stu2.rollno, stu2.marks);
printf("stu3 : %s  %d  %.2f\n", stu3.name, stu3.rollno, stu3.marks);
}

Program to understand the use of realloc() function

#include<stdio.h>
#include<stdlib.h>
main( )
{
int i, *ptr;
ptr = (int *)malloc(5 *sizeof(int));
if(ptr == NULL)
{
printf("Memory not available\n");
exit(1);
}
printf("Enter 5 integers : ");
for(i=0; i<5; i++)
scanf("%d", ptr+i);
/*Allocate memory for 4 more integers*/
ptr = (int *)realloc(ptr, 9*sizeof(int));
if(ptr == NULL)
{
printf("Memory not available\n");
exit(1);
}
printf("Enter 4 more integers : ");
for(i=5; i<9; i++)
scanf("%d", ptr+i);
for(i=0; i<9; i++)
printf("%d ", *(ptr+i));
}

Program to understand dynamic allocation of memory

#include<stdio.h>
#include<stdlib.h>
main( )
{
int *p, n, i;
printf("Enter the number of integers to be entered : ");
scanf("%d", &n);
p = (int *)malloc(n * sizeof(int));
if(p==NULL)
{
printf("Memory not available\n");
exit(1);
}
for(i=0; i<n; i++)
{
printf("Enter an integer : ");
scanf("%d", p+i);
}
for(i=0; i<n; i++)
printf("%d\t", *(p+i));
}

Array of Pointers

#include<stdio.h>
#include<conio.h>
main( )
{
int *pa[3];
int i, a=5, b=10, c=15;
pa[0] = &a;
pa[1] = &b;
pa[2] = &c;
for(i=0; i<3; i++)
{
printf("pa[%d] =  %p\t",i,pa[i]);
printf("*pa[%d] =  %d\n",i,*pa[i]);
}
getch();
}

When an array is passed to a function, the receiving argument is declared as a pointer

#include<stdio.h>
#include<conio.h>
func(float f[ ] , int *i, char c[5]);
main( )
{
float f_arr[5] ={1.4, 2.5, 3.7, 4.1, 5.9 };
int i_arr[5] = { 1, 2, 3, 4, 5};
char c_arr[5] = {'a', 'b', 'c', 'd', 'e'};
printf("Inside main( )  :   ");
printf("Size of f_arr = %u\t", sizeof(f_arr));
printf("Size of i_arr = %u\t", sizeof(i_arr));
printf("Size of c_arr = %u\n", sizeof(c_arr));
func(f_arr, i_arr, c_arr);
getch();
}    
func(float f[ ], int *i, char c[5])
{
printf("Inside func( )  : ");
printf("Size of f = %u\t", sizeof(f));
printf("Size of i = %u\t", sizeof(i));
printf("Size of c = %u\n", sizeof(c));
}              

Program to show that changes to the array made inside the function affect the original array

#include<stdio.h>
#include<conio.h>
void func(int a[]);
main( )
{
int i, arr[5] = {3, 6, 2, 7, 1};
func(arr);
printf("Inside main( ) : ");
for(i=0; i<5; i++)
printf("%d  ", arr[i]);
printf("\n");
getch();
}
void func(int a[])
{
int i;
printf("Inside func( ) : ");
for(i=0; i<5; i++)
{
a[i] = a[i] + 2;
printf("%d  ",a[i]);
}
printf("\n");
}

Program to show a function that returns pointer

#include<stdio.h>
#include<conio.h>
int *fun(int *p, int n);
main( )
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, n, *ptr;
n = 5;
ptr = fun(arr, n);
printf("arr = %p, ptr = %p, *ptr = %d\n", arr, ptr, *ptr);
getch();
}
int *fun(int *p, int n) 
{
p = p+n;
return  p;
}

Program to show how to return more than one value from a function using call by reference

#include<stdio.h>
#include<conio.h>
func(int x, int y, int *ps, int *pd, int *pp);
main( )
{
int a, b, sum, diff, prod;
a = 6;
b = 4;
func( a, b, &sum, &diff, &prod );
printf("Sum = %d, Difference = %d, Product = %d\n", sum, diff, prod );
getch();
}
func(int x, int y, int *ps, int *pd, int *pp)
{
*ps = x+y;
*pd = x-y; 
*pp = x*y;    
}

CALL BY REFERENCE

#include<stdio.h>
#include<conio.h>
void ref(int *p, int *q);
main()
{
int a = 5;
int b = 8;
printf("Before calling the function, a = %d and b = %d\n", a, b);
ref(&a, &b);
printf("After calling the function, a = %d and b = %d\n", a, b);
getch();
}
void ref(int *p, int *q)
{
(*p)++;
(*q)++;
printf("Inside function *p = %d, *q = %d\n", *p, *q);
}

CALL BY VALUE

#include<stdio.h>
#include<conio.h>
void value(int x, int y);
main( )
{
int a=5, b=8;
printf("Before calling the function, a = %d and b = %d\n", a, b);
value(a, b);
printf("After calling the function, a = %d and b = %d\n", a, b);
getch();
}
void value(int x, int y)
{
x++;
y++;
printf("Inside function x = %d , y = %d\n",x,y);
}