Docsity

Notes on Structures - Data and File Structures | CS 246, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Structures
A Structure is a data type suitable for grouping data elements together.
All of the elements in the structure are logically related.
The elements or fields, which make up the structure use the five basic data
types: int, char, float, double, bool.
We want to define a point in a plan that is two coordinates define each point:
x and y.
The following code fragment shows how to create a structure template to
define the Point structure:
struct Point{
int x;
int y;
};
This declares a NEW data type called Point.
This Point structure consists of two basic data elements, of type integer, one
called x and one called y.
Definition is terminated by a semicolon, because the definition is a
statement.
The structure tag Point identifies or is the name this particular data structure.
We want to define a data type called date.
A date is defined by a month, a day and a year, so we would need at least 3
variables to define that structure.
struct date {
int month;
int day;
int year;
};
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Notes on Structures - Data and File Structures | CS 246 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Structures

A Structure is a data type suitable for grouping data elements together.

All of the elements in the structure are logically related.

The elements or fields, which make up the structure use the five basic data types: int, char, float, double, bool.

We want to define a point in a plan that is two coordinates define each point: x and y.

The following code fragment shows how to create a structure template to define the Point structure:

struct Point{

int x; int y; };

This declares a NEW data type called Point. This Point structure consists of two basic data elements, of type integer, one called x and one called y. Definition is terminated by a semicolon, because the definition is a statement.

The structure tag Point identifies or is the name this particular data structure.

We want to define a data type called date. A date is defined by a month, a day and a year, so we would need at least 3 variables to define that structure.

struct date { int month; int day; int year; };

In essence, structure define a new data type keyword, like int and char , and can be used to create variables.

A structure definition is a definition to the compiler. It does not create any storage space and cannot be used as a variable.

General form of a structure definition is:

struct tag { type variable_name1; type variable_name2; type variable_name3; . . . } structure_variables;

  • the reserved word struct is used to declare a structure. ƒ tag is the name of the structure ƒ type is an of the 5 basic types or a user defined type ƒ variable_name is the name of the element that will hold a given value, it is also called a member variable.

Structure definition forms a template that is used to create variables of the structure type.

At this point in the code, no variable has actually been declared.

Only the form of the data has been defined.

This declaration creates a variable of type date and initializes the members month, day and year respectively to 04, 08, and 2002.

If there are fewer initializations in the list than members of the structure, the remaining members are automatically initialized to 0 or NULL if the member is a pointer.

Jack ={“Jack”, 7, ‘M’};

#include

using namespace std;

struct person {

char *name;

int age;

char gender;

};

This declares a structure. The structure tag is person.

The structure has 3 data elements or members :

ƒ a string name, ƒ an integer age, ƒ and a character gender.

To initialize a variable Jack of type struct we use:

int main() {

struct person Jack = {"Jack", 7, 'M'} ;

return 0;

}

Accessing and referencing members of Structures:

Two operators are used to access members of structure:

. operator: the dot operator

The structure member operator accesses a structure member via the structure variable name.

For example, to print the member name of the variable Jack, we use:

cout << Jack.name;

#include

using namespace std;

struct person {

char *name;

int age;

char gender; };

int main() {

struct person Jack = {"Jack", 7, 'M'} ;

struct person Jill;

Jill.gender = 'F';

Jill.name= "Jill";

cout<< Jack.name <<Jill.name;

return 0;

}

Pointers to Structures:

#include

using namespace std;

struct person {

char *name;

int age;

char gender; };

int main() {

struct person Jack = {"Jack", 7, 'M'} ;

struct person Jill, HumptyDumpty;

Jill.gender = 'F';

Jill.name= "Jill";

cout<< Jack.name<<endl<< Jill.name<<endl;

struct person *HumptyPtr;

HumptyPtr =&HumptyDumpty;

HumptyDumpty.name= "Humpty Dumpty";

cout<< HumptyPtr->name<<endl;

return 0; }

Sample Program Output:

Jack

