Professional Documents
Culture Documents
cp11 Structs
cp11 Structs
Swaroop Joshi
2023
Types
How would you represent a tweet ?
Types
How would you represent a tweet ?
Types
int
double
char
bool
char[]
How would you represent a tweet ?
Types
int
double
char
bool
char[]
double
char
bool
char[]
double
char
bool
char[]
✤ E.g., Tweet
✤ E.g., Car
✤ E.g., Student
Structs
✤ E.g., Student
Structs
struct tweet
{
char author[20];
char text[140];
char timestamp[40];
int num_rt;
int num_quote_rt;
int num_likes;
};
Structs
struct tweet
{
char author[20];
char text[140];
char timestamp[40];
int num_rt;
int num_quote_rt;
int num_likes;
};
Structs
typedef struct
{
char author[20];
char text[140];
char timestamp[40];
int num_rt;
int num_quote_rt;
int num_likes;
} tweet_t;
Structs: with typedef
typedef struct
{
char author[20];
char text[140];
char timestamp[40];
int num_rt;
int num_quote_rt;
int num_likes;
} tweet_t;
Structs: with typedef
Structure de nition:
What does the structure look
like? (Don’t forget the semicolon)
fi
Structs: with typedef
Structure instantiation:
Structure de nition: What does this speci c instance
What does the structure look of this structure look like?
like? (Don’t forget the semicolon)
fi
fi
Example
✤ De ne a structure that
represents a batsman using
his last name, country
code, and total runs
report_century(b1);
report_century(b2);
Passing a struct to a function
/**
* @brief Reports if the given batsman scored a century.
*
*/
void report_century(batsman_t b)
{
printf("%s %s a century\n", b.name,
b.runs >= 100 ? "scored" : "did not score");
}
report_century(b1);
report_century(b2);
> ./a.out
Kohli did not score a century
Yadav scored a century
Building more complex data
✤ An array of structs
✤ A struct containing
arrays.
Building more complex data
const int LENGTH = 5;
batsman_t my_fav[LENGTH];
✤ You rarely want a single
struct for (int i = 0; i < LENGTH; ++i) {
printf("Enter the player's data: ");
scanf("%s %s %d",
my_fav[i].name,
✤ You can create aggregate my_fav[i].country_code,
data using aggregate data &my_fav[i].runs);
}
✤ An array of structs
✤ A struct containing
arrays.
An array of 5 values of
Building more complex data type batsman_t
✤ An array of structs
✤ A struct containing
arrays.
Building more complex data
const int LENGTH = 5;
batsman_t my_fav[LENGTH];
✤ You rarely want a single
struct for (int i = 0; i < LENGTH; ++i) {
printf("Enter the player's data: ");
scanf("%s %s %d",
my_fav[i].name,
✤ You can create aggregate my_fav[i].country_code,
data using aggregate data &my_fav[i].runs);
}
int main()
{
//...
print_batsmen_info(my_fav, LENGTH);
return 0;
}
Your turn
✤ Allows programmers to create new types, thus extending the language and create
in nite possibilities
✤ Provides encapsulation –an important OOP concept– that simpli es modelling the real
world data
✤ e.g., list of batsmen as batsman_t list[], where the k-th element represents the
k-th batsman
✤ Instead of three parallel arrays for names, countries, runs, where the k-th element in
each represents the corresponding batsman’s name, country, and run respectively
fi
fi