EXERCISE-9
1. Write a C Program to Store Information Using Structures with Dynamically Memory Allocation
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct course{
char subject[30];
int marks;
};
int main(){
struct course *ptr;
int i, noOfRecords;
printf("Enter number of records : ");
scanf("%d", &noOfRecords);
ptr = (struct course*)malloc(sizeof(struct course)*noOfRecords);
for(i=0;i<noOfRecords;i++){
printf("Enter the subject name and marks : ");
scanf("%s%d",ptr[i].subject,&ptr[i].marks);
}
printf("Subject\tMarks\n");
for(i=0;i<noOfRecords;i++){
printf("%s\t%d\n",ptr[i].subject,ptr[i].marks);
}
}
OUTPUT:
Enter number of records : 2
Enter the subject name and marks : ENG 79
Enter the subject name and marks : CP 65
Subject Marks
ENG 79
CP 65
2. Write a program in C to demonstrate how to handle the pointers in the program.
SOURCE CODE:
#include<stdio.h>
int main(){
int a = 9, *intptr;
float pi = 3.14, *floatptr;
char ch = 'I', *charptr;
intptr = &a;
floatptr = π
charptr = &ch;
printf("Size of an int ptr = %d\n",sizeof(intptr));
printf("Size of a float ptr = %d\n",sizeof(floatptr));
printf("Size of a char ptr = %d\n",sizeof(charptr));
printf("Size of an int type = %d\n",sizeof(*intptr));
printf("Size of a float type = %d\n",sizeof(*floatptr));
printf("Size of a char type = %d\n",sizeof(*charptr));
return 0;
}
OUTPUT:
Size of an int ptr = 4
Size of a float ptr = 4
Size of a char ptr = 4
Size of an int type = 4
Size of a float type = 4
Size of a char type = 1
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct course{
char subject[30];
int marks;
};
int main(){
struct course *ptr;
int i, noOfRecords;
printf("Enter number of records : ");
scanf("%d", &noOfRecords);
ptr = (struct course*)malloc(sizeof(struct course)*noOfRecords);
for(i=0;i<noOfRecords;i++){
printf("Enter the subject name and marks : ");
scanf("%s%d",ptr[i].subject,&ptr[i].marks);
}
printf("Subject\tMarks\n");
for(i=0;i<noOfRecords;i++){
printf("%s\t%d\n",ptr[i].subject,ptr[i].marks);
}
}
OUTPUT:
Enter number of records : 2
Enter the subject name and marks : ENG 79
Enter the subject name and marks : CP 65
Subject Marks
ENG 79
CP 65
2. Write a program in C to demonstrate how to handle the pointers in the program.
SOURCE CODE:
#include<stdio.h>
int main(){
int a = 9, *intptr;
float pi = 3.14, *floatptr;
char ch = 'I', *charptr;
intptr = &a;
floatptr = π
charptr = &ch;
printf("Size of an int ptr = %d\n",sizeof(intptr));
printf("Size of a float ptr = %d\n",sizeof(floatptr));
printf("Size of a char ptr = %d\n",sizeof(charptr));
printf("Size of an int type = %d\n",sizeof(*intptr));
printf("Size of a float type = %d\n",sizeof(*floatptr));
printf("Size of a char type = %d\n",sizeof(*charptr));
return 0;
}
OUTPUT:
Size of an int ptr = 4
Size of a float ptr = 4
Size of a char ptr = 4
Size of an int type = 4
Size of a float type = 4
Size of a char type = 1
No comments