Structures
So far:
primitive data types: int, double, float, char
arrays( 1D and 2D)
strings(arrays for char)
(this is how they create objects here)
Nearly all programing languages allow the user to define their own “type” beyond the ones provided
In C, a struct is the way in which a user can define their own “type”
Examples:
block example( letter,(char) number(int), color(char color [15])
Tile example from scrabble
How to define a struct:
struct <yourstructnamehere> {
type1 var1;
type2 var2;
};
struct block{
char letter;
int number;
char color[15];
};
Define your structs after your #includes but before your function prototypes.(NOT in ANY function)
Now i can create a function of variable of type struct block:
main
struct block myblock; // In myblock, it has the letter, number, and color, all uninitializied, garbage in each slot
Q1. How to put something in those boxes or refer to these boxes?
To access a field(letter, number, and color), use the dot operator
myblock.letter = ‘A’;
myblock.number = f;
strcpy(myblock.color, “blue”);
If we pass a struct into a function by reference, then the function gets a pointer to the struct. Here is a general picture:
main
~-~
struct block myblock;
…
f(&myblock);
void f(struct block* b){
// To refer what b is, we do (*b)
(*b).letter = ‘A’;
\\ This is so common, it has an alternate syntax
\\ (*ptr).field is the same as ptr → field
b→ number = 3;
}
Alternate picture of passing struct by value
struct block myblock;
// file with ‘A’, 6, “blue”
f(myblock);
void f(struct block b) {
// Contents of whatever was passed in to f get copied // into b. Changes to b are only in the struct function.
b.number = 13;
// number in main stays the same (6)….
}
Myblocks in an integer array with random integers in between 0 and 259
myblocks
When you do a #defin, it does a copy replace.
Guha wrote:
myblocks[i] = x%SIZE;
His #define is
#define SIZE ALPHASIZE*NUMSIZE;
myblocks[i] = x%ALPHASIZE*NUMSIZE;
The line of code does the mod 26, then takes that answewr and multiples it by 10, hence my number always ended in 10!!!
The fix was putting () around size;
now it does x%(APLHASIZE*NUMSIZE);
better fix is const int = #;
Structures 2
Hotel
rooms( array, 1 available, 0 NA)
int available [10]
& is needed to affect the original struct.
. versus → versus []
for h, is needed to refer to the varibale a pointer is pointing to put a * to the left of it
(*h).rooms[i] // Dot is needed to go to that struct variable
(instead, do ptr → comp for an alternate example)
(h → comp)
To access
To access array index []
component of a strucut .
componet via ptr to struct → (alt syn for (*ptr).
/
An edit:
give each room a capacity
then when check in, ask size of aprty, give room with least number of extra capacity
Toys
Struct toy
struct block {
int number;
char letter;
char color[15];
};
struct doll {
char name[20];
double price;
};
struct toys {
struct block rubix;
struct doll barbie;
};struct toy t;
t.rubix.number →
strcpy(t.barbie.name, “Ken”);
in printf when you pass a struct by value, it gets copied byte by byte into the print function to print
const restrict the info to be read only, can’t change values