Jill

Humpty Dumpty

The structure pointer operator (->) accesses a structure member via a pointer to the variable.

struct person HumptyDumpty; Å Line A

struct person *HumptyPtr; Å Line B

HumptyPtr =&HumptyDumpty; Å Line C

HumptyDumpty.name= “Humpty Dumpty”;

cout<< HumptyPtr->name<<endl;

Line A declares a variable HumptyDumpty of type person.

Line B: declares a pointer named HumptyPtr that points to a variable of type person.

Line C: assigns the address of the variable HumptyDumpty to the pointer variable HumptyPtr.

Operations on Structures The only valid operations that may be performed on structures are : ƒ Assigning structure variables to structure variables of the same type ƒ Taking the address (&) of a structure variable. ƒ Accessing the members of a structure variable ƒ Using the sizeof operator to determine the size of a structure variable.

#include using namespace std; void main(void) { struct { int a; int b; } x, y; x.a= 10; y=x; cout<< y.a; return 0;}

When using a structure as a parameter, the type of the argument must match the type of the parameter.

#include using namespace std; int main() { struct { int a, b; char ch; } arg;

arg.a = 1000; f1(arg); }

void f1( struct { int x, y; char ch;} param ) { cout<< param.x); }

This program could not compile.

Most compilers do not allow structures to be passed to functions, by call-by-value.

One way to solve this issue is to declare the structure as a global variable:

#include using namespace std; struct A { int a, b; char ch; }; void f1(A); int main() { A arg; arg.a = 1000; f1(arg); return 0;} void f1(A param) { cout<< param.a; }

This compiles correctly.

Output Sample Program: 1000

The major drawback in passing structures to functions, call-by-value, is the overhead needed to put all the structure elements into the stack (push) and remove all of them from it(pull). Run time performance degrades to unacceptable levels if several structure variables are used in a function call. The solution to this problem is to pass only a pointer to the structure to a function. Structure Pointers

The way to define a pointer to a structure is:

struct tag *structure_variable;

For example: struct person HumptyDumpty, *HumptyPtr;

This declaration creates a pointer called HumptyPtr that points to (or holds the address of) a structure variable.

The statement: HumptyPtr = &HumptyDumpty;

assigns the address of the variable HumptyDumpty to the pointer HumptyPtr.

Using Structure Pointers:

A primary use for structure pointers is to generate a call by reference to functions.

When a pointer to a structure is passed to a function, only the address of the structure variable is pushed (and popped) on the stack.

This makes for very fast function calls.

Example: #include using namespace std; struct addr{ char *name; char *street; char *city; char *state; unsigned long int zip; } address;

struct addr *addrPtr;

void deletef(struct addr *address);

int main() {

struct addr Jackaddress= {"Jack","234 UpHill","Houston", "Texas", 90876}; addrPtr = &Jackaddress; cout< addrPtr->name; deletef(addrPtr); cout<< "Name is now "<< addrPtr->name; return 0; }

void deletef(struct addr *Address){ Address->name= '\0'; }

Program Sample Output:

Name is Jack

Name is now (null)

Typedef:

The keyword typedef provides a mechanism for creating synonyms (or aliases) for previously defined data types.

These data types can be the language defined or user defined. For example: typedef float balance;

This statement tells the compiler to recognize balance as another name for float.

The new name is in addition to, not a replacement for, the existing type name.

Next, we can create a float variable using balance: balance over_due;

Using typedef can make the code easier to read.

Names for structure types are often defined with typedef to create synonyms:

For example: typedef struct { char *face; char *suit; } Card;

This creates the structure type Card without the need for a separate typedef statement.

High Performance Card Shuttling and Dealing Simulation:

-The program represents the deck of cards as an array of structures. -The program uses high-performance card shuffling and dealing program.

wDeck[j] =temp; } }

void deal (Card *wDeck) { int i; for (i=0; i<52; i++) printf("%5s of %8s \n", wDeck[i].face, wDeck[i].suit); }