Unit IV & V Previous Papers 1 mark Answers

Similar documents
int Return the number of characters in string s.

Computer Programming Unit 3

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

IV Unit Second Part STRUCTURES

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Advanced C Programming and Introduction to Data Structures

Arrays and Pointers (part 1)

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

dynamic memory allocation

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Module 6: Array in C

Subject: Fundamental of Computer Programming 2068

Dynamic Memory Allocation

// CSE/CE/IT 124 JULY 2015, 2.a. algorithm to inverse the digits of a given integer. Step 1: Start Step 2: Read: NUMBER Step 3: r=0,rev=0

Introduction to C Language (M3-R )

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Arrays and Pointers (part 1)

UNIT - V STRUCTURES AND UNIONS

First of all, it is a variable, just like other variables you studied

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Pointers. Introduction

Pointers and File Handling

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

In Java we have the keyword null, which is the value of an uninitialized reference type

Arrays, Pointers and Memory Management

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Dynamic Data Structures. CSCI 112: Programming in C

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Structure, Union. Ashishprajapati29.wordpress.com. 1 What is structure? How to declare a Structure? Explain with Example

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

CS201- Introduction to Programming Current Quizzes

Syntax to define a Structure: struct structurename { datatype membername1; datatype membername2;... } ; For Example:

Example: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( )

C & Data Structures syllabus

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

C Programming Basics II

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

M1-R4: Programing and Problem Solving using C (JAN 2019)

Pointers and Arrays 1

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Tin học cơ sở 4$ Structures!

[0569] p 0318 garbage

INTRODUCTION 1 AND REVIEW

int marks[10]; // fixed size and fixed address No change in Memory address.

MYcsvtu Notes LECTURE 34. POINTERS

Computer Systems Principles. C Pointers

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

3.3 Structures. Department of CSE

Procedural programming with C

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

C PROGRAMMING Lecture 4. 1st semester


Kurt Schmidt. October 30, 2018

'C' Programming Language

Language comparison. C has pointers. Java has references. C++ has pointers and references

Memory, Data, & Addressing II CSE 351 Spring

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

C Programming Language

C-Refresher: Session 06 Pointers and Arrays

Dynamic memory allocation (malloc)

UNIT-IV. Structure is a user-defined data type in C language which allows us to combine data of different types together.

Lectures 5-6: Introduction to C

POINTER AND ARRAY SUNU WIBIRAMA

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

POINTERS, STRUCTURES AND INTRODUCTION TO DATA STRUCTURES

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Character Strings. String-copy Example

C Structures & Dynamic Memory Management

Introduction to Scientific Computing and Problem Solving

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Variation of Pointers

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

MODULE V: POINTERS & PREPROCESSORS

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Object Oriented Programming. Solved MCQs - Part 2

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

NCS 301 DATA STRUCTURE USING C

Multi-Dimensional arrays

Data Representation and Storage

Memory Allocation in C

Transcription:

1 What is pointer to structure? Pointer to structure: Unit IV & V Previous Papers 1 mark Answers The beginning address of a structure can be accessed through the use of the address (&) operator If a variable represents a structure type variable, then &variable represents the starting address of that variable It is equivalent to variablemember struct student s, *ptr; ptr=&s; An individual structure member can be accessed through a pointer variable by writing ptr->member or (*ptr)member 2 What is malloc and state its default value? malloc: Allocates requested size of bytes and returns a pointer to the first byte of the allocated space A block of memory may be allocated using the function malloc The malloc function reserves a block of memory of specified size and returns a pointer type of void This means that we can assign it to any type of pointer General form: ptr=(cast type *)malloc(byte size); - where ptr is a pointer of type cast type - The malloc returns pointer (of cast type) to an area of memory with size byte size Default value is garbage value 3 What is void pointer? void pointer in C: A void pointer is a pointer that has no associated data type with it A void pointer can hold address of any type and can be type casted to any type Prepared by Aravinda Kasukurhi, CSE, RVRJCCE 1

#include<stdioh> main() int a=10; void *ptr; ptr=&a; printf("ptr value=%d\n",*(int*)ptr); } Output: ptr value=10 1 2 4 What is the difference between structure and union? Structure Union Structure is a user defined data type and Heterogeneous Structure has members with different data types Syntax: structure <tagname> datatype1 member1; datatype2 member2; datatypen membern; }; Structure members are accessed through structure variable and dot operator In structure each member has separate space in memory Take below example struct student int rollno; char gender; float marks; }s1; Union is a user defined data type and Heterogeneous Union has members with different data types Syntax: union <tagname> datatype1 member1; datatype2 member2; datatypen membern; }; Union members are accessed through Union variable and dot operator In union, all members share the same memory space This is the biggest difference between structure and union union student int rollno; char gender; float marks; Prepared by Aravinda Kasukurhi, CSE, RVRJCCE 2

3 4 The total memory required to store a structure variable is equal to the sum of size of all the members In above case 9 bytes (4+1+4) will be required to store structure variable s1 We can access any member in any sequence s1rollno = 20; s1marks = 900; printf( %d,s1rollno); The above code will work fine but will show erroneous output in the case of union All the members can be initialized while declaring the variable of structure }s1; In above example variable marks is of float type and have largest size (4 bytes) So the total memory required to store union variable s1 is 4 bytes We can access only that variable whose value is recently stored s1rollno = 20; s1marks = 900; printf( %d,s1rollno); The above code will show erroneous output The value of rollno is lost as most recently we have stored value in marks This is because all the members share same memory space Only first member can be initialized while declaring the variable of union In above example we can initialize only variable rollno at the time of declaration of variable 5 Explain the role of pointers in C language Pointer: A pointer is a variable that contains an address which is a location of another variable in memory A pointer is a variable; its value is also stored in the memory in another location Declaring and initializing pointers: datatype *ptrname; This tells the compiler 3 things about the variable ptrname i The asterisk (*)tells that the vaiable ptr_name is a pointer variable ii Ptrname needs a memory location iii Ptrname points to a variable of type data type Prepared by Aravinda Kasukurhi, CSE, RVRJCCE 3

