100% found this document useful (1 vote)
134 views

Structures in C

The code copies the structure s to m by the assignment m = s. So both s and m will point to the same string "st". So the output will be st st.

Uploaded by

Garima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
134 views

Structures in C

The code copies the structure s to m by the assignment m = s. So both s and m will point to the same string "st". So the output will be st st.

Uploaded by

Garima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Structures in C

Structures

A structure is a user defined data type in C/C++. A structure


creates a data type that can be used to group items of possibly
different types into a single type.
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
Example

struct employee
{ int id;
char name[10];
float salary;
};
Declaring a structure variable

We can declare a variable for the structure so that we can


access the member of the structure easily. There are two ways
to declare structure variable:
By struct keyword within main() function
By declaring a variable at the time of defining the structure.
By struct keyword within main() function

struct employee
{ int id;
char name[50];
float salary;
};

Now write given code inside the main() function.


struct employee e1, e2;

The variables e1 and e2 can be used to access the values stored in the
structure.
By declaring a variable at the time of
defining the structure.

struct employee
{ int id;
char name[50];
float salary;
}e1,e2;
Which approach is good?

If number of variables are not fixed, use the 1st approach. It


provides you the flexibility to declare the structure variable
many times.
If no. of variables are fixed, use 2nd approach. It saves your
code to declare a variable in main() function.
How to initialize structure members?

Structure members can be initialized using curly braces ‘{}’. For


example, following is a valid initialization.
struct Point
{
int x, y;
};

int main()
{
// A valid initialization. member x gets value 0 and y
// gets value 1. The order of declaration is followed.
struct Point p1 = {0, 1};
}
How to access structure elements?

Structure members are accessed using dot (.) operator.


#include<stdio.h>

struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
// Accessing members of point p1
p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y);
return 0;
}
How to access structure elements?

Structure members are accessed using dot (.) operator.


#include<stdio.h>

struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
// Accessing members of point p1
p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y);
return 0;
}
Output:x = 20, y = 1
Designated Initialization

Designated Initialization allows structure members to be initialized in any orderThis


feature is not available in C++ and works only in C.
#include<stdio.h>
struct Point
{
int x, y, z;
};
int main()
{
// Examples of initialization using designated initialization
struct Point p1 = {.y = 0, .z = 1, .x = 2};
struct Point p2 = {.x = 20};

printf ("x = %d, y = %d, z = %d\n", p1.x, p1.y, p1.z);


printf ("x = %d", p2.x);
return 0;
}
Designated Initialization

#include<stdio.h>
struct Point
{
int x, y, z;
};
int main()
{
// Examples of initialization using designated initialization
struct Point p1 = {.y = 0, .z = 1, .x = 2};
struct Point p2 = {.x = 20};

printf ("x = %d, y = %d, z = %d\n", p1.x, p1.y, p1.z);


printf ("x = %d", p2.x);
return 0;
}
Output: x = 2, y = 0, z = 1
x = 20
Array of structures

#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
// Create an array of structures
struct Point arr[10];
// Access array members
arr[0].x = 10;
arr[0].y = 20;
printf("%d %d", arr[0].x, arr[0].y);
return 0;
}
Output:10 20
Structure pointer

If we have a pointer to structure, members are accessed using arrow ( -> ) operator.
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {1, 2};
// p2 is a pointer to structure p1
struct Point *p2 = &p1;
// Accessing structure members using structure pointer
printf("%d %d", p2->x, p2->y);
return 0;
}
Output:?
Structure pointer

If we have a pointer to structure, members are accessed using arrow ( -> ) operator.
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {1, 2};
// p2 is a pointer to structure p1
struct Point *p2 = &p1;
// Accessing structure members using structure pointer
printf("%d %d", p2->x, p2->y);
return 0;
}
Output:1 2
Question

What is the size of a C structure.?


A) C structure is always 128 bytes.
B) Size of C structure is the total bytes of all elements of
structure.
C) Size of C structure is the size of largest element.
D) None of the above
Question

What is the size of a C structure.?


A) C structure is always 128 bytes.
B) Size of C structure is the total bytes of all elements of
structure.
C) Size of C structure is the size of largest element.
D) None of the above

Answer: B
Explanation: Individually calculate the sizes of each member
of a structure and make a total to get Full size of a structure.
Question

What is the output of C program with structures.?


int main()
{
structure hotel
{
int items;
char name[10];
}
a; strcpy(a.name, "TAJ");
a.items=10;
printf("%s", a.name);
return 0;
}
A) TAJ
B) Empty string
C) Compiler error
D) None of the above
Question

What is the output of C program with structures.?


int main()
{
structure hotel
{
int items;
char name[10];
}
a; strcpy(a.name, "TAJ");
a.items=10;
printf("%s", a.name);
return 0;
}
Answer: C
Explanation: Keyword used to declare a structure is STRUCT not structURE in
lowercase i.e struct.
Question

What is the output of C program.?

A) empty string=10
B) C=basics
C) Cbasics=10
D) Compiler error
Question

What is the output of C program.?

A) empty string=10
B) C=basics
C) Cbasics=10
D) Compiler error

Answer: c
Explanation: pages and name are structure members. a is a structure
variable. To refer structure members use a DOT operator say a.name.
Question

struct node
{
int i;
float j;
};
struct node *s[10];

The above C declaration define ‘s’ to be


(A) An array, each element of which is a pointer to a structure of type node
(B) A structure of 2 fields, each field being a pointer to an array of 10 elements
(C) A structure of 3 fields: an integer, a float, and an array of 10 elements
(D) An array, each element of which is a structure of type node.

.
Question

struct node
{
int i;
float j;
};
struct node *s[10];

The above C declaration define ‘s’ to be


(A) An array, each element of which is a pointer to a structure of type node
(B) A structure of 2 fields, each field being a pointer to an array of 10 elements
(C) A structure of 3 fields: an integer, a float, and an array of 10 elements
(D) An array, each element of which is a structure of type node.

Answer: (A)

.
Question

What will be the output of the following C code?

#include <stdio.h>
struct student
{
char *name;
};
void main()
{
struct student s, m;
s.name = "st";
m = s;
printf("%s%s", s.name, m.name);
}
a) Compile time error
b) Nothing
c) Junk values
d) st st
Question

What will be the output of the following C code?

#include <stdio.h>
struct student
{
char *name;
};
void main()
{
struct student s, m;
s.name = "st";
m = s;
printf("%s%s", s.name, m.name);
}
a) Compile time error
b) Nothing
c) Junk values
d) st st
Answer: d

You might also like