Structures in C
Structures in C
Structures
struct employee
{ int id;
char name[10];
float salary;
};
Declaring a structure variable
struct employee
{ int id;
char name[50];
float salary;
};
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?
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?
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?
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
#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};
#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
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
A) empty string=10
B) C=basics
C) Cbasics=10
D) Compiler error
Question
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];
.
Question
struct node
{
int i;
float j;
};
struct node *s[10];
Answer: (A)
.
Question
#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
#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