









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to structs and enumerations in c and c++ programming languages. Structs are used to group different data types together, while enumerations are user-defined data types. Declaring structures, user-defined data types (typedef), accessing struct members, and sample programs. It also introduces the concept of unions as a member of a struct.
Typology: Slides
1 / 16
This page cannot be seen from the preview
Don't miss anything!










/* The name "my_example" is
called a structure tag */
typedef int Length; makes the name Length a synonym (or alias) for the data type int.
Length a, b, len ; Length numbers[10] ;
Accessing Struct Members
Some_name myptr = &mystruct ; by using the structure pointer operator (the “->“): myptr -> letter ; which could also be written as: (myptr).letter ;
/* This program illustrates creating structs and then declaring and using struct variables. Note that struct personal is an included data type in struct "identity". */ #include <stdio.h> struct personal //Create a struct but don’t reserve space. { long id; float gpa; } ; struct identity //Create a second struct that includes the first one. { char name[30]; struct personal person; } ;
#include <stdio.h>
union status
{
int rank ; char deg[4] ;
} ;
struct personal { long id ; float gpa ; union status level ; } ;
struct identity { char name[30] ; struct personal student ; } ;
else { printf ("Enter degree sought -- ms or phd\n"); scanf ("%s", &jb.student.level.deg); printf ("%s is a %s candidate\n”, jb.name , jb.student.level.deg ); } /* End of else statement */ printf ("%s %ld %f\n” , jb.name , jb.student.id , jb.student.gpa ); printf ("%s%ld %f\n” , ptr->name , ptr->student.id , ptr->student.gpa );
} /* End of program */