struct advanced programming language. struct struct allows us to create a new data type we can now...
TRANSCRIPT
Struct
Advanced Programming Language
Struct
• Struct allows us to create a new data type
• We can now declare “struct Point” as a new data type and can use it as another type of data:
struct Point {int x;int y;
};
struct Point P1, P2;
P1.x = 10; P1.y = 20;P2.x = 100; P2.y = 150;
printf(“P1.x = %d - P1.y = %d\n”, P1.x, P1.y);printf(“P2.x = %d - P2.y = %d\n”, P2.x, P2.y);
User Defined Data Type
• Sometimes, we need to declare new data type, which is not available in c
• For example, we want to keep the record of student mid-term scores– We need to keep student_id, student_name, and score.
• Struct can be used to declare a new data type to support this need.
Defining a Struct
• struct std_score consists of three elements: – std_id (type of string)– std_name (type of string)– score (type of float)
• Thus, we can now create a new data type called: “struct std_score”
struct std_score {char std_id[15];char std_name[50];float score;
};
Declaring a Struct Variable
• Andy and Bill are now type of “struct std_score” and contain all information as defined previously
• The instances of both variables contain std_id, std_name, and score
struct std_score Andy, Bill;
Assigning Values to Struct
• Each element of Andy can be assigned just like a normal data type as follows
• Note: we cannot use = operator to assign a string to the variable directly (See next slide, how we can do it)
strcpy(Andy.std_id, “53-2074-044-5”);strcpy(Andy.std_name, “Andy Williams”);Andy.score = 78;
strcpy(Bill.std_id, “53-2075-042-2”);strcpy(Bill.std_name, “Bill Gates”);Bill.score = 78;
strcopy()
• strcpy() copies string pointed to by src, to buffer pointed to by dest.
• strncpy() is similar to strcpy() but it copies at most n bytes for src to dest
• Keep in mind: we must have enough space for dest buffer
char *strcpy(char *dest, const char *src)
and
char *strncpy(char *dest, const char *src, size_t n)
Example of String Copy
char buf[20];char *str = “This is a book.”;
strncpy(buf, str, 10);printf(“%s\n”, buf);
strcpy(buf, str);printf(“%s\n”, buf);
This is a This is a book.
The result is:
Retrieving Values from Struct
• We can read the value of each element from the variable just as we do it for a normal data type.
printf(“ID:\t %s\n”, Andy.std_id);printf(“Name:\t %s\n”, Andy.std_name);printf(“Score:\t %.2f\n”, Andy.score);
printf(“ID:\t %s\n”, Bill.std_id);printf(“Name:\t %s\n”, Bill.std_name);printf(“Score:\t %.2f\n”, Bill.score);
Array of Struct
• Struct can also be declared as an array of data type
struct std_score TCT1RA[38], TCT1TA[38];
strcpy(TCT1RA[0].std_id, “53-2074-001-1”);strcpy(TCT1RA[0].std_name, “Somchai”);TCT1RA[0].score = 82;
strcpy(TCT1TA[10].std_id, “53-2075-011-1”);strcpy(TCT1TA[10].std_name, “Tongchai”);TCT1TA[10].score = 74;
printf(“ID:\t %s\n”, TCT1RA[8].std_id);printf(“Name:\t %s\n”, TCT1RA[8].std_name);printf(“Score:\t %.2f\n”, TCT1RA[8].score);
typedef
• typedef allows us to introduce synonyms for types which could have been declared some other way
• The new name becomes equivalent to the type that we wanted– typedef int SIZE
• We can then use SIZE as if we use int– SIZE x; // is now equivalent to int x;
Using typedef for struct
typedef struct {int x;int y;
} POINT;
POINT p1, p2;
p1.x = 5;p1.y = 10;
p2.x = 20;p2.y = 40;
We can now use POINT as if it is a new type of data:
Using typedef for struct
typedef struct {int width;int height;
} RECT;
RECT rect;
rect.width = 5;rect.height = 10;
printf(“Area of rect is %d\n”, rect.width * rect.height);
Question?
• How can you sort the array of struct by the score, by name, or by ID ?
• Hint: – we need to be able to swap two struct– We have to swap element by element to make sure that the
entire struct are moved
• Can you use struct with pointer?– You may need calloc() or malloc() to dynamically allocate
spaces of those records (struct).
Union
Union
• How we use Union is the same as how we use struct
• The different is that all elements of union share the same memory space
charint
double
union un_test {int x;double y;char c;
};
Size of Union
• The size of the union is size of the largest element contained in union
union un_test {int x;double y;unsigned char byte;
} un;
printf(“Size of un is %d byte.\n”, sizeof(un));
Size of un is 8 bytes.
Using union in struct
• Wind_chill is calculated when it is cold• Heat_index is calculated when it is hot• Thus, we know that we don’t need wind_chill and
heat_index at the same time• It is a good idea to save some memory by declaring both
variable as a union
struct conditions {
float temp;union feels_like {
float wind_chill;float heat_index;
}} today;
Another Example of Union
struct var_type{ int type_in_union; union{
float un_float; char un_char; int un_int;
} vt_un;} var_type;
Struct & Union Definition
#include <stdio.h>#include <stdlib.h>
/* code for types in union */#define FLOAT_TYPE 1#define CHAR_TYPE 2#define INT_TYPE 3
struct var_type{ int type_in_union; union{
float un_float; char un_char; int un_int;
} vt_un;} var_type;
Print function
void print_vt(void){
switch(var_type.type_in_union){
case FLOAT_TYPE: printf("%f\n", var_type.vt_un.un_float);
break;case CHAR_TYPE:
printf("%c\n", var_type.vt_un.un_char);
break; case INT_TYPE:
printf("%d\n", var_type.vt_un.un_int);break;
default: printf("Unknown type in union\n");
}}
Main Function
int main(){
var_type.type_in_union = FLOAT_TYPE; var_type.vt_un.un_float = 3.5;
print_vt();
var_type.type_in_union = CHAR_TYPE; var_type.vt_un.un_char = 'a';
print_vt();
exit(EXIT_SUCCESS);}
Question?
• Can we modify the print_vt() so that it can receive any instance of the same type?