|< C & C++ Preprocessor Directives | Main | C & C++ Type Specifiers 2 >| C-Extra | Microsoft C/Win32 | Site Index | Download |


 

 

 

 

 

MODULE 11

THE C/C++ TYPE SPECIFIERS 1

struct, typedef, enum, union

 

 

 

 

 

 

My Training Period: xx hours

 

From this Module you can jump to the Object Oriented idea and C++ or proceed to extra C Modules or Microsoft C: implementation specific to experience how C is used in the real implementation. The struct lab worksheets are: C/C++ struct part 1 and C/C++ struct part 2. Also the combination of the struct, arrays, pointers and function C worksheet 1, C lab worksheet part 2 and C lab worksheet part 3.

 

The C & C++ programming skills that must be acquired:

 

  • Able to understand and use structure (struct).

  • Able to relate structure, functions and array.

  • Able to understand and use typedef.

  • Able to understand and use union.

  • Able to understand and use enumeration (enum).

 

11.1   Structure (struct)

  • We have learned that by using an array, we only can declare one data type per array, and it is same for other data types.  To use the same data type with different name, we need another declaration.

  • struct data type overcomes this problem by declaring aggregate data types.

  • A structure is a collection of related data items stored in one place and can be referenced by more than one names.  Usually these data items are different basic data types.  Therefore, the number of bytes required to store them may also vary.

  • It is very useful construct used in data structure, although in C++, many struct constructs has been replaced by class construct but you will find it a common in the Win32 programming's APIs.

  • In order to use a structure, we must first declare a structure template.  The variables in a structure are called elements or members.

  • For example, to store and process a student’s record with the elements id_num (identification number), name, gender and age, we can declare the following structure.

struct student {

     char id_num[5];

     char name[10];

     char gender;

     int age;

     };

  • Here, struct is a keyword that tells the compiler that a structure template is being declared and student is a tag that identifies its data structure.  Tag is not a variable; it is a label for the structure’s template.  Note that there is a semicolon after the closing curly brace.

  • A structure tags simply a label for the structure’s template but you name the structure tag using the same rules for naming variables.  The template for the structure can be illustrated as follow (note the different data size):

Structure template illustration

1.     struct  student{

char id_num[5];

char name[10];

char gender;

int age;

} studno_1, studno_2;

 

2.     struct{//no tag

char id_num[5];

char name[10];

char gender;

int age;

}studno_1, studno_2;

 

3.     struct student{

char id_num[5];

char name[10];

char gender;

int age;

};

struct    student    studno_1, studno_2;

struct    student    studno_1, studno_2;

typedef struct TOKEN_SOURCE {

  CHAR    SourceName[8];

  LUID    SourceIdentifier;

} TOKEN_SOURCE, *PTOKEN_SOURCE;

TOKEN_SOURCE  myToken;

11.2    Accessing The Structure Element

studno_1.name = "jasmine";

// a simple structure program example

#include <stdio.h>

#include <stdlib.h>

 

struct student{

  char id_num[6];

  char name[11];

  char gender;

  int age;

  };

 

int main(void)

{

    struct student studno_1;

    

    // studno_1.id_num = "A3214"; // illegal, const char to char[]

    // studno_1.name = "Smith";   // illegal, const char to char[]

    printf("Enter student ID num (5 max): ");

    scanf("%s", studno_1.id_num);

    printf("Enter student name (10 max): ");

    scanf("%s", studno_1.name);

    studno_1.gender = 'M';

    studno_1.age = 30;

    

    printf("\n------------------\n");

    printf("ID number: %s\n", studno_1.id_num);

    printf("Name     : %s\n", studno_1.name);

    printf("Gender   : %c\n", studno_1.gender);

    printf("Age      : %d\n", studno_1.age);

    printf("------------------\n");

    return 0;

}

 

Output:

 

struct typedef enum union program example

// accessing structure element

#include <iostream>

   using namespace std;

 

struct Card

{

   char  *face// pointer to char type

   char  *suit;   // pointer to char type

};

 

void main()

{

    // declare the struct type variables

    struct Card  p;

    struct Card  *SPtr;

    p.face = "Ace";

    p.suit = "Spades";

    SPtr = &p;

    cout<<"Accessing structure element:\n";

    cout<<"\n\'SPtr->suit\'  = "<<SPtr->suit<<endl;

    cout<<"\'SPtr->face\'  = "<<SPtr->face<<endl;

}

 

Output:

 

 

 

 

 

 

 

 

-------------------------------------------------------------------------

 

struct typedef enum union program example

// using the structure member and structure

// pointer operators – accessing structure elements styles...

#include <iostream>

using namespace std;

 

struct Card

{

    char  *face;

    char  *suit;

};

 

int main()

{

    struct Card  p;

    struct Card  *SPtr;

    p.face = "Ace";

    p.suit = "Spades";

    SPtr = &p;

    cout<<"Accessing structure element styles"<<endl;

    cout<<"----------------------------------"<<endl;

    cout<<"Style #1-use p.face:       "<<p.face<<" of "<<p.suit<<endl;

    cout<<"Style #2-use Sptr->face:   "<<SPtr->face<<" of "<<SPtr->suit<<endl;

    cout<<"Style #3-use (*Sptr).face: "<<(*SPtr).face<<" of "<<(*SPtr).suit<<endl;

    return 0;

}

 

