C Programming

C Structures
- A structure in the C programming language is a collection of same or different type of variables to be placed under one name in a block of memory.
- The variables that make up the structure are called members. (Structure members are also commonly referred to as elements or fields.)
- The keyword struct tells the compiler that a structure is being declared.
- The general form of a structure declaration is
structstruct-type-name{ type member-name; type member-name; type member-name; . . . }structure-variables;
Example:
struct addr { char name[30]; char street[40]; char city[20]; char state[3]; unsigned long int zip; } addr_info;
- Here declaration is terminated by a semicolon. This is because a structure declaration is a statement.
- The type name of the structure is addr. As such, addr identifies this particular data structure and is its type specifier.
Click The Image For zoom View
Accessing Structure Members:
- Individual members of a structure are accessed through the use of the . operator (usually called the dot operator). For example, the following code assigns the ZIP code 12345 to the zip field of the structure variable addr_info declared earlier:
addr_info.zip = 12345;
- The structure variable name followed by a period and the member name references that individual member. The general form for accessing a member of a structure is
structure-name.member-name
Therefore, to print the ZIP code on the screen, write
printf("%d", addr_info.zip);
- This prints the ZIP code contained in the zip member of the structure variable addr_info.
- In the same fashion, the character array addr_info.name can be used to call gets(), as shown here:
gets(addr_info.name);
- This passes a character pointer to the start of name.
Structure Assignments:
- The information contained in one structure may be assigned to another structure of the same type using a single assignment statement.
- The following program illustrates structure assignments:
#include <stdio.h> int main(void) { struct { int a; int b; } x, y; x.a = 10; y = x; /* assign one structure to another */ printf("%d", y.a); return 0; }
After the assignment, y.a will contain the value 10.
Arrays of Structures:
the most common usage of structures is in arrays of structures. To declare an array of structures, you must first define a structure and then declare an array variable of that type. For example, to declare a 100-element array of structures of type addr, defined earlier, write
struct addr addr_info[100];
For example, to print the ZIP code of structure 3, write
printf("%d", addr_info[2].zip);
Like all array variables, arrays of structures begin indexing at 0.
Passing Structures to Functions:
Passing Structure Members to Functions:
When you pass a member of a structure to a function, you are actually passing the value of that member to the function. Therefore, you are passing a simple variable (unless, of course, that element is compound, such as an array). For example, consider this structure:
struct fred { char x; int y; float z; char s[10]; } mike;
Here are examples of each member being passed to a function:
func(mike.x); /* passes character value of x */ func2(mike.y); /* passes integer value of y */ func3(mike.z); /* passes float value of z */ func4(mike.s); /* passes address of string s */ func(mike.s[2]); /* passes character value of s[2] */
Passing Entire Structures to Functions:
- When a structure is used as an argument to a function, the entire structure is passed using the standard call-by-value method.
- When using a structure as a parameter, remember that the type of the argument must match the type of the parameter. For example, in the following program both the argument arg and the parameter parm are declared as the same type of structure.
#include <stdio.h> int main(void) { struct { int a; int b; } x, y; x.a = 10; y = x; /* assign one structure to another */ printf("%d", y.a); return 0; }#include <stdio.h> /* Define a structure type. */ struct struct_type { int a, b; char ch; } ; void f1(struct struct_type parm); int main(void) { struct struct_type arg; arg.a = 1000; f1(arg); return 0; } void f1(struct struct_type parm) { printf("%d", parm.a); }
Declaring a Structure Pointer:
- Like other pointers, structure pointers are declared by placing*in front of a structure variable’s name. For example, assuming the previously defined structure addr, the following declares addr_pointer as a pointer to data of that type:
-
struct addr *addr_pointer;
For example, given the following fragment:
struct bal { float balance; char name[80]; } person; struct bal *p; /* declare a structure pointer */ then p = &person;
- To access the members of a structure using a pointer to that structure, you must use the−>operator. For example, this references the balance field:
p->balance
- The−>is usually called the arrow operator, and consists of the minus sign followed by a greater-than sign. The arrow is used in place of the dot operator when you are accessing a structure member through a pointer to the structure.
Example:
prints the hours, minutes, and seconds on your screen using a software timer.
/* Display a software timer. */ #include <stdio.h> #define DELAY 128000 struct my_time { int hours; int minutes; int seconds; } ; void display(struct my_time *t); void update(struct my_time *t); void delay(void); int main(void) { struct my_time systime; systime.hours = 0; systime.minutes = 0; systime.seconds = 0; for(;;) { update(&systime); display(&systime); } return 0; } void update(struct my_time *t) { t->seconds++; if(t->seconds==60) { t->seconds = 0; t->minutes++; } if(t->minutes==60) { t->minutes = 0; t->hours++; } if(t->hours==24) t->hours = 0; delay(); } void display(struct my_time *t) { printf("%02d:", t->hours); printf("%02d:", t->minutes); printf("%02d\n", t->seconds); } void delay(void) { long int t; /* change this as needed */ for(t=1; t<DELAY; ++t) ; }
C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging from supercomputers to embedded systems.
C was originally developed by Dennis Ritchie between 1969 and 1973 at Bell Labs, and used to re-implement the Unix operating system. It has since become one of the most widely used programming languages of all time, with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the American National Standards Institute (ANSI) since 1989 and subsequently by the International Organization for Standardization (ISO).
Course Features
- Lectures 19
- Quizzes 0
- Duration 50 hours
- Skill level All levels
- Language English
- Students 2
- Certificate No
- Assessments Self