Difference between revisions of "ES101 - Lesson 9 : Structures"

From Embedded Systems Learning Academy
Jump to: navigation, search
(Created page with "== 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, y...")
 
(Type Define Structure)
Line 9: Line 9:
 
== Type Define Structure ==
 
== Type Define Structure ==
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 +
/* 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()
 
void main()
 
{
 
{
      
+
     /* Your main() code here */
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 23:04, 29 July 2013

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

int main(void)
{
   
}

Passing structure to a function

Pass a copy

Pass by pointer

Assignment