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)
Can you have a strucutre member of same type in a strucutre?
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
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
Or we can have
Struct y = {
10,
{1,2,3,4,5},
{25,1.9}
}
If we have two structures of x and y that are the same structure, can set x = y
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!!
How shoudlsturcutres be passed into functions
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
Const keyword
Int func1(int const *ptr){}
Function1 will not be able to change the values in the array or structure
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.
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
So int const *ptr is a pointer to constant integer