Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

BITS Pilani

K K Birla Goa Campus

CSF111: Computer Programming

Aggregate data type II – 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[]

An aggregate non-homogenous data:


Author, text, timestamp, num_rt, num_quote_rt,
num_likes, …
How would you represent a tweet ?
Types
int

double

char

bool

char[]

tweet An aggregate non-homogenous data:


Author, text, timestamp, num_rt, num_quote_rt,
num_likes, …
How would you represent a tweet ?
Types
int

double

char

bool

char[]

tweet An aggregate non-homogenous data:


Author, text, timestamp, num_rt, num_quote_rt,
A user de ned data type num_likes, …
fi
Structs
Structs

✤ Aggregate data of possibly different types to


make a new, meaningful type

✤ E.g., Tweet

✤ E.g., Car

✤ E.g., Student
Structs

struct tweet ✤ Aggregate data of possibly different types to


{ make a new, meaningful type
char author[20];
char text[140];
char timestamp[40];
int num_rt;
✤ E.g., Tweet
int num_quote_rt;
int num_likes;
};
✤ E.g., Car

✤ 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

struct tweet struct tweet musk_tweet = {


{ "@elonmusk",
char author[20]; "Let's make Twitter maximum fun!",
char text[140]; "2022-04-28 0723 +0530",
char timestamp[40]; 207100,
int num_rt; 36600,
int num_quote_rt; 2700000};
int num_likes;
}; printf("%s tweeted %s\n",
musk_tweet.author,
musk_tweet.text);
Detour: typedef

typedef double real;


real pi = 22.0/7;
printf("%lf\n", pi);
Detour: typedef

✤ The typedef declaration provides a way to


declare an identi er as a type alias, to be used
to replace a possibly complex type name

typedef double real;


real pi = 22.0/7;
printf("%lf\n", pi);
fi
Detour: typedef

✤ The typedef declaration provides a way to


declare an identi er as a type alias, to be used
to replace a possibly complex type name

typedef double real;


real pi = 22.0/7; Works perfectly ne
printf("%lf\n", pi);
fi
fi
Detour: typedef

✤ The typedef declaration provides a way to


declare an identi er as a type alias, to be used
to replace a possibly complex type name

typedef double real;


real pi = 22.0/7;
printf("%lf\n", pi);

✤ Common to use in de ning structs for better


reading
fi
fi
Detour: typedef

✤ The typedef declaration provides a way to


typedef struct
{ declare an identi er as a type alias, to be used
char author[20]; to replace a possibly complex type name
char text[140];
char timestamp[40];
int num_rt; typedef double real;
int num_quote_rt; real pi = 22.0/7;
int num_likes; printf("%lf\n", pi);
} tweet_t;

✤ Common to use in de ning structs for better


reading
fi
fi
Detour: typedef

✤ The typedef declaration provides a way to


typedef struct
{ declare an identi er as a type alias, to be used
char author[20]; to replace a possibly complex type name
char text[140];
char timestamp[40];
int num_rt; typedef double real;
int num_quote_rt; real pi = 22.0/7;
int num_likes; printf("%lf\n", pi);
} tweet_t;

✤ Common to use in de ning structs for better


Convention: append _t to
reading
the type names
fi
fi
Structs: with typedef

typedef struct tweet_t musk_tweet = {


{ "@elonmusk",
char author[20]; "Let's make Twitter maximum fun!",
char text[140]; "2022-04-28 0723 +0530",
char timestamp[40]; 207100,
int num_rt; 36600,
int num_quote_rt; 2700000};
int num_likes;
} tweet_t; printf("%s tweeted %s\n",
musk_tweet.author,
musk_tweet.text);
Structs: with typedef

typedef struct tweet_t musk_tweet = {


{ "@elonmusk",
char author[20]; "Let's make Twitter maximum fun!",
char text[140]; "2022-04-28 0723 +0530",
char timestamp[40]; 207100,
int num_rt; 36600,
int num_quote_rt; 2700000};
int num_likes;
} tweet_t; printf("%s tweeted %s\n",
musk_tweet.author,
musk_tweet.text);

