Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views64 pages

Structures

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views64 pages

Structures

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Structures

Accessing Members of a struct (continued)

• Let s es?
nth e
struct motor *p; ar e
th ep
• Then h y
W
(*p).volts — is the voltage of the motor pointed
to by p
(*p).phases — is the number of phases of the
motor pointed to by p

Structures, Unions, and Typedefs CS-2303, C-Term 2010 35


Accessing Members of a struct (continued)

er ator
• Let ' op nce
struct motor *p; u s e ' .
r ecede
a
Bec igher p *'
• Then h '
has unary
(*p).volts an voltage of the motor pointed
— isththe
to by p
(*p).phases — is the number of phases of the
motor pointed to by p

Structures, Unions, and Typedefs CS-2303, C-Term 2010 36


The typedef is a keyword used in C programming to provide some meaningful names to
the already existing variable in the C program. It behaves similarly as we define the alias
for the commands. In short, we can say that this keyword is used to redefine the name of an
already existing variable.

typedef <existing_name> <alias_name>

typedef unsigned int unit;


unit a, b;

instead of writing:

unsigned int a, b;
1.struct student
2.{
3.char name[20];
4.int age;
5.};
6.struct student s1;

1.struct student 1.typedef struct student 1.typedef struct


2.{ 2.{ 2.{
3.char name[20]; OR 3.char name[20];
OR 3.char name[20];
4.int age; 4.int age; 4.int age;
5.}; 5.} stud; 5.} stud;
6.typedef struct student stud; 6.stud s1,s2; 6.stud s1,s2;
7.stud s1, s2;
1.#include <stdio.h>
2.typedef struct student
3.{ 1.int main()
4.char name[20]; 2.{
5.int age; 3.stud s1;
6.}stud; 4.printf("Enter the details of student s1: ");
5.printf("\nEnter the name of the student:");
6.scanf("%s",&s1.name);
7.printf("\nEnter the age of student:");
8.scanf("%d",&s1.age);
9.printf("\n Name of the student is : %s", s1.name);
10.printf("\n Age of the student is : %d", s1.age);
11.return 0;
12.}
//A program to find ave salary of n employees
int main()
#include <stdio.h> {
struct employee{ int n;
char name[20]; printf("\nEnter the number of employees: ");
scanf("%d",&n);
char address[100]; struct employee e[n];
int age; int i ;
float salary; float total=0;
for(i=0;i<n;i++)
}; {
printf("\n Enter name, address, age and salary for %d
employee: ",n);
scanf("%s%s%d%f",e[i].name,e[i].address,&e[i].age,
&e[i].salary);
total=total+e[i].salary;
}
printf("\n The ave salary is : %0.2f", (total/n));

return 0;
}
//A program to sort student record for(i=0;i<N-1;i++)
#define N 5 {
#include <stdio.h> for(j=0;j<N-1-i;j++)
struct student{ {
char name[20]; if(s[j+1].roll<s[j].roll)
int roll; {
}; temp=s[j];
void main() s[j]=s[j+1];
{ s[j+1]=temp;
struct student s[N], temp; }
int i,j; }
for(i=0;i<N;i++) }
{ printf("\n The sorted record is: \n");
printf("\n Enter name, and roll no. for 5 students: "); for(i=0;i<N;i++)
scanf("%s%d",s[i].name,&s[i].roll); {
} printf("\n %s\t%d",s[i].name,s[i].roll );
} }
//A program regarding DMA using pointers to for(i=0;i<5;i++)
structures
{
#include <stdio.h>
printf("\n Enter name, roll no.and marks for %d students:
#include <stdlib.h> ",(i+1));
struct student{ scanf("%s%d%f",(s+i)->name,&(s+i)->roll, &(s+i)->C_marks);
char name[20]; }
int roll;
float C_marks; printf("\n The entered record is: \n");
}; for(i=0;i<5;i++)
{
int main() printf("\n %s\t%d\t%f",(s+i)->name,(s+i)->roll,(s+i)->C_marks
{ );
struct student *s; }
int i; return 0;
s=(struct student*)malloc(5*sizeof(struct student)); }
1. Write a program to initialize and print the roll no., name , age
and marks of a student using structures.

2. Write a program to input from user and print the roll no., name ,
age and marks of a student using structures.
1. Write a program to store and print the employee, name, age
and address of 15 students using structure (use array).

2. Write a program to compare two dates entered by user. Make


a structure named Date to store the elements day, month and
year to store the dates. If the dates are equal, display "Dates
are equal" otherwise display "Dates are not equal".
1. Write a program as follows:(i). Define a structure called
Complex with data members float x and float y
(ii) Read the structure elements and display the output in
the format Complex number = “x+iy”
(iii) Extend it t display a table of 10 complex numbers

1. Declare a pointer of struct type i.e struct student *ptr. (ii)


Assign the address of the struct variable to this pointer. (iii)
Access the structure members using this pointer.

2. Arrange student record in descending order


What is the output of this program? #include
<stdio.h>
#include<stdlib.h>
int main()
{
struct test {
int i; float f; char c;
};
struct test *ptr;
ptr = (struct test *)malloc(sizeof(struct test)); ptr
->f = 2.5f;
printf("%f", ptr->f);
return 0;
}
a. Compilation error b. Garbage value
*c. 2.500000 d. 0.000000
Consider the following code snippet. struct employee {
char name[30];
int gender; };
void main() {
struct employee e = {“Ram Kumar”,1}, *ptr; ptr = &e;
}
*(ptr -> name +2) prints the character ‘m’ from “Ram Kumar”.
Which one of the following option can be used to print ‘m’ instead
of *(ptr->name+2)?

a. e.name+2
b. ptr->name+2
*c. *((*ptr).name + 2)
d. either option a or option b, but not option c
Which one of the following is never possible in C when members in a
structure are same as that in a union? Assume that X is the structure and
Y is the union.

a. sizeof(X) is equal to sizeof(Y)


*b. sizeof(X) is greater than sizeof(Y)
c. sizeof(X) is less than to sizeof(Q)
d. None of the above

main()
{ enum result {pass, fail};
enum result s1,s2;
s1=pass;
s2=fail; printf("%d",s1); }

In the given code, can pass and fail the components of the enum result be initialised to
the same integer constant? *a. Yes b. No
What is the output of C program with
structures?
int main()
{
struct tree What is the output of following C program with structure
{ arrays? int main()
int h, int rate; {
}; struct pens
strcut tree tree1={0}; {
printf(“%d”, tree1.rate); int color;
printf(“%d”, tree1.h); }p1[2];
return 0; struct pens p2[3];
} p1[0].color=5;
*a. 0 0 p1[1].color=9;
b.-1 -1 printf(“%d”,p1[0].color);
c. NULL NULL printf(“%d”,p1[1].color);
d. Compiler error return 0;
}
a. 5 5 *b. 5 9 c. 9 5
d. Compiler error
# define CUBE(x) ----------
main()
{
float y= CUBE(3); printf(“%f ”,y);}
Fill in the blanks such that the output is 27.0?
a. 3*3
b.x*x
*c. x*x*x

Is the following structure definition correct?


struct sample
{ int a=5;
float f;
char name[10];
};
a. Correct
*b. Not correct
What is the output of this program?
#include <stdio.h>
#include<stdlib.h>
int main()
{
struct test {
int i; float f; char c;
};
struct test *ptr;
ptr = (struct test *)malloc(sizeof(struct test)); ptr ->f = 2.5f;
printf("%f", ptr->f);
return 0;
}

a. Compilation error b. Garbage value *c. 2.500000


d. 0.000000
What is the output of C program with structures? int
main()
{
struct tree
{
int h, int rate;
};
strcut tree tree1={0};
printf(“%d”, tree1.rate);
printf(“%d”, tree1.h);
return 0;
}
*a. 0 0
b.-1 -1
c. NULL NULL
d. Compiler error

You might also like