Structures in C By Chirag, Sobhit , Fazal, Zishan, Saad and Misbah B.Tech in Food Technology ( 2 nd Sem) Submitted to Mr. Faisal Malik (bftc-202,206)
Definition Structure is a user-defined data type in C which allows you to combine different data types to store a particular type of record. It is somewhat similar to an Array. The only difference is that array is used to store collection of similar data types while structure can store collection of any type of data.
Example: Structure is used to represent a record. Suppose you want to store record of Student which consists of student name, address, roll number and age. You can define a structure to hold this information. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book − Title Author Subject Book ID
Defining a structure struct keyword is used to define a structure. struct defines a new data type which is a collection of different types of data. Syntax : Note: The closing braces in the structure type declaration must be followed by a semicolon(;).
Example of Structure Here the struct Book declares a structure to hold the details of book which consists of three data fields, namely name , price and pages . These fields are called structure elements or members . Each member can have different data type, like in this case, name is of char type and price is of int type etc. Book is the name of the structure and is called structure tag.
Declaring Structure Variables It is possible to declare variables of a structure , after the structure is defined. Structure variable declaration is similar to the declaration of variables of any other data types. Structure variables can be declared in following two ways.
1) Declaring Structure variables separately
2) Declaring Structure Variables with Structure definition
Accessing Structure Members Structure members can be accessed and assigned values in number of ways. Structure member has no meaning independently. In order to assign a value to a structure member, the member name must be linked with the structure variable using dot . operator also called period or member access operator.
We can also use scanf () to give values to structure members through terminal.
Structure Initialization Like any other data type, structure variable can also be initialized at compile time. Or
Array of Structure We can also declare an array of structure . Each element of the array represents a structure variable. Example : struct employee emp [5];
The below code define an array emp of size 5 elements. Each element of array emp is of type employee
Nested Structures Nesting of structures, is also permitted in C language. Example :
Structure as function arguments We can pass a structure as a function argument in similar way as we pass any other variable or array. Example:
C Pointers Pointers are one of the core components of the C programming language. A pointer can be used to store the memory address of other variables, functions, or even other pointers. The use of pointers allows low-level memory access, dynamic memory allocation, and many other functionality in C.
What is a Pointer in C A pointer is defined as a derived data type that can store the address of other C variables or a memory location. We can access and manipulate the data stored in that memory location using pointers.
Syntax of C Pointers The syntax of pointers is similar to the variable declaration in C, but we use the ( * ) dereferencing operator in the pointer declaration. datatype * ptr ; Where ptr is the name of the pointer. datatype is the type of data it is pointing to. The above syntax is used to define a pointer to a variable. We can also define pointers to functions, structures, etc.
How To Use Pointers The use of pointers in C can be divided into three steps: Pointer Declaration Pointer Initialization Pointer Dereferencing
Pointer Declaration In pointer declaration, we only declare the pointer but do not initialize it. To declare a pointer, we use the ( * ) dereference operator before its name. Example: int * ptr ; The pointer declared here will point to some random memory address as it is not initialized. Such pointers are called wild pointers.
Pointer Initialization Pointer initialization is the process where we assign some initial value to the pointer variable. We generally use the ( & ) addressof operator to get the memory address of a variable and then store it in the pointer variable. Example: int var = 10; int * ptr ; ptr = &var; We can also declare and initialize the pointer in a single step. This method is called pointer definition as the pointer is declared and initialized at the same time. Example: int * ptr = &var;
Pointer Dereferencing Dereferencing a pointer is the process of accessing the value stored in the memory address specified in the pointer. We use the same ( * ) dereferencing operator that we used in the pointer declaration.
C Pointer Example // C program to illustrate Pointers #include < stdio.h > void geeks() { int var = 10; // declare pointer variable int* ptr ; // note that data type of ptr and var must be same ptr = &var; // assign the address of a variable to a pointer printf ("Value at ptr = %p \n", ptr ); printf ("Value at var = %d \n", var); printf ("Value at * ptr = %d \n", * ptr ); } // Driver program int main() { geeks(); return 0; } Output: Value at ptr = 0x7fff1038675c Value at var = 10 Value at * ptr = 10
C Pointers and Arrays In C programming language, pointers and arrays are closely related. An array name acts like a pointer constant. The value of this pointer constant is the address of the first element. For example, if we have an array named val then val and & val [0] can be used interchangeably. If we assign this value to a non-constant pointer of the same type, then we can access the elements of the array using this pointer.
Example // C Program to access array elements using pointer #include < stdio.h > void geeks() { // Declare an array int val [3] = { 5, 10, 15 }; // Declare pointer variable int* ptr ; // Assign address of val [0] to ptr . // We can use ptr =& val [0];(both are same) ptr = val ; printf ("Elements of the array are: "); printf ("%d, %d, %d", ptr [0], ptr [1], ptr [2]); return; } // Driver program int main() { geeks(); return 0; } Output: Elements of the array are: 5 10 15
Self Referential Structres It is used to create the dynamic data structures linked lists, queues stacks and tress etc . A structure cannot contain an instance of itself However it can contain a pointer to the same structure type A structure containing a pointer to the same structure type is known as self referential structure
Difference 1-D ARRAY 2-D ARRAY 1. 1D Array Contains single row and multiple columns 1. 2D Array contains Mutiple row and multiple columns 2. 1D Array is a simple collection of elements. 2. 2D Array is a collection of 1D Array. 3. Store data as a list 3. Stores data in a row column format
Strings are Character Arrays Strings in c are Simply Arrays of characters - Example: Char s[10]; This is a 10 element array that can hold a character string consisting = or < than 9 characters This is because c does not know where the end of an array is at run time by using NULL character to terminate all strings in its functions For example char str [10] ={‘u’, ‘n’, ‘I’, ‘x’,’\0’};
Advantages of Structured Programming Application programs are easier to read and comprehend. Application programs are less likely to contain logic errors. Errors are more easily identified. It increases productivity during application program development. The design of application programs has been improved. Application programs are more easily maintained. Machine-Independent, mostly.