HSS Live XII Computer Science Notes PDF

Document Details

SweetRadon

Uploaded by SweetRadon

ACT Malappuram

Tags

computer science notes structures and pointers computer science programming

Summary

These are computer science notes for the second year, covering structures, pointers, and memory allocation. The document includes definitions, syntax examples, and explanations.

Full Transcript

Join Now: https://join.hsslive.in/group Downloaded from www.Hsslive.in ® ACT -ASSOCIATION OF COMPUTER TEACHERS MALAPPURAM COMPUTER SCIENCE SECOND YEAR ...

Join Now: https://join.hsslive.in/group Downloaded from www.Hsslive.in ® ACT -ASSOCIATION OF COMPUTER TEACHERS MALAPPURAM COMPUTER SCIENCE SECOND YEAR Join Now: https://join.hsslive.in/group Downloaded from www.Hsslive.in ® CHAPTER 1 Structures and Pointers Structure is a user-defined data type to represent a collection of different types of data under a common name. Syntax: struct structure_name { data type variable1; data type variable1;.............................. }; Eg: struct stud { int roll; char name; }; Variable declaration Once we have defined a structure data type, variable is declared using the following syntax: struct structure_tag var1, var2,..., varN; OR structure_tag var1, var2,..., varN; Eg: stud s1; Accessing elements of structure The dot operator (.) is used for accessing structure elements using the following syntax: structure_variable.element_name Nested structure A structure inside another structure is nested structure Syntax for accessing the inner structure element is: outer_structure_varaiable.inner_structure_variable.element Array Vs Structure Arrays Structures It is a derived data type. It is a user-defined data type A collection of same type of data. A collection of different types of data Elements of an array are referenced Elements of structure are referenced using the corresponding subscripts. using dot operator (.) Pointers Pointer is a variable that can hold the address of a memory location. Syntax to declare pointer variable: data_type * variable; The address of operator (&), is used to get the address of a variable. The value at operator (*) is used to retrieve the value pointed to by the pointer. Team ACT Malappuram 2 Join Now: https://join.hsslive.in/group Downloaded from www.Hsslive.in ® Two types of memory allocation The memory allocation before the execution of the program is static memory allocation. Memory allocation during run-time is dynamic memory allocation. The new operator is used for dynamic memory allocation and delete operator to de-allocate (free) the memory. Dynamic operators - new and delete The following syntax is used for dynamic memory allocation: pointer_variable = new data_type; Eg:- int *p=new int; The delete operator is used with the following syntax for de-allocating the memory. delete pointer_variable; Eg: delete p; Memory leak If the memory allocated using new operator is not freed using delete a part of the memory seems to disappear on every run of the program This situation is known as memory leak. Operations on pointers 1. Arithmetic operations on pointers -> -> Two types a. Addition (+ and ++) b. Subtraction (- and --) 2. Relational operations on pointers-> pointers-> Two types a. ==(equality operator) b. !=(non equality operator) Dynamic array Dynamic array is created during run time using the dynamic memory allocation operator new. The syntax is: pointer variable= new data_type[size]; Pointer and structure The syntax for accessing structure by its pointer is given below structure_pointer->element_name; [Structure definition struct employee { int ecode; char ename; float salary; }; Variable initialisation using pointer employee *eptr; Accessing elements eptr->ecode = 657346; cin>> eptr->salaray;] Self referential structure Self referential structure is a structure in which one of the elements is a pointer to the same structure. Eg: struct employee { int ecode; char ename; float salary; employee *ep; Team ACT Malappuram 3 Join Now: https://join.hsslive.in/group Downloaded from www.Hsslive.in ® }; Questions 1.Structure 1. within a structure is termed as..................... 2.Orphaned memory blocks are undesirable. How can they be avoided OR Discuss problems created by memory leaks. 3.Define a structure called time to group the hours, minutes and seconds. Also write a statement that declares two variables current-time and next-time which are of type struct time 4.Compare the aspects of arrays and structures. 5.Run time allocation of memory is triggered by................ operator. 6.State any two differences between static and dynamic memory allocation 7.Read the following code fragment: int a[] = {5, 10, 15, 20, 25}; int *p = a; Predict the output of the following statements: cout

Use Quizgecko on...
Browser
Browser