1 / 22

Concepts of Structure in C Programming

Learn about the concepts of structures in C programming, including pointers of structures, structure arrays, and passing structures to functions.

iglen
Download Presentation

Concepts of Structure in C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 9 Structure 1. Concepts of structure • Pointers of structures • Structure arrays • Pass structure to functions

  2. 1. Concepts of structure • Structure is a user-defined data type that can store a sequence of various data types together • Different from array, array has to be the same data type • Similar to array, data of a structure are stored in consecutive memory space • A structure is declared using the keyword struct followed by a structure name. Syntax: struct struct-name { data_type var-name; data_type var-name; ... };

  3. Concepts by example struct student { int id; char name[20]; }; • The structure definition does not allocate any memory. • It just gives a template that conveys to the compiler how the structure is laid out in memory and gives details of the members. • Memory is allocated for the structure when we declare a variable of the structure. Example: define a variable of student by writing struct student student1;

  4. Typedef Declaration • When we precede a struct name with typedef keyword, then the struct becomes a new type. • For example, consider the following declaration: typedef struct { int id; char name[20]; } student; • Now we can straightaway declare variables of this new data type as we declare variables of type int, float, char, double, etc. • To declare a variable of structure student, we will just write: student student1;

  5. More examples struct point { int x; int y; } p1; // declare p1 struct point p2; // declare p2 typedef struct { int x; int y; } POINT; POINT p1, p2;

  6. Size of structure Size of a user defined structure data type is the sum of the sizes of data types in the structure definition. Example: typedef struct { int x; int y; } POINT; POINT p1, p2; sizeof(POINT) is equal to size(int) + sizeof(int)

  7. typedef struct { int id; char name[20]; } student; sizeof(student) is equal to ?

  8. Initializing Structures • The general syntax to initialize a structure variable is given as follows: struct struct_name { data_type member_name 1; data_type member_name 2; data_type member_name 3; ………………………………………….. } struct_var= {constant1, constant2, constant3, ….};

  9. Accessing the Members of a Structure • Each member of a structure can be used just like a normal variable, but its name will be a bit longer. • A structure member variable is generally accessed using a ‘.’ (dot operator). • The syntax of accessing a member of a structure is: struct_var.member_name • For example, to assign value to the individual data members of the structure variable student1, we may write: student1.id = 01;

  10. Accessing the Members of a Structure • For example, struct student student1 = {01, "Rahul"};studen1.id = 2; struct student student2; • We can assign a structure to another structure of the same type. • Then to assign one structure variable to another, we will write: student2 = student1;

  11. More examples Declare and initialize struct point p1 = {320, 100}; POINT p2 = {200, 100}; Declare, set values, get values p1.x = 320; p1.y = 100; printf(“%d,%d”,p1.x, p1.y); p2.x = 300; p2.y = 300; printf(“%d,%d”,p2.x, p2.y);

  12. Nested Structures • A structure can be placed within another structure. • Such a structure that contains another structure as its member is called a nested structure. typedef struct { int dd; int mm; int yy; } DATE; typedef struct { int id; NAME name; DATE dob; } student; typedef struct { char first_name[20]; char mid_name[20]; char last_name[20]; } NAME; To assign values to the structure fields, we will write: student student1; Strcpy(&student1.name.first_name , “Janak”); studdent1.dob.dd = 15; studuent1.dob.mm = 03; student1.dob.yy= 1990;

  13. 3. Pointers of Structures • C allows to create a pointer to a structure. • Like in other cases, a pointer to a structure is never itself a structure, but merely a variable that holds the address of a structure. • The syntax to declare a pointer to a structure can be given as: struct struct_name { data_type member_name1; data_type member_name2; ..................................... } *ptr; OR struct struct_name *ptr;

  14. Access structure through pointers • For our student structure we can declare a pointer variable by writing: student *ptr, student1; • The next step is to assign the address of stud to the pointer using the address operator (&). So to assign the address, we will write: ptr = &student1; • To access the members of the structure, one way is to write: (*ptr).id; • An alternative to the above statement can be used by using ‘pointing-to’ operator (->) ptr->id = 1;

  15. Expression equivalence ptr -> id  studuent1.id (*ptr).id studuent1.id &ptr->id  &student1.id &(*ptr).id  &student1.id

  16. POINT p1; • The address of p1 is &p1, • The address of p1.x is &p1.x • Pointers POINT *pt;/* define a pointer of POINT */ pt = &p1;/* assign the address of p1 to pt */ pt -> x  p1.x (*pt).x  p1.x &pt->x  &p1.x &(*pt).x  &p1.x See demo example

  17. Self-referential Structures • Self-referential structures are those structures that contain a reference to data of its same type. • That is, a self-referential structure contains a pointer to a data that is of the same type as that of the structure. struct node { int val; struct node *next; }; • Here the structure node contains two types of data: an integer val and next that is a pointer to a node. You must be wondering why do we need such a structure? Actually, self-referential structure is the foundation of other data structures.

  18. 3. Structure arrays • The general syntax for declaring an array of structure can be given as: struct struct_name struct_var[index]; e.g. student studuents[30]; • Now, to assign values to the ith student of the class, we will write: students[i].id = 9; strcpy(&students[i].name, “Peter”);

  19. More examples POINT p[10];/* declare an array of point types */ POINT *pt;/* define a pointer variable for structure point */ pt = &p[0];/* assign the address of p or p[0] to pt */ pt + 1 is pointing to p[1] (pt+1) -> x is equivalent to p[1].x

  20. 4. Pass Structure to Function • To pass any individual member of the structure to a function we must use the direct selection operator to refer to the individual members for the actual parameters. • The called program does not know if the two variables are ordinary variables or structure members. void display(int x, int y) { printf(“%d, %d”, x, y); } POINT p1={2,3}; display(p1.x, p1.y); //passing member values of p1 to function display(int, int)

  21. Passing a Structure to a Function • When a structure is passed as an argument, it is passed using call by value method. That is a copy of each member of the structure is made. • The general syntax for passing a structure to a function and returning a structure can be given as: struct struct_name func_name(struct struct_name struct_var); Example void display(POINT p) { printf(“%d, %d”, p.x, p.y); } POINT p1={2,3}; display(p1); / /passing entire structure p1 to function display(POINT)

  22. Pass by Structure Reference • When a structure reference (pointer) is passed as an argument, the address of structure is passed to function • The general syntax for passing a structure to a function and returning a structure can be given as: struct struct_name func_name(struct struct_name *struct_var); void display(POINT *p) { printf(“%d, %d”, p->x, p->y); } POINT p1={2,3}; display(&p1); / /passing the address of p1 to function display(POINT*) See demo example

More Related