You are on page 1of 25

STRUCTURES

• Structure is a collection of variables (can be of different types) under


a single name.
• Keyword struct is used for creating a structure.

struct Person
{
char name[50];
int age;
float salary;
};
Create structure variable
struct Person
struct Person
{
{
char name[50];
char name[50];
int age;
int age;
float salary;
float salary;
};
} person1, person2, p[20];
int main()
{
struct Person person1, person2, p[20];
return 0;
}
Keyword typedef

• Keyword typedef can be used to simplify syntax of a structure.

struct Distance{ typedef struct Distance{


int feet; int feet;
float inch;
float inch;
} distances;
};
int main() {
int main() { distances dist1, dist2, sum;
struct Distance d1, d2; }
}
How to Access members of a structure?

• There are two types of operators used for accessing members of a


structure.
• Member operator(.)
• Structure pointer operator(->)
• Suppose, you want to access salary of person2. Here's how you can
do it:
• person2.salary
Initialize Structrue
• We can initialize pt by pt.x=10.0; pt.y=2.0;

• Another way to initialize pt is point pt={10.0, 2.0}; In this case, x gets


the first value and y gets the second.
Nested Structures

• You can create structures within a structure in C programming. For


example:
struct complex Suppose, you want to set imag of num2
{ variable to 11. Here's how you can do it:
int imag;
float real; num2.comp.imag = 11;
};

struct number
{
struct complex comp;
int integers;
} num1, num2;
Structures & Pointers
• Structures can be accessed using pointers. Here's how:

In this example, the address of person1 is stored


in personPtr pointer variable using code
personPtr = &person1;.

Now, you can access members of person1 using


personPtr pointer. For that we use -> operator.
• personPtr->age is equivalent to (*personPtr).age
• personPtr->weight is equivalent to (*personPtr).weight
Pass structure to a function
Breakdown of scanf("%*[^\n]%*c"):

%[^\n] scans everything until a \n, but doesn't scan in the \n.
%*c scans a single character, which will be the \n left over by %*[^\n] in this case. The
asterisk instructs scanf to discard the scanned character.

Here, a structure variable s1 of type struct student is


created. This structure variable is passed to the
display() function using display(s1); statement.
Returning structure from a function

Here, getInformation() function is called using s = getInformation();


statement.
The function returns a structure of type struct student.
The returned structure is displayed from the main() function.

Notice that, the return type of getInformation() is struct student.


Passing structure by
reference
• In the above program, three structure
variables c1, c2 and the address of result is
passed to the addNumbers() function. Here,
result variable is passed by reference.
• When result variable inside the addNumbers()
is altered, result variable inside the main()
function is also altered accordingly.
• Problem 1. Provide an implementation of a function POINTdist() that
computes the Euclidean distance between two Points.
typedef struct {
double x;
double y; } Point;
Problem 2.
• Define a type Rect for rectangles that are parallel to the axes in a
Cartesian coordinate system. Represent a rectangle by its lower left and
upper right endpoints using the Point type above.
a. Write a function RECTarea() that computes the area of a rectangle.
b. Write a function that returns 1 if a point falls within a rectangle, 0 otherwise.
c. Write a function that returns 1 if the first rectangle is completely contained
inside the second rectangle, and 0 other-wise. Hint: check if the lower left and upper
right endpoints of the first rectangle fall within the second rectangle.
int RECTintersectsRect(Rect r1, Rect r2) {
return POINTinRect(r1.upperleft, r2) ||
POINTinRect(r1.lowerright, r2) ||
POINTinRect(r2.upperleft, r1) ||
POINTinRect(r2.lowerright, r1);
}
• Problem 3. Write a program that reads in a list of points (given by
their x and y coordinates) and determines the pair that is the farthest
apart. Hint: store them in an array and use the POINTdist() function.
• Problem 4. The code below makes use of square brackets and the
arrow (->) operator. Rewrite it in terms of only dereferencing (*) and
pointer addition (+).
Problem 5. Consider the following C
program.
• Predict the result of each of the printf
lines. (Some may cause compiler errors.)
• b. List three different ways you could fill
in the blank in the statement below to
print the value 781 by extracting it from
• the data structures given in the above
program.
• printf("%d\n", ______ );
• Do the same for the values 77 and 87.
SELFCHECK 
• https://sites.cs.ucsb.edu/~
kyledewey/cs24/exams/exam1/exam1reviewsolutions.html

• https://www.cs.princeton.edu/courses/archive/spring2000/cs126/exe
rcises/structs.txt

You might also like