Class 17: Structures Flashcards

1
Q

struct book {
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
What is the TAG for this structure declaration?

A

Book

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

struct book {
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
How would you declare structure of this data type?

A

struct book library;

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

Declaring multiple structures of same type

A

struct book doyle, panshin, *ptbook

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

How to declare structure variable after the structure bracket

A

struct book {
char title[MAXTITL];
char author[AXAUTL];
float value;
} library; /* follow declaration with variable name */

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

How to intialize a structure variable given the follwing structure

struct book {
char title[MAXTITL];
char author[AXAUTL];
float value;
} ;

A

struct book library = {
“The Pious Pirate and the Devious Damsel”,
“Renee Vivotte”,
1.95
};
MUST BE SEPARATED BY COMMA

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

struct book {
char title[MAXTITL];
char author[AXAUTL];
float value;
} ;
How to access structure members

A

struct book library;
library.value

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

Which is correct
scanf(“%f”,&library.value) or
scanf(“%f”,&(librarby.value))

A

Second option because & has higher precedence than dot operator

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

Is this valid way to initialize a structures variable?

struct book gift = { .value = 25.99,
.author = “James Broadfool”,
.title = “Rue for the Toad”};

A

Yes

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

How to declare an array of structures

A

struct book library[MAXBKS]
library[0].value;

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

struct guy { // second structure
struct names handle; // nested structure
char favfood[LEN];
char job[LEN];
float income;
};

A

This structure contains a nested structure called handle. The name structure should be declared above the structure guy

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

struct guy *him

A

Pointer to structure guy

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

Suppose you have struct guy *him

A

him = &fellow[0]
him–>income = (*him).income

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

o_data = n_data; // assigning one structure to another

A

This causes each member of n_data to be assigned the value of the corresponding member of 0_data This works even if a member happens to be an array. They must be structures of the SAME TYPE

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

Strict{
Int a;
Float b;
}x;

Strict{
Int a;
Float b;
}y[10], *z;

Z = &x;
Y[2] = x;
Are these valid statements?

A

No because even though we can tell these two structures are the same, the compiler has not way of recognizing that these structures are identical, so they are treated as different. And you cannot have two different data types equal to each other!!!

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

Typedef long long lol;
Typedef unsigned Int unit;

Ll long_var;
Unint count;

A
  1. ll var1 declares var1 as long long
  2. Unint count declares count as unsigned int
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Typedef strict{
Int a;
Int b;
}Simple;

Or
Typedef struct Simple{

17
Q

Typedef strict{
Int a;
Int b;
}Simple;

Or
Typedef struct Simple{
Int a;
Float;
}

How would you declare variables of this structure?

A

Simple x, y[10], *z;

18
Q

What should you do if you want to use particular structure type throughout a source file?

A

You should use Typedef or struct declaration WITH a tag BEFORE MAIN. (Lab4) This is necessary to give file scope to the type created with Typedef or struct tag.

19
Q

What is you want to use a particular structure in more than once source file?

A

You should put the tag declaration in a header file. Then you can use the #include the header file with the declaration whenever it is needed.

20
Q

Can structure members have identical names to members of a structure of different type?

A

Yes
Struct S_2{
Int memebr1;
Int member2
Struct *S_2 member3;
Int member4[45]
};

22
Q

How to directly access members of a structure??

A

THe dot operator

23
Q

If we have a pointer to a structure, how can we access the members?

A

-> : ptr->first_name
Or (*ptr).first_name

1st method is more common

24
Q

Op1->op2->op3 is equivalent to?

A

(((op1).op2).op3)

25
Q

Can you have a strucutre member of same type in a strucutre?

A

Not because the problem is infinite recursion. The compiler would have to allocate infinite amount of storage. Instead we can declare a member pointer (ex: struct *S_2 member). The compiler can allocate space for pointer to structure which requires finite amount of space

26
Q

Initialize the following structure as an array

Struct Init_example{
Int a;
Short b[10];
Simple c;
} y = {
10,
{1,2,3,4,5},
{25,1.9}
};

  • Intial values for the members MUST IN THE ORDER GIVEN BY MEMBER LIST
A

Or we can have

Struct y = {
10,
{1,2,3,4,5},
{25,1.9}
}

27
Q

If we have two structures of x and y that are the same structure, can set x = y

A

Yes, since x and y have been declared of the same type, we can assign y to x by saying x = y. This assigns each member of y to the corresponding member of x!!

28
Q

How shoudlsturcutres be passed into functions

A

In C, parameters are passed by value so if the strucutre is large of of the values of all the members in the structure will have to be COPIED and pushed onto the stack when the function is called. WE SHOULD PASS a pointer instead because only the values of the pointer is passed rather than copies of all the values in the structure

29
Q

Const keyword

Int func1(int const *ptr){}

Function1 will not be able to change the values in the array or structure

A

Unless the functions needs to change values using the pointer, we should declare the parameter with const to indicate the the value(s) pointer to by the parameter WILL NOT CHANGE.

30
Q

Int const *ptr

Const means that the value the pointer is pointing to CANNOT CHANGE. You can READ the value of the integer but you cannot modify it

A

So int const *ptr is a pointer to constant integer