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