Class 17: Structures Flashcards
struct book {
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
What is the TAG for this structure declaration?
Book
struct book {
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
How would you declare structure of this data type?
struct book library;
Declaring multiple structures of same type
struct book doyle, panshin, *ptbook
How to declare structure variable after the structure bracket
struct book {
char title[MAXTITL];
char author[AXAUTL];
float value;
} library; /* follow declaration with variable name */
How to intialize a structure variable given the follwing structure
struct book {
char title[MAXTITL];
char author[AXAUTL];
float value;
} ;
struct book library = {
“The Pious Pirate and the Devious Damsel”,
“Renee Vivotte”,
1.95
};
MUST BE SEPARATED BY COMMA
struct book {
char title[MAXTITL];
char author[AXAUTL];
float value;
} ;
How to access structure members
struct book library;
library.value
Which is correct
scanf(“%f”,&library.value) or
scanf(“%f”,&(librarby.value))
Second option because & has higher precedence than dot operator
Is this valid way to initialize a structures variable?
struct book gift = { .value = 25.99,
.author = “James Broadfool”,
.title = “Rue for the Toad”};
Yes
How to declare an array of structures
struct book library[MAXBKS]
library[0].value;
struct guy { // second structure
struct names handle; // nested structure
char favfood[LEN];
char job[LEN];
float income;
};
This structure contains a nested structure called handle. The name structure should be declared above the structure guy
struct guy *him
Pointer to structure guy
Suppose you have struct guy *him
him = &fellow[0]
him–>income = (*him).income
o_data = n_data; // assigning one structure to another
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
Strict{
Int a;
Float b;
}x;
Strict{
Int a;
Float b;
}y[10], *z;
Z = &x;
Y[2] = x;
Are these valid statements?
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!!!
Typedef long long lol;
Typedef unsigned Int unit;
Ll long_var;
Unint count;
- ll var1 declares var1 as long long
- Unint count declares count as unsigned int
Typedef strict{
Int a;
Int b;
}Simple;
Or
Typedef struct Simple{
Typedef strict{
Int a;
Int b;
}Simple;
Or
Typedef struct Simple{
Int a;
Float;
}
How would you declare variables of this structure?
Simple x, y[10], *z;
What should you do if you want to use particular structure type throughout a source file?
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.
What is you want to use a particular structure in more than once source file?
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.
Can structure members have identical names to members of a structure of different type?
Yes
Struct S_2{
Int memebr1;
Int member2
Struct *S_2 member3;
Int member4[45]
};
How to directly access members of a structure??
THe dot operator
If we have a pointer to a structure, how can we access the members?
-> : ptr->first_name
Or (*ptr).first_name
1st method is more common
Op1->op2->op3 is equivalent to?
(((op1).op2).op3)