Difference between revisions of "ES101 - Lesson 9 : Structures"
(→Passing structure to a function) |
(→Passing structure to a function) |
||
Line 139: | Line 139: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | <BR/> | ||
+ | === Summary === | ||
+ | In the first example, the <code>get_student_info()</code> populates its internal structure and returns it. This returned structure is then '''copied''' into the student array. In the second example, the <code>get_student_info()</code> directly writes the memory of the given structure pointer, and hence large amount of structure data is not returned or copied therefore improving performance. | ||
<BR/> | <BR/> | ||
==Assignment== | ==Assignment== | ||
<Coming soon> | <Coming soon> |
Revision as of 21:19, 30 July 2013
Contents
Objective
You will learn how to create structures.
Structures can help you group information together. For example, if you wish to ask for student age, ID, and name, you can group this data into a single structure that represents one student.
Video Demonstrates :
- <Coming soon>
Type Define Structure
/* It is recommended to define your structure
* before function declarations (outside main)
*/
typedef struct { /* Syntax of struct type-define */
int age;
int id; /* Put variables separated by semicolon */
char name[32];
} student_t; /* Name of the structure read as "student type" */
void main()
{
/* Your main() code here */
}
Declare and use structure
#include <stdio.h>
typedef struct {
int age;
int id;
char name[32];
} student_t;
int main(void)
{
/* One student declared */
student_t student;
/* Access the "members" of the struct using the "dot" . operator. */
printf("Enter student name: "); scanf("%s", &student.name[0]);
printf("Enter student age: "); scanf("%i", &student.age);
printf("Enter student id: "); scanf("%i", &student.id);
/* Declare 100 students */
student_t students[100];
for (int i=0; i<100; i++) {
/* Ask for data for 100 students */
printf("Enter ID of student #%i\n", (i+1));
/* Access student array element by index, then the dot to access member */
scanf("%i", &student[i].id);
}
}
Passing structure to a function
Structure(s) can be passed to a function just like a variable. It is recommended that you pass a pointer instead of a copy because passing a very large structure to a function as a copy can consume a lot of memory.
Let's demonstrate both uses :
#include <stdio.h>
typedef struct {
int age;
int id;
char name[32];
} student_t;
/* This function returns a structure back */
student_t get_student_info(void);
int main(void)
{
/* Declare 100 students */
student_t students[100];
for (int i=0; i<100; i++) {
students[i] = get_student_info();
}
}
student_t get_student_info(void)
{
student_t student;
printf("Enter student name: "); scanf("%s", &student.name[0]);
printf("Enter student age: "); scanf("%i", &student.age);
printf("Enter student id: "); scanf("%i", &student.id);
return student;
}
Now, let's do the same thing, but this time, using a pointer to populate student structure:
#include <stdio.h>
typedef struct {
int age;
int id;
char name[32];
} student_t;
void get_student_info(student_t *student);
int main(void)
{
/* Declare 100 students */
student_t students[100];
/* This method is more efficient */
for (int i=0; i<100; i++) {
get_student_info(&student[i]);
}
}
void get_student_info(student_t *student)
{
/* Access the members of the structure using -> operator
* because student is now a pointer.
* No need to return anything because we directly modify
* structure's memory since pointer tells us where it is.
*/
printf("Enter student name: "); scanf("%s", &student->name[0]);
printf("Enter student age: "); scanf("%i", &student->age);
printf("Enter student id: "); scanf("%i", &student->id);
}
Summary
In the first example, the get_student_info()
populates its internal structure and returns it. This returned structure is then copied into the student array. In the second example, the get_student_info()
directly writes the memory of the given structure pointer, and hence large amount of structure data is not returned or copied therefore improving performance.
Assignment
<Coming soon>