int *p; declares the variable p as a pointer variable that points to an integer data type 6 What is a structure? Explain Structure: Represent a collection of data items of different types using a single name, that constructed data type is known as structure (Heterogeneous type) A structure is a convenient tool for handling a group of logically related data items It can be used to represent a set of attributes, such as student name, roll number and marks Structures help to organize complex data in a more meaningful way Declaration of a structure: struct <tagname> datatype1 member1; datatype2 member2; datatypen membern; }; structure template The keyword struct declares a structure to hold the details struct student int rollnumber; char name[20]; }s1,s2; Here, s1,s2 are structure variables Members of structure do not occupy any memory until they are associated with the structure variables such as s1, s2 7 Briefly explain about Self Referential Structure Self referential structures: Structures which contain a member that point to the same structure type are called self-referential structures Prepared by Aravinda Kasukurhi, CSE, RVRJCCE 4

//Example #include<stdioh> typedef struct node int data; struct node *next; }list; main() list *l; l=(list*)malloc(sizeof(list)); printf("enter data\n"); scanf("%d",&l->data); l->next=null; printf("l->data=%d\n",l->data); } Output: enter data 1 l->data=1 8 What is meant by Dynamic memory allocation? Dynamic memory allocation: The process of allocating memory at run time is known as dynamic memory allocation Function malloc calloc realloc free Task Allocates requested size of bytes and returns a pointer to the first byte of the allocated space initializes with garbage value Allocated space for an array of elements, initializes them to zero and then returns a pointer to the memory modifies the size of previously allocated space frees previously allocated space Prepared by Aravinda Kasukurhi, CSE, RVRJCCE 5

9 Define the term realloc( ) realloc(): If the previously allocated memory is insufficient or more than required, you can change the previously allocated memory size using realloc() General form: ptr=(cast type *)relloc(ptr, n*byte size); Here, ptr is reallocated with size of byte size (new size) 10 Differentiate between *(arr+i) and (arr+i) *(arr+i) refers the content of ith index of arr or content of (arr+i) means content of (base address + i) For example, if i=1, then *(arr+1) refers the content of (base address +1) Whereas (arr+i) refers to the address or location of of ith index of arr (or) address of (arr+i) means (base address + i) For example, if i=1, then (arr+1) refers to the address location of (base address +1) int *a,i; a=(int*)malloc(5*sizeof(int)); If arr base address=1000, then (1000+1) means next byte address (data type is int, so 1004) 11 Distinguish between arrays and pointers by giving examples Pointer 1 A pointer is a place in memory that keeps the address of another place inside 2 Pointer can t be initialized at definition 3 Pointer is dynamic in nature The memory allocation can be resized or freed later Array 1 An array is a single, preallocated chunk of contiguous elements (all of the same type), fixed in size and location 2 Array can be initialized at definition Example int num[] = 2, 4, 5} 3 They are static in nature Once the memory is allocated, it cannot be resized or freed dynamically 12 Explain how the structure variable can be declared Prepared by Aravinda Kasukurhi, CSE, RVRJCCE 6

Structure: Represent a collection of data items of different types using a single name, that constructed data type is known as structure (Heterogeneous type) The keyword struct declares a structure to hold the details struct student int rollnumber; char name[20]; }s1,s2; Here, s1,s2 are structure variables 13 What do you mean by array of structure? Array of structures: An array of structures is an array in which each element is a structure If we want to store the details of more than one student, like rnu, name, cgpa obtained by 10 students struct student int rnu; char name[20]; float cgpa; }s1,s2; struct student stu[10]; where s[10] is a structure variable It may contain the details of 10 students If you treat the rnu, name, cgpa of each individual student as a record then each record may be accessed and processed separately like individual elements of an array 14 Pointer arithmetic Pointer arithmetic: Pointer variables can be used in expressions (arithmetic expressions like normal variable) For example, if p1 and p2 are properly declared and initialized pointers, then the following statements are valid s1 = *p1 * *p2; s2 = x + *p2; s3 = 5 * *p2 / *p1; Prepared by Aravinda Kasukurhi, CSE, RVRJCCE 7

15 Importance of unions Unions, contain members whose individual data types may differ from one another The members within a union all share the same storage area within the computer s memory Unions are used to conserve memory They are useful for applications involving multiple members, where values need not be assigned to all of the members at one time All the members of a union use the same location This implies that, although a union may contain many members of different types, it can handle only one member at a time 16 Write the syntax for command line argument We can pass the parameters or arguments to any user defined function at the time of calling the function We can pass the arguments to the main function through command line arguments only Prepared by Aravinda Kasukurhi, CSE, RVRJCCE 8

17 What is NULL pointer? NULL pointer: It is a pointer which is pointing to nothing Pointer which is initialized with NULL value is considered as NULL pointer #include<stdioh> main() int *ptr; ptr=null; printf("the value of ptr=%u\n",ptr); } Output: The value of ptr=0 18 What is a file? A file is a place on the disk where a group of related data is stored Files are used for permanent maintenance of large amounts of data The output of one program stored in a file, which is further used for analysis or processing such dada 19 Types of files? Files are of two types: i Text files ii Binary files 20 What is the significance of End of File? File pointer is to read/write data from/to a file To know where to stop reading or writing from/to a file End Of the File (EOF) is marked by compiler 21 Which data type a constructor can return? (out of syllabus) int Prepared by Aravinda Kasukurhi, CSE, RVRJCCE 9