ES101 - Lesson 9 : Structures

From Embedded Systems Learning Academy
Revision as of 23:15, 29 July 2013 by Preet (talk | contribs)

Jump to: navigation, search

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, _t means "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]);
   }
}

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


Pass a copy

Pass by pointer

Assignment