Now tweet_t is a new type


in our program
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

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 tweet_t musk_tweet = {


{ "@elonmusk",
char author[20]; "Let's make Twitter maximum fun!",
char text[140]; "2022-04-28 0723 +0530",
char timestamp[40]; 207100,
int num_rt; 36600,
int num_quote_rt; 2700000};
int num_likes;
} tweet_t;
Structs: with typedef

typedef struct tweet_t musk_tweet = {


{ "@elonmusk",
char author[20]; "Let's make Twitter maximum fun!",
char text[140]; "2022-04-28 0723 +0530",
char timestamp[40]; 207100,
int num_rt; 36600,
int num_quote_rt; 2700000};
int num_likes;
} tweet_t;

Structure de nition:
What does the structure look
like? (Don’t forget the semicolon)
fi
Structs: with typedef

typedef struct tweet_t musk_tweet = {


{ "@elonmusk",
char author[20]; "Let's make Twitter maximum fun!",
char text[140]; "2022-04-28 0723 +0530",
char timestamp[40]; 207100,
int num_rt; 36600,
int num_quote_rt; 2700000};
int num_likes;
} tweet_t;

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

✤ Instantiate two batsmen


using this structure

✤ Print the name of the one


with more runs
fi
Example
typedef struct
✤ De ne a structure that {
char name[20];
represents a batsman using char country_code[4];
int runs;
his last name, country } batsman_t;
code, and total runs

✤ Instantiate two batsmen


using this structure

✤ Print the name of the one


with more runs
fi
Example
typedef struct
✤ De ne a structure that {
char name[20];
represents a batsman using char country_code[4];
int runs;
his last name, country } batsman_t;
code, and total runs
batsman_t b1 = {"Kohli", "IND", 34};
✤ Instantiate two batsmen batsman_t b2 = {"Dhoni", "IND", 42};
using this structure

✤ Print the name of the one


with more runs
fi
Example
typedef struct
✤ De ne a structure that {
char name[20];
represents a batsman using char country_code[4];
int runs;
his last name, country } batsman_t;
code, and total runs
batsman_t b1 = {"Kohli", "IND", 34};
✤ Instantiate two batsmen batsman_t b2 = {"Dhoni", "IND", 42};
using this structure
if (b1.runs > b2.runs)
printf("%s has more runs\n", b1.name);
✤ Print the name of the one else
printf("%s has more runs\n", b2.name);
with more runs
fi
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");
}
Passing a struct to a function
/**
* @brief Reports if the given batsman scored a century.
*
*/ The type of the parameter b
void report_century(batsman_t b)
{ is batsman_t
printf("%s %s a century\n", b.name,
b.runs >= 100 ? "scored" : "did not score");
}
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");
}

Conditional operator: a handy alternative for


if-else in certain situations
The expression a ? b : c evaluates to
b if a is true
c if a is false
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");
}

batsman_t b1 = {"Kohli", "IND", 34};


batsman_t b2 = {"Yadav", "IND", 102};

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");
}

batsman_t b1 = {"Kohli", "IND", 34};


batsman_t b2 = {"Yadav", "IND", 102};

report_century(b1);
report_century(b2);

> ./a.out
Kohli did not score a century
Yadav scored a century
Building more complex data

✤ You rarely want a single


struct

✤ You can create aggregate


data using aggregate 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

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.
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 Note where the & is put and


where it isn’t
✤ A struct containing
arrays.
Passing an array of structs to a function
/**
* @brief Prints the name, country code, and runs of each batsman in the given array.
*
*/
void print_batsmen_info(batsman_t list[], int length)
{
for (int i = 0; i < length; ++i) {
batsman_t b = list[i];
printf("Batsman %d: %s, %s, %d\n", i + 1, b.name, b.country_code, b.runs);
}
}

int main()
{
//...
print_batsmen_info(my_fav, LENGTH);
return 0;
}
Your turn

✤ Write a function that prints


the name of the highest run
scorer given an array of
batsmen
Some remarks on structures

✤ 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

You might also like