Output:

 

struct typedef enum union program example

 

11.3    Arrays Of Structures

struct student{

char id[5];

char name[80];

char gender; int age;

}stud[100];

struct student{

char id[5];

char name[80];

char gender;

};

 

struct    student    stud[100];

cout<<stud[6].name;

for(i=0; i<100; i++)

{

  stud[i].name = " ";

  stud[i].age = 0;

}

// an array structure of student information

#include <iostream>

using namespace std;

 

struct student

{

    char id[6];              // student id number, max. 5 integer number

    char name[50];     // student name, max 49 characters

    char gender;         // student gender Male or Female

    int  age;                 // student age

};

 

void main()

{

    // declaring array of 10 element of structure type

    // and some of the element also are arrays

    struct student stud[10];

    int i = 0;

   

    cout<<"Keying in student data and then display\n";

    cout<<"---------------------------------------\n";

    cout<<"Enter student data\n";

   

    for(i=0; i<2; i++)

    {

         // storing the data

         cout<<"\nID number (4 integer number) student #"<<i<<": ";

         cin>>stud[i].id;

         cout<<"First name student #"<<i<<": ";

         cin>>stud[i].name;

         cout<<"Gender (M or F) student #"<<i<<": ";

         cin>>stud[i].gender;

         cout<<"Age student #"<<i<<": ";

         cin>>stud[i].age;

}

 

    cout<<"\n----------Display the data---------\n";

    cout<<"You can see that the data storage\n";

    cout<<"has been reserved for the structure!\n";

    cout<<"------------------------------------\n";

    for(i=0; i<2; i++)

    {

    // displaying the stored data

    cout<<"\nID number student  # "<<i<<": "<<stud[i].id;

    cout<<"\nFirst name student # "<<i<<": "<<stud[i].name;

    cout<<"\nGender student     # "<<i<<": "<<stud[i].gender;

    cout<<"\nAge student        # "<<i<<": "<<stud[i].age<<"\n";

    }

}

 

Output:

 

array of struct typedef enum union program example

C C++ structure struct template example

 

11.4    Structures And Function

  • Individual structure elements or even an entire structure can be passed to functions as arguments.  For example, to modify the name of the seventh student, let say the function name is modify(), we could issue the following function call:

modify(stud[6].name);

  • This statement passes the structure element name of the seventh student to the function modify().  Only a copy of name is passed to the function.  This means any change made to name in the called function is local; the value of name in the calling program remains unchanged.

  • An entire structure can also be passed to a function.  We can do this either by passing the structure itself as argument or by simply passing the address of the structure.

  • For structure element example, to modify the seventh student in the list, we could use any of the following statements.

modify(stud[6]);

modify(&stud[6]);

  • In the first statement, a copy of the structure is passed to the function while in the second only the address of the structure is passed.

// passing structures to functions and

// a function returning a structure

#include <iostream>

#include <cstdio>

using namespace std;

 

 // -----structure part---

struct vegetable

{

    char   name[30];

    float  price;

};

 

// -----main program-----

int main()

{

    // declare 2 structure variables

    struct vegetable veg1, veg2;

    // function prototype of type struct

    struct vegetable  addname();

    // another function prototype

    int list_func(vegetable);

   

    // functions call for user input...

    veg1  =  addname();

    veg2  =  addname();

    cout<<"\nVegetables for sale\n";

   

    // function call for data display...

    list_func(veg1);

    list_func(veg2);

    cout<<endl;

    return  0;

}

//-------------function----------------

// this functions returns a structure

struct vegetable addname()

{

    char  tmp[20];

    // declare a structure variable

    struct vegetable vege;

 

    cout<<"\nEnter name of vegetable:  ";

    gets(vege.name);

    cout<<"Enter price (per 100gm):  $ ";

    gets(tmp);

    // converts a string to float

    vege.price = atof(tmp);

    return (vege);

}

 

// structure passed from main()

int list_func(vegetable list)

{

    cout<<"\nVegetable name:  "<<list.name;

    cout<<"\nVegetable price:  $"<<list.price;

    return 0;

}

 

Output:

 

C and C++ struct and function

C & C++ programming tutorials

 

 

 

 

 

 

 

 

Further related reading and digging:

 

  1. The struct lab worksheets for your practice are: C/C++ struct part 1 and C/C++ struct part 2.
  2. Also the combination of the struct, arrays, pointers and function C worksheet 1, C lab worksheet part 2 and C lab worksheet part 3.
  3. Check the best selling C/C++ books at Amazon.com.

 

 

 

 

 

|< C & C++ Preprocessor Directives | Main | C & C++ Type Specifiers 2 >| C-Extra | Microsoft C/Win32 | Site Index | Download |


C and C++ Type Specifiers:  Part 1 | Part 2 |