What Is Structure? Explain C Syntax of Structure Declaration With Example

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Module 5

1. What is structure? Explain C syntax of structure declaration with example.


 structure is a collection of elements of different data types.
 Declaration of structure:

Syntax:
struct struct_name
{
data_type1 member1;
data_type2 member2;
…………………………………..
data_typen membern;
};

 struct is a keyword
 strut_name is the name of the structure which is userdefined
 datatype1,datatyp2,…..datatypen are the basic data types
 member1,member2,…..membern are the different data items

Example:
struct student
{
int roll_num;
char name[20];
float marks;
};
Declaration of structure variable:
Syntax: struct struct_name structvar_list;
Example: struct student stud1,stud2;

2. Explain structure within a structure with an example.


 Structure written inside another structure is called as nesting of structure
 Structure can be a member of another structure

Example 1: Example 2:
struct date struct employee
{ {
int date; char name[20;
int month; float salary;
int year; struct date
}; {
struct employee int date;
{ int month;
char name[20]; int year;
float salary; } doj;
struct date doj; }emp1;
} emp1;

 Accessing nested elements:

emp1.doj.date;
emp1.doj.month;

3. Write a c-program using structures to read, write, compute average - marks and display the students
scoring above and below the average marks for a class of N students.
/* Program to read, write, compute average- marks and the students scoring above and below the average
marks for a class of N students using structures */
#include<stdio.h>
struct student
{
int roll;
char name[50];
int marks;
};
void main()
{
struct student s[10];
int i,n,sum=0;
float average=0.0;
printf("\nEnter the number of students:");
scanf("%d",&n);
// Reading details of N students
for(i=0;i<n;i++)
{
printf("Enter the details of student %d\n",i+1);
printf("Enter the roll number:");
scanf("%d",&s[i].roll);
printf("Enter the Name:");
gets(s[i].name);
printf("Enter Total Marks:");
scanf("%d",&s[i].marks);
printf("\n");
}
// compute sum and average marks
for(i=0;i<n;i++)
{
sum=sum+s[i].marks;
}
average=(float)sum/n;
// print the details of N students
printf("Class Average is %g\n",average);
printf("-----------------------------------------------------------------------------------\n");
printf("Record details of students:\n");
printf("\nRollNo\t Student Name\t Total_Marks\t Above_Average(Y/N) \n");
printf("---------------------------------------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("%4d",s[i].roll);
printf("%15s",s[i].name);
printf("%15d",s[i].marks);
if(s[i].marks>=average)
printf("\tYes");
else
printf("\tNo");
printf("\n");
}
}
Output:
4. What is a pointer? Explain how the pointer variable declared and initialized.
 A pointer is a variable that contains an address of another variable.
 Declaring a pointer variable:
Syntax:
data_type *pt_name;
 The (*) asterisk tells that the variable pt_name is a pointer variable.
 Pt_name needs memory location
 Pt_name points to a variable of type data_type

Example:
int *p; // declares the variable p as a pointer that points to an integer data type.
 Initializing the pointer variable:
 Declare a data variable
 Declare a pointer variable
 Assign the address of a variable to pointer variable using & operator and assignment operator
Syntax:
pointer_var=&data_var;
Examples:
int x; //declare a data variable
int *px; //declare a pointer variable
px=&x // copy the address of data variable to pointer variable

int x; //declare a data variable


int *px=&x; //assign the address of data variable to pointer variable

int x,*px=&x; //declare data variable and assign address

5. Write a program in C to find the sum and mean of all elements in an array using pointers.
/* program to compute the sum, mean and standard deviation of all elements stored in an array of n real
numbers using pointers */

#include<stdio.h>
#include<math.h>
void main()
{
float a[10],*ptr, mean, std, sum=0,sumstd=0;
int n, i;
printf("Enter the no. of elements\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for(i=0; i<n; i++) // read n real numbers and store in the array a
{
scanf("%f",&a[i]);
}
ptr=a; // assign the base address of array a to pointer variable ptr
for(i=0; i<n; i++) // compute sum
{
sum=sum+*ptr;
ptr++;
}
mean=sum/n; // compute mean
ptr=a;
for(i=0; i<n; i++) // compute standard deviation
{
sumstd=sumstd+pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf("Sum=%.3f\t",sum);
printf("Mean=%.3f\t",mean);
printf("Standard Deviation=%.3f\t",std);
}
Output

6. Explain different categories of pre-processor directives used in C.

 preprocessor directives in C can be divided into three categories:

1. Macro substitution directives


2. File inclusion directives
3. Compiler control directives

1. Macro substitution directives

- Macro substitution is a process where an identifier in a program is replaced by a predefined string


composed of one or more tokens.
- General form:
#define identifier string
- string may be any text, while the identifier must be a valid C name
- If this statement is included at the beginning of the program, then the preprocessor replaces every
occurrence of the identifier by the string
- Example:
#define COUNT 100
#define AREA 5*12.46

2. File inclusion directives


 An external file containing functions or macro definitions can be included as a program so that
we need not rewrite those functions or macro definitions

- General form:
#include “filename” or #include<filename>
where filename is the name of the file containing the required definitions or functions.

 When the filename is included within the double quotes, the search for the file is made first in the
current directory and then in the standard directories
 When the file name is included with angular brackets, the file is searched only in the standard
directories
 If an included files is not found, an error is reported and compilation is terminated
 Example:
#include<stdio.h>
#include “test.c”

3. Compiler control directives


 These directives allow the programmer to include the portions of the codes based on the conditions
 The most frequently used conditional compilation directives are :

#if
#else
#endif

 Example:
#if((9%2)==0)

printf(“number is even”);
#else output: number is odd
printf(“number is odd”);
#endif
7. Explain how the structure variable can be passed as a parameter to a function with an example.
 There are 3 methods of passing structure to a function
1. pass each member of the structure as an actual argument of the function call
 this is the most elementary method and becomes unmanageable and inefficient when the
structure size is large
 Example:

void display(char,int);
struct student
{
char name[20];
int usn;
};
void main() void display(char name[20],int usn)
{ {
struct student s1; printf(“Name:%s\n”,name);
printf(“enter name & usn:”); printf(“USN:%d”,usn);
scanf(“%s%d”,s1.name,&s1.usn); }
display(s1.name,s1.usn);

}
2. Passing a copy of entire structure
 The called function works on the copy of the structure, any changes to structure members
within the function are not reflected in the original structure
 All compilers may not support this method
 Example:
void display(struct student s);

struct student
{
char name[20];
int usn;
};
void main() void display(struct student s1)
{ {
struct student s1; printf(“Name:%s\n”,s1.name);
printf(“enter name & usn:”); printf(“USN:%d”,s1.usn);
scanf(“%s%d”,s1.name,&s1.usn); }
display(s1);
}
3. Pass the address location of the structure to the called function
 The function can access indirectly the entire structure and work on it
 This method is more efficient as compared to the second one
 Example:
void display(struct student *s);
struct student

{
char name[20];
float marks;
};
void main() void display(struct student *r)
{ {
struct student st; printf(“Name:%s”,r->name);
strcpy(st.name,”Raju”); printf(“marks:%f”,r->marks);
st.marks=45; }
display(&st);
}

8. Define pointer. Explain the declaration &initialization of pointer.


Refer question number 4
9. Write a ‘C’ Program to maintain a record of student details. Print the marks of the student given student
name as input using structures.
#include <stdio.h>
#include <string.h>
struct student
{
char name[50];
int roll;
int marks;
char grade;
};
void main()
{
struct student s[10];
int i,flag=0,n;
char dname[50];
printf("Enter the no: of students");
scanf("%d",&n);
printf("Enter information of students:\n");
for(i=0;i<n;i++)
{
printf("\nEnter the roll number ");
scanf("%d",&s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%d",&s[i].marks);
printf("Enter the Grade(A,B,C)");
scanf("%c",&s[i].grade);
}
printf("Enter the name of student whose details need to be displayed");
scanf("%s",dname);
for(i=0;i<n;i++)
{
if(strcmp(s[i].name,dname)==0)
{
printf("The Details are:\n");
printf("\nRoll number: %d",s[i].roll);
printf("\nName: ");
puts(s[i].name);
printf("\nMarks: %d",s[i].marks);
printf("\nGrade: %c",s[i].grade);
flag=1;
break;
}
}
if(flag==0)
printf("The student details are not found");
}
Output:
Enter the no: of students: 2
Enter information of students:
Enter the roll number: 1
Enter name: Mohit
Enter marks: 85
Enter the Grade(A,B,C) :A
Enter the roll number : 2
Enter name: Rahul
Enter marks: 75
Enter the Grade(A,B,C) :B
Enter the name of student whose details need to be displayed:
Rahul
The Details are:
Roll number: 2
Name: Rahul
Marks: 75
Grade: B

10. Explain the array of pointers with examples.


 we can also declare an array of pointers using the following the syntax:
datatype *array_name[size];
 Example:
int *arrop[5];
- Here arrop is an array of 5 integer pointers. It means that this array can hold the address of 5
integer variables. In other words, you can assign 5 pointer variables of type pointer to int to the
elements of this array.
 Example program:
Output

11. What are pre-processor directives? Explain #define & #include pre-processor directives.
 Preprocessor is a program that processes the source code before it passes through the compiler
 Preprocessor directives are placed in the source program before the main line
 Before the source code passes through the compiler, it is examined by the preprocessor for any
preprocessor directives
 Preprocessor directives begin with the symbol # in column one and do not require a semicolon at
the end.
 #define:
General form: #define identifier string
- string may be any text, while the identifier must be a valid C name
If this statement is included at the beginning of the program, then the preprocessor replaces
every occurrence of the identifier by the sting
Example: #define PI 3.142

#define AREA 5*12.46

 #include:
Used to include an external file containing functions or macro definitions
General format: #include “filename”
where filename is the name of the file containing the required definitions or functions.
This directive can take the form #include<filename> without double quotation marks
If an included files is not found, an error is reported and compilation is terminated
Example: #include<stdio.h>
#include “a.c”

12. Write a ‘C’ Program using pointers to compute the sum, mean & standard derivation of all elements
stored in an array of ‘n’ real numbers.
Refer question number 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy