Chp 10 Structures Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Structure:

A

A structure contains a number of data types grouped together.
These data types may or may not be of the same type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define new Structure data type:

A
The foll. statement defines structure type:
struct name 
{
 char name;
 int number;
 float f;
 char s[20];
};

this defines a new data type called struct name.
Each variable of this data type will consist of char variable name, int var number, float var f, array of char(string) s.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Declare variable of structure data type:

and initialisation?

A

struct name a,b,c ;

This statement sets aside space in memory.

we can also
struct name a = { ‘A’, 12, 20.0, “Alpha”};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Declaration of structure data type and the structure variables in 1 statement :

A
struct name 
{
 char name;
 int number;
 float f;
 char s[20];
} a, b, c ;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Rules

Precautions while declaring a structure type:

A
  1. Put a ; after closing braces.
  2. Structure type declaration just defines the form of a structure. It does not reserve any space.
  3. Usually, they happen at the beginning or in sep header file.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Accessing Structure elements:

A

structVarName.element

Here the use of dot operator is done for that purpose.

Before dot there must always be a structure variable and after dot there must always be a structure element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How structure elements are stored:

A

Stored in contiguous memory locations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Array of Structures:

Declare it:

A

struct name arr[100] ;

this provides space in memory for 100 structures of the type struct name.

(This really allows us to handle number of things which have types of data types.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

refer an element in array of structures:

and memory map of array of structures:

A

arr[92].name

arr[0]’s structure elements followed by arr[1]’s….
All adjacent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

linkfloat()

why is it used?

A

It forces linking of floating point emulator into an application. No need to call it, just define it.

In its absence “Floating point formats not linked” error occurs with majority of C compilers.

When parsing our source file, if the compiler encounters a float it sets a flag to have the linker link in the floating-point emulator, which manipulates floating-point numbers in runtime library functions like scanf(),atof().
Sometimes reference to float is obscure and the compiler does not detect the need for emulator.
Example: Using scanf() to read a float in an array of structures.
So here we force the formats to be linked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

linkfloat()

defined:

A

linkfloat()
{
float a=0,*b;
b = &a; / Cause emulator to be linked */
a = b; / suppresses earning, var not used */
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Features of structures:

A
  1. Values of a structure variable can be assigned to another structure variable of the same type using the assignment operator. In array it wasn’t possible.
    Which is possible as they have contiguous memory locations.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Nested structure, access the element?

A

suppose large is nested in struct bolt.
then
bolt.large.qty
will give us element qty within large struct which is in bolt.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Passing structure variable to function?

A

diaplay ( char *s, char *t, int n)
{}

Here call by value and call by reference both is done.
in case of char, base address is passed.
OR passing entire struct var at a time:

display ( struct name p)
{}

Make sure struct name is defined outside of any function so that display can access it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Pointer to a structure:

when u have address, how to access structure element?

A

struct book *ptr;
ptr = &structbookVarname

printf(‘%s’, ptr->name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Operator ->

A

arrow operator.
LHS : pointer to a structure.
RHS : Structure element.

17
Q

Pass address of struct variable to a function:

A
func(&b)
{ b->name  /* to access  */ }
18
Q

Memory map is VC++:

A

this compiler is 32 bit compiler targetted to generate code for 32 bit microprocessor. The architecture of this is such that it’s able to fetch data that is present in multiple of 4 faster.
So a char that takes 1 byte to store, if after that say a float is present in the struct then there will be a hole of 3 bytes.

19
Q

How to get precise control over the memory?

to avoid that 3 byte hole..

A

Use #pragma pack directive.
Specifies packing allignment for structure members.
this takes effect for structure declaration after it. VC++ supports this, turboC/C++ doesn’t

20
Q

Uses of Structure:

A
  1. Database management.
  2. Graphics.
  3. Fxing position of cursor.
  4. Cursor Size.
  5. Format floppy disk.
  6. Hide file from directory.
  7. Sending the output to the printer.
  8. Interacting with the mouse.
  9. Clearing the contents on screen.
  10. Receiving a key from keyboard.
  11. Checking memory size of computer.