How Do We Declare
Structs and use in a C Program?
Using the following program...
#include <stdio.h>
struct student
{ char student_name [15];
int student_id, student_hours;
float studentgpa;
struct student * nextgpa;
int main()
{
struct student active = {"Homer Simpson",12345,150,3.27};
struct student alumni = {"Raul Gonzalez", 23456, 98, 1.34};
printf("Name: %13s %14s\n",active.student_name, alumni.student_name);
printf("ID: %13d %14d\n",active.student_id, alumni.student_id);
printf("gpa: %13.3f %14.3f\n",active.gpa, alumni.gpa);
printf("Hours: %13d %14d\n",active.total_hrs, alumni.total_hrs);
return 0;
}
By looking at each part of the program we
can identify them.
| struct student |
This is only the name of the struct, which can have anyone that
you want.
{
|
char [15]
studentname |
Here, we are reserving 15 bytes of memory for each name, In
other words, we are creating a string of the size of 30 characters.
Therefore, there will be a space provided of 15 characters to
include the name of the student.
| int studentage, student credithours |
Here we are reserving just 2 bytes of space for each variable
because an integer uses 2 bytes. They're both numbers that is why
they are integers.
| float studentgpa |
Since gpa is a real number we are reserving 4 bytes of space to
declare GPA, it's a real number (in other words it requires
decimals).
| struct student * nextgpa |
Finally, if we want to know where the base address of the struct
is located we need a pointer. That's why we set up this pointer to
know where the base address of the struct is, and from there we can
find any element in the struct.
}
This basically shows the logic behind building structs we have
to know what kind of values we want and then we define our
variables.
This is stored in RAM in the following way.
7690 |
7691 |
7692 |
7693 |
7694 |
7695 |
7696 |
7697 |
7698 |
7699 |
7700 |
7701 |
H |
o |
e |
r |
|
S |
i |
m |
p |
s |
o |
n |
7702 |
7703 |
7704 |
7705 |
7706 |
7707 |
7708 |
7709 |
7710 |
711 |
7712 |
7713 |
\0 |
|
|
12345 |
150 |
3.15 |
|
As all data type, the data type struct has a base address:
Assume that variable active (of data type struct
student) has the base address 7690 (and requiring 27 contiguous
bytes, to 7712).
Questions:
1. If we want to include a name in our struct,
how do we tell C++ to do that ???
a) By sending a letter to the pope
b) char name[30], because we a re declaring a
space available of 30 letters for each name or record.
c) by going to Christian's house and asking him
d) int 1000000
2.- If we want to know were our struct is located
in RAM , it's possible by doing which of the following:
a) Singing the national anthem
b) by declaring a pointer for example mystruct *
hereitis (mystruct the name of the struct, hereitis will hold the
base adress of the struct)
c) by sending an e-mail to ricky martin
3.- if we have the following struct
yo{
char [8]
int hola, bye
float gpa}
How many bytes do I need per record
a) how am I supposed to know only God knows
b) Kirs and Charles ages squred + the number of
people in the class
c) 16
d) none of the above
Answers:
1. b) char name[30], because we a re declaring a
space available of 30 letters for each name or record.
2. b) by declaring a pointer for example mystruct
* hereitis (mystruct the name of the struct, hereitis will hold the
base adress of the struct)
3. c) 16
References:
http://www.dgp.toronto.edu/~ajr/209/notes/struct.html
http://www.ecs.soton.ac.uk/~dem/teaching/maths/proginc/chapter3/chapter3.htm
http://www.cdf.utoronto.ca/~tff/180/practicals/11structSort.html