3-Struct Data Type
3-Struct Data Type
サルバド-ル・フロランテ 1
Q: What is a “structure”?
• Group of elements, called “members”, of
possibly different data types (heterogeneous)
• Layman’s term for structure is “record”
• Example
Student Record
▪ ID #
▪ Name (Last, First, Middle)
▪ Birthday (Month, Day, Year)
▪ Course
▪ GPA
2
Graphical Representation of a Structure and Its
Members (Parent-Child Relationship)
3
Three Graphical Examples
4
Nested Structure Graphical Example
5
Q: How do you declare a structure in C?
struct [tag-name] {
<data type> <member-name>;
:
:
<datatype> <member-name>
} [structure-var-name];
6
Variation #1 – no tag name, no structure var name
struct {
char ch;
int i;
float f;
double d;
};
• Compiler warning: “unnamed struct/union
that defines no instances”
• Normally used with typedef
7
Variation #2 – with tag name, no struct var name
struct sampleTag {
char ch;
int i;
float f;
double d;
};
• Note: only the structure data type is declared
• Recommended practice in CCPROG2
8
Variation #3 – no tag name, with struct var name
struct {
char ch;
int i;
float f;
double d;
} x; // x is the instance or structure variable
9
Q&A
Q: What is the data type of x in the following
declaration?
int x;
A: int
10
Q&A
Q: What is the data type of x in the following
declaration?
struct {
char ch;
int i;
float f;
double d;
} x;
11
Variation #3 – no tag name, with struct var name
struct {
char ch;
int i;
float f;
double d;
} x; // x is the instance or structure variable
12
Variation #4 –with tag name, with struct var name
struct sampleTag {
char ch;
int i;
float f;
double d;
} x, y, z; // note: 3 structure variables
13
Sample Codes
/* 1st step: declare the structure data type */
struct sampleTag {
char ch;
int i;
float f;
double d;
};
14
Sample Codes…
/* 1D array of structures */
struct sampleTag Z[100];
/* 2D array of structures */
struct sampleTag M[2][3];
15
Sample Codes…
/* structures and functions */ structure type
struct sampleTag function
A_func(struct sampleTag param)
{
struct sampleTag temp; structure type
… parameter
return temp;
}
structure type local
int main() variable
{
struct sampleTag s, t;
…
t = A_func(s);
…
16
Sample Codes… structure pointer
/* structures pointers and functions */ type function
struct sampleTag *
B_func(struct sampleTag Z[]) Structure Array
{
struct sampleTag *pTemp; Name parameter
…
return pTemp;
}
int main()
{
struct sampleTag Z[10], *ptr; structure pointer
… type local variable
ptr = B_func(Z);
…
17
Q: What operations can be performed
on structures?
1. Get the memory address of the structure
variable via & (address-of) operator
2. Access the structure members using the
structure member operator (dot symbol)
3. Assign a structure value to a structure
variable of the same data type (structure-to-
structure assignment)
18
Q: How do you get the address of of a
structure variable?
Use & operator
//… assume struct sampleTag was declared above
int main()
{
struct sampleTag s, t, u;
struct sampleTag *ptr, *pList;
19
Q: How do you access a structure
member?
• Syntax: Take note of the dot symbol
.
struct-var-name member-name
// example
struct sampleTag s;
s.ch = ‘A’;
s.i = 123;
s.f = 8.5f;
s.d = 3.1416;
20
// more examples int main ()
struct phoneTag { {
int number ; struct phoneTag landline;
char name [30]; struct dateTag birthday;
}; …
// access members
struct dateTag { landline.number = 5244611;
int month ; strcpy(landline.name, “DLSU”);
int day; …
int year;
}; birthday.month = 10;
birthday.day = 24;
birthday.year = 2016;
21
// & and . operators int main ()
struct phoneTag { {
int number ; struct phoneTag landline;
char name [30]; struct dateTag birthday;
}; …
// input data via scanf()
struct dateTag { scanf(“%d”, &landline.number);
int month ; scanf(“%s”, landline.name);
int day; …
int year;
}; scanf(“%d %d %d”,
&birthday.month,
&birthday.day,
&birthday.year);
22
Q: How do you assign a structure to
another structure?
• Syntax for structure-to-structure assignment
struct-var-name = struct-var-name
// example
struct sampleTag s, t;
s.ch = ‘A’;
s.i = 123;
s.f = 8.5f;
s.d = 3.1415;
24
// INVALID example int main ()
struct phoneTag { {
int number ; struct phoneTag landline;
char name [30]; struct phoneTag office;
}; struct dateTag birthday;
struct dateTag anniversary;
struct dateTag { …
int month ; landline.number = 5244611;
int day; strcpy(landline.name, “DLSU”);
int year; …
}; office = landline;
…
birthday.month = 10;
birthday.day = 24;
birthday.year = 2016;
…
anniversary = office; // INVALID!
25
Q: How do you specify a nested
structure?
A member of a structure can be another
structure. For example:
Student Record
▪ ID #
▪ Name
▪ Last
▪ First
▪ Middle
▪ Birthday
▪ Month
▪ Day
▪ Year
26
// declaration, 1st version
typedef char Str20[21];
struct studentTag {
int ID;
struct {
Str20 last, first, middle;
} name;
struct {
int month, day, year;
} birthday;
};
27
// 2nd version struct studentTag {
typedef char Str20[21]; int ID;
struct nameTag name;
struct nameTag { struct dateTag birthday;
Str20 last, first, };
middle;
}; struct studentTag s1, s2;
struct studentTag S[40];
struct dateTag {
int month, day, year;
};
28
// 3rd version with typedef struct studentTag {
typedef char Str20[21]; int ID;
nameType name;
struct nameTag {
dateType birthday;
Str20 last, first, middle;
}; };
29
// access members of a nested structure
struct studentTag student;
student.ID = 12345;
strcpy(student.name.last, “TAN”);
strcpy(student.name.first, “ALEX”);
strcpy(student.name.middle, “LIM”);
student.birthday.month = 8;
student.birthday.day = 8;
student.birthday.year = 1988;
30
// input via scanf() members of a nested structure
struct studentTag student;
scanf(“%d”, &student.ID);
scanf(“%s”, student.name.last);
scanf(“%s”, student.name.first);
scanf(“%s”, student.name.middle);
scanf(“%d”, &student.birthday.month);
scanf(“%d”, &student.birthday.day);
scanf(“%d”, &student.birthday.year);
31
Exercise: Assume the following declarations:
struct A {
int i; Write the word VALID if the assignment statement
float f; below is syntactically correct, otherwise write INVALID.
};
struct B {
int i;
1. a2.i = a1.i;
float f; 2. b1.f = a2.f;
}; 3. b2.f = a1.i;
4. c1.f = a1.f;
struct C { 5. a2 = a1;
float f; 6. b1 = b2;
int i; 7. a1 = b1;
}; 8. b2 = a1;
9. c1 = a2;
struct D {
struct A a;
10. b1 = c2;
struct C c; 11. d1.a = a1;
}; 12. d2.c.f = a2;
struct A a1, a2; 13. c2 = d2.a;
struct B b1, b2; 14. a1 = d1.A;
struct C c1, c2; 15. d2.a.f = d2.c.i;
struct D d1, d2; 32
Q. Can you pass a structure as
parameter?
• The value of a structure can be passed as a function
parameter
• The value of the address of a structure can be passed
as a function parameter
• A function can return the value of a structure as its
value (i.e., the function has a struct data type)
• A function can return the address of a structure as its
value (i.e., the function has a struct pointer data type)
33
Structure data type as parameter and return type
/* structures and functions */ structure type
struct sampleTag
function
A_func(struct sampleTag param)
{
struct sampleTag temp;
…
structure type
return temp; parameter
}
int main()
{ structure type local
struct sampleTag s, t; variable
…
t = A_func(s);
…
34
Sample codes…
// assume struct sampleTag was int
// already declared before the following main ()
// functions… {
struct sampleTag x;
void
PrintStruct_ver1(struct sampleTag s) x.ch = ‘A’;
{ x.i = 123;
printf(“%c %d %f %lf\n”, x.f = 8.8f;
s.ch, s.i, s.f, s.d); x.d = 3.1416;
}
// parameter is a structure
void PrintStruct_ver1 ( x );
PrintStruct_ver2(struct sampleTag *ptr)
{ // parameter is address of a struct.
printf(“%c %d %f %lf\n”, PrintStruct_ver2( &x );
(*ptr).ch,
(*ptr).i, return 0;
(*ptr).f, }
(*ptr).d);
}
36
Exercise: Implement InputStruct() function
void
// assume struct sampleTag was
InputStruct( __________ )
// already declared before the following
{
// functions…
// implement this function
}
void
PrintStruct_ver1(struct sampleTag s)
int
{
main ()
printf(“%c %d %f %lf\n”,
{
s.ch, s.i, s.f, s.d);
struct sampleTag x;
}
InputStruct( ________ );
void
PrintStruct_ver2(struct sampleTag *ptr)
// parameter is a structure
{
PrintStruct_ver1 ( x );
printf(“%c %d %f %lf\n”,
(*ptr).ch,
// parameter is address of a struct.
(*ptr).i,
PrintStruct_ver2( &x );
(*ptr).f,
(*ptr).d);
return 0;
}
}
37
INCORRECT solution
void
// assume struct sampleTag was InputStruct( struct sampleTag s)
// already declared before the following {
// functions… scanf(“%c %d %f %lf”,
&s.ch, &s.i, &s.f, &s.d);
void }
PrintStruct_ver1(struct sampleTag s)
{ int
printf(“%c %d %f %lf\n”, main ()
s.ch, s.i, s.f, s.d); {
} struct sampleTag x;
void InputStruct( x );
PrintStruct_ver2(struct sampleTag *ptr)
{ // parameter is a structure
printf(“%c %d %f %lf\n”, PrintStruct_ver1 ( x );
(*ptr).ch,
(*ptr).i, // parameter is address of a struct.
(*ptr).f, PrintStruct_ver2( &x );
(*ptr).d);
} return 0;
} 38
CORRECT solution
void
// assume struct sampleTag was InputStruct( struct sampleTag *ptr)
// already declared before the following {
// functions… scanf(“%c %d %f %lf”,
&(*ptr).ch,
void &(*ptr).i ,
PrintStruct_ver1(struct sampleTag s) &(*ptr).f,
{ &(*ptr).d );
printf(“%c %d %f %lf\n”, }
s.ch, s.i, s.f, s.d);
} int
main ()
void {
PrintStruct_ver2(struct sampleTag *ptr) struct sampleTag x;
{
printf(“%c %d %f %lf\n”, InputStruct( &x );
(*ptr).ch,
(*ptr).i, // etc …
(*ptr).f,
(*ptr).d);
} return 0;
} 39
Q: How do you access a structure
member indirectly?
Use the structure pointer operator denoted by
->
to access a structure member indirectly via a
structure pointer variable
Syntax:
structure ptr var name -> member name
40
Sample codes…
// assume struct sampleTag was
// already declared before the following
// functions…
void
PrintStruct_ver1(struct sampleTag s)
{
printf(“%c %d %f %lf\n”,
s.ch, s.i, s.f, s.d);
}
void void
PrintStruct_ver2(struct sampleTag *ptr) PrintStruct_ver2(struct sampleTag *ptr)
{ {
printf(“%c %d %f %lf\n”, printf(“%c %d %f %lf\n”,
(*ptr).ch, ptr->ch,
(*ptr).i, ptr->i,
(*ptr).f, ptr->f,
(*ptr).d); ptr->d);
} }
41
-> makes codes on the right shorter and less prone to error
void void
PrintStruct_ver2(struct sampleTag *ptr) PrintStruct_ver2(struct sampleTag *ptr)
{ {
printf(“%c %d %f %lf\n”, printf(“%c %d %f %lf\n”,
(*ptr).ch, ptr->ch,
(*ptr).i, ptr->i,
(*ptr).f, ptr->f,
(*ptr).d); ptr->d);
} }
void void
InputStruct( struct sampleTag *ptr) InputStruct( struct sampleTag *ptr)
{ {
scanf(“%c %d %f %lf”, scanf(“%c %d %f %lf”,
&(*ptr).ch, &ptr->ch,
&(*ptr).i , &ptr->i ,
&(*ptr).f, &ptr->f,
&(*ptr).d ); &ptr->d );
} }
42
COMMON ERROR alert!!!
Q: What is wrong with the ff code?
struct sampleTag s;
struct sampleTag *ptr;
43
Accessing elements of a list of structures
// allocate space (static mem. alloc.)
struct sampleTag A[10];
// recall that: A[i] == *(A + i)
// print the members of A[i] // print the members of A[i]
printf(“%c %d %f %lf\n”, printf(“%c %d %f %lf\n”,
A[i].ch, ( *(A + i) ).ch,
A[i].i, ( *(A + i) ).i,
A[i].f, ( *(A + i) ).f,
A[i].d); ( *(A + i) ).d);
45
47
48
49
50
51
-- The End --
サルバド-ル・フロランテ 52