Page 1 of 1

C STRUCTURE TUTORIAL PART 1 C STRUCTURE TUTORIAL PART 1 Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 21 April 2010 - 05:28 AM

C STRUCTURE TUTORIAL PART 1


CONTENTS

• I. INTRODUCTION
• II. VOCABULARY
• III. THE C STRUCTURE
• IV. EXAMPLE PROGRAM
• V. REFERENCES

WHAT YOU WILL LEARN IN THIS TUTORIAL

1. You will learn C structure vocabulary.
2. You will learn how to declare a structure.
3. You will learn how memory for a structure is allocated.
4. You will learn how to declare an instance of a structure.

• I. INTRODUCTION

Hello; nice to meet you. Welcome to the “C Structure Tutorial Part 1.”

C++ includes the entire C language; therefore, all C programs, with a few minor exceptions, are also C++ programs.

The C code shown in the tutorial were written using the Microsoft Visual C++ 2008 Express Edition Integrated Development Environment (IDE) and the “C99 subset.”

• II. VOCABULARY

1. What is a structure?

A structure is a collection of logically related variables of different types. A structure groups logically related standard or user defined data types. Once declared, a structure defines a template for creating compound variable type objects which are instances of a structure.

Structures can be nested as elements in other structures. Structures can be used recursively. Structures can be used to define new data types.

2. What is a tag?

A structure type name is referred to as a tag. The tag appears right after the keyword struct.

If your program uses only one structure_variable, you do not need the structure_type_name.

3. What do you call variables that make up a structure?

The variables that make up the structure are called members, elements, or fields.

• III. THE C STRUCTURE

1. How do you declare a structure?

The syntax for simultaneously declaring a structure template and creating compound variable type objects is as follows:


struct structure_type_name{  //  keyword struct and tag, user defined type
type member_name;  //  members, elements, or fields
type member_name;
type member_name;
.
.
.
}structure_variables;  //  compound variable type objects




2. How do you allocate memory for a structure?

Memory is automatically allocated for all the members by the compiler when a structure variable is declared.

3. How do you define an instance of a structure?

After the structure template has been defined, an instance of a physical compound variable type object, a structure_variable can be declared using the following syntax:


struct structure_type_name structure_variable;




• IV. EXAMPLE PROGRAM


//**********************************
//  C STRUCTURE TUTORIAL PART 1
//**********************************

#include<conio.h>   //  contains function prototypes
#include<ctype.h>   //  character handling
#include<stdio.h>   //  input/output
#include<stdlib.h>  //  general utilities
#include<string.h>  //  string handling

struct item  //  structure declaration
{
	int item_stock_number;        //  member, element, or field
	char *item_name;              //  member, element, or field
	char *item_description;       //  member, element, or field
	float item_cost;              //  member, element, or field
	float item_sales_price;       //  member, element, or field
	int item_inventory_quantity;  //  member, element, or field
}item1, item2, item3;             //  compound variable type objects

void f_item1(void);  //  function prototype
void f_item2(void);  //  function prototype
void f_item3(void);  //  function prototype

void p_item1(void);  //  function prototype
void p_item2(void);  //  function prototype
void p_item3(void);  //  function prototype

int main(void)
{
	f_item1();  //  function call
	f_item2();  //  function call
	f_item3();  //  function call

	p_item1();  //  function call
	p_item2();  //  function call
	p_item3();  //  function call

	char ch;
	ch = getch();  //  keeps screen from closing until a key is pressed
		   
	return 0;
}

void f_item1(void)  //  function definition
{
    item1.item_stock_number = 128;
	item1.item_name = "Diamond Cufflinks";
	item1.item_description = "Each 3 Carat Skull Shaped, 9K (375Au) yellow gold.";
	item1.item_cost = 250.00;
	item1.item_sales_price = 2500.00;
	item1.item_inventory_quantity = 10;
}

void f_item2(void)  //  function definition
{
	item2.item_stock_number = 142;
	item2.item_name = "Ruby Cufflinks";
	item2.item_description = "Each 3 Carat Skull Shapped, 9K (375Au) yellow gold.";
	item2.item_cost = 500.00;
	item2.item_sales_price = 5000.00;
	item2.item_inventory_quantity = 20;
}

void f_item3(void)  //  function definition
{
	item3.item_stock_number = 187;
	item3.item_name = "Sapphire Cufflinks";
	item3.item_description = "Each 3 Carat Skull Shapped, 9K (375Au) yellow gold.";
	item3.item_cost = 1000.00;
	item3.item_sales_price = 10000.00;
	item3.item_inventory_quantity = 30;
}

void p_item1(void) //  function definition
{
	printf("Stock Number: %i \n", item1.item_stock_number);
	printf("Name: %s \n", item1.item_name);
	printf("Description: %s \n", item1.item_description);
	printf("Cost: $%.2f \n", item1.item_cost);
	printf("Sales Price: $%.2f \n", item1.item_sales_price);
	printf("Inventory: %i \n\n\n", item1.item_inventory_quantity);
}

void p_item2(void) //  function definition
{
	printf("Stock Number: %i \n", item2.item_stock_number);
	printf("Name: %s \n", item2.item_name);
	printf("Description: %s \n", item2.item_description);
	printf("Cost: $%.2f \n", item2.item_cost);
	printf("Sales Price: $%.2f \n", item2.item_sales_price);
	printf("Inventory: %i \n\n\n", item2.item_inventory_quantity);
}

void p_item3(void) //  function definition
{
	printf("Stock Number: %i \n", item3.item_stock_number);
	printf("Name: %s \n", item3.item_name);
	printf("Description: %s \n", item3.item_description);
	printf("Cost: $%.2f \n", item3.item_cost);
	printf("Sales Price: $%.2f \n", item3.item_sales_price);
	printf("Inventory: %i \n\n\n", item3.item_inventory_quantity);
}




• V. REFERENCES

The C Programming Language by Brian Kernighan and Dennis Ritchie (Englewood Cliffs, N.J.: Prentice-Hall, 1978).

C++: The Complete Reference, Fourth Edition by Herbert Schildt (Berkeley, California: McGraw-Hill/Osborne, 2003).


Is This A Good Question/Topic? 2
  • +

Page 1 of 1