1.5 - Review Structure
1.5 - Review Structure
return 0;
}
•
Nested Structures
We can nest a structure within a structure, within another structure, which is in still another structure
and so on... This is referred to as Nested Structures.
• The following example shows, a Book has a variable of type Author in it
• An author has a variable of type Date in it.
• You can even declare a variable of each of those structure on your own when needed. Like you can
declare a variable for Date, or Author in your code.
• We have used typedef and without typedef structure for this example to show you the difference of
declaring variable. Otherwise, you can choose whichever way you wish.
• Example:
typedef struct Date
{
int day;
int month;
int year;
}Date;
struct Author{
char authorName[50];
char authorLanguage [50];
Date dob; //Date was typedef so no struct
};
typedef struct Book {
char title[50];
struct Author author; //author is a strucute variable
float price;
int pages;
}Book;
int main( ) {
Example of nested structure Book b1;
typedef struct Date char x;
{ printf("Enter book title: ");
int day; scanf("%[^\n]s", b1.title);
int month; while((x=getchar() != '\n') && x != EOF);
int year; //clearing up the \n from the last input
}Date; printf("Enter author name: ");
scanf("%[^\n]s", b1.author.authorName);
struct Author{ while((x=getchar() != '\n') && x != EOF);
char authorName[50]; //clearing up the \n from the last input
char authorLanguage [50]; printf("Enter author Language: ");
Date dob; //Date was typedef so no scanf("%[^\n]s", b1.author. authorLanguage);
struct printf("Enter price: ");
}; scanf("%f", &b1.price);
typedef struct Book { printf("Enter total pages for b1: ");
char title[50]; scanf("%d", &b1.pages);
struct Author author; //author is
a strucute variable printf("Enter day month and year of birth of
float price; author: ");
int pages; scanf("%d%d%d", &b1.author.dob.day,
}Book; &b1.author.dob.month, &b1.author.dob.year);
return 0; }
Array of Structures
• Let’s consider a case where we want to store data of
100 books. We would be required to use 100
different structure variables from b1 to b100, which
is definitely not very convenient.
• A better approach would be to use an array of
structures. books[0] title, author, price, pages
• Example: books[1] title, author, price, pages
typedef struct Book { books[2] title, author, price, pages
char title[50];
books[3] title, author, price, pages
char author[50];
float price; ……
int pages;
……
}Book;
Books[99] title, author, price, pages
Book books[100];
main(){
struct book b1={“Programming knights”,”Arup
Guha”,50};
Here, we are passing our
display(b1); structure variable.
} Note: we are not passing
reerence
Struct and Pointer
struct employee {
char[20] name;
double salary;
int empID;
};
• Now, to declare a variable of this declared type, we write:
struct employee officeworker;
• Often times, for efficiency purposes, it's easier to declare pointers to structs and use those to
manipulate the structures formed.
• Consider the following:
struct employee *temp;// declare a pointer of type employee
temp = &officeworker; //store the address of office worker to temp
(*temp).salary = 50000; //dereference temp to access salary of officeworker
• Since expressions similar to the last one are used so often, there is a shorthand to access a
field/member of a record through a pointer to that record. The last statement can also be
written as:
temp->salary = 50000 // -> is mostly used to dereference structure pointer.
• Another reason to use a pointer to a struct is to dynamically allocate the memory for the
struct. (We will briefly discuss about dynamic memory allocation in our next class).
• This allows the memory to be allocated beyond the “life”/”scope” of the function within
which it was declared. 23
Pointers and Structures
temp
typedef struct bookRec{
float price;
char name[7];
}Book;
main() {
Book temp;
24
Pointers and Structures
aPtr
temp
typedef struct bookRec{
float price;
char name[7];
}Book;
main()
{
Book *aPtr;
Book temp;
aPtr = &temp;
scanf("%d %s", &(aPtr->price), aPtr->name);
}
25
More example on accessing Structure Variables
main(){
struct book b1={“Programming knights”,”Arup
Guha”,50};
Here, we are passing our
display(&b1); structure variable.
} Note: we are not passing
reerence
Returning a structure from a function
struct point{
int x;
This function returns a point
int y;
type data
};
main(){
struct point p1, p2;
p1 = get();//calling get() function and storing the result in p1
p2 = get();
}
Self-Referential Structures
• It is sometimes desirable to include within a
structure one member that is a pointer to the parent
structure type. Such structures are known as self-
referential structures.
• Example:
struct emp{
char name[40];
struct emp *next;
};
• This is a structure of type emp.
• The structure contains two members:
– 40 element character array, called name,
– and a pointer to a structure of the same type (i.e., a pointer to a structure of type emp),
called next.
• Therefore, this is a self-referential structure.
Self-Referential Structures
• Self-referential structures are very useful in applications that
involve linked data structures, such as lists and trees.
• The basic idea of a linked data structure is that each
component within the structure includes a pointer indicating
where the next component can be found. Therefore, the
relative order of the components can easily be changed
simply by altering the pointers. In addition, individual
components can easily be added or deleted, again by altering
the pointers.
• An example of linked data structure is shown bellow:
Additional notes on Structure before
you start doing the exercise
• The Structure members are stored together in the memory
one after another according to the sequence of their
declaration inside the structure.
• The best way to know the size of your structure is using the
sizeof() function. Just adding the variable sizes inside the
structure is not enough to know the size as the operating
system can add additional memory for the structure to
efficiently access it.
• We can assign a struct variable to another variable of the
same type. When we assign a struct variable to another, all
members of the variable are copied to the other struct
variable. Even if you have an array inside your structure, the
assigned structure will have its own copy of the array.
• For example:
• book b1={“Programming knights”,”Arup Guha”,125};
• If you write,
book b2 = b1; //it will copy the data from b1 and will
maintain a separate copy of the array and other members in
b2.