2
What is the difference between break & continue? The break statement will immediately jump to the end of the current block of code. The continue statement will skip the rest of the code in the current loop block and will return to the evaluation part of the loop. break will exit the loop completely, but continue will just stop the current iteration. i.e. Continue will skip the rest of the code and goes for another iteration in the loop. What is the difference between structure & union? 1. Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. 2. In union, one block is used by all the member of the union but in case of structure, each member has their own memory space. 3. Union is best in the environment where memory is less as it shares the memory allocated. But structure can not implement in shared memory. 4. As memory is shared, ambiguities are more in union, but less in structure. 5. Self referential union cannot be implemented in any datastructure, but self referential structure can be implemented. What is self referential structure? It is exactly what it sounds like: a structure which contains a reference to itself. A common occurrence of this is in a structure which describes a node for a linked list. Each node needs a reference to the next node in the chain. struct linked_list_node { int data; struct linked_list_node *next; // <- self reference };

Structure & union

Embed Size (px)

Citation preview

Page 1: Structure & union

What is the difference between break & continue?

The break statement will immediately jump to the end of the current block of

code. The continue statement will skip the rest of the code in the current loop block and will return to the evaluation part of the loop.

break will exit the loop completely, but continue will just stop the current iteration. i.e. Continue will skip the rest of the code and goes for another iteration in the loop.

What is the difference between structure & union?

1. Union allocates the memory equal to the maximum memory required by the

member of the union but structure allocates the memory equal to the total

memory required by the members.

2. In union, one block is used by all the member of the union but in case of

structure, each member has their own memory space.

3. Union is best in the environment where memory is less as it shares the memory

allocated. But structure can not implement in shared memory.

4. As memory is shared, ambiguities are more in union, but less in structure.

5. Self referential union cannot be implemented in any datastructure, but self

referential structure can be implemented.

What is self referential structure?

It is exactly what it sounds like: a structure which contains a reference to itself. A common

occurrence of this is in a structure which describes a node for a linked list. Each node needs a

reference to the next node in the chain.

struct linked_list_node {

int data;

struct linked_list_node *next; // <- self reference

};

Page 2: Structure & union

A self-referential structure is a data structure that includes references to other data of its same

type. A simple example of this would be an implementation in C of a linked list:

typedef struct listnode {

void *data;

struct listnode *next;

} list_t;

The reference to a listnode struct from within a listnode struct is the self-referential aspect of

the structure.

Q: What is the difference between binary and text files?

While both binary and text files contain data stored as a series of bits

(binary values of 1s and 0s), the bits in text files represent characters,

while the bits in binary files represent custom data. While text files

contain only textual data, binary files may contain both textual and

custom binary data.

Mode Description

r Opens an existing text file for reading purpose.

w Opens a text file for writing, if it does not exist then a new file is created. Here your program will start writing content from the beginning of the file.

a Opens a text file for writing in appending mode, if it does not exist then a new file is created. Here your program will start appending content in the existing file content.

r+ Opens a text file for reading and writing both.

w+ Opens a text file for reading and writing both. It first truncate the file to zero length if it exists otherwise create the file if it does not exist.

a+ Opens a text file for reading and writing both. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.