C structure Tutorial - Part 1


Khaoz.Tk
 
PortalHomeSearchMemberlistCalendarFAQLog inRegisterUsergroups
Increase font size   Decrease font size

Share | 
 

 C structure Tutorial - Part 1

View previous topic View next topic Go down 
AuthorMessage
_-_EVIL-LOCO_-_
Programmer
Programmer



Posts : 7
Join date : 2010-06-02

PostSubject: C structure Tutorial - Part 1   Mon Jul 05, 2010 3:15 am

Introduction

Hello I am _-_EVIL-LOCO_-_, and today I will teach you guys the basics of C structure. This tutorial contains information about the C language. Usually C++ includes the entire C language. Therefore, all C programs, with a few minor exceptions, are also C++ programs. As you can see below, written examples by me and other expert are used in Microsoft Visual C++ 2008 Express Edition(Also if you want the download file, please contact me) Integrated Development Environment(IDE). And also, the"C99 subset" edition. I will keep this tutorial short and understandable as possible.

What will be our lesson today and additional references:

1. You will learn basic vocabulary of C structure.
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.

So let's get started....


Vocabulary of C Structure

You may take notes if you want. But I rather do that just in case if you had C classes in your school schedule Smile.

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.

Examples and Info of C Structure

1) How do you declare your structure?

Well it is like forcing an function. Meaning, that the syntax for simultaneously declaring a structure template and creating compound variable type objects is as follows:



Code:

//*****************
//  Written By Elcric
//*****************
struct structure_type_name{  //  keyword struct and tag, user defined [color=violet]type[/color]
[color=violet]type[/color] member_name;  //  members, elements, or fields
[color=violet]type[/color] member_name;
[color=violet]type[/color] 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.
Which we shown in Example #1.

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:


Code:
struct structure_type_name structure_variable;

Main Example of C Structure

Code:
 
//**********************************
//  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);
}



Thank for reading and I hope you learn something

Credits and 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).

~_-_EVIL-LOCO_-_
Back to top Go down
View user profile
kHaoZ
ŠїŦĕ ǾŵŇĕŖŠ
ŠїŦĕ ǾŵŇĕŖŠ

avatar


Posts : 210
Join date : 2010-03-22
Location : Cali

PostSubject: Re: C structure Tutorial - Part 1   Mon Jul 05, 2010 8:08 am

Hmm I understand this one a bit, I'll study it some more and see if I can master it.

_________________
~[Khaos] Leader



Back to top Go down
View user profile
 

C structure Tutorial - Part 1

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
KhaoticGamerz.Tk :: PC Tutorials and Sources :: C/C#/C+/C++-
Jump to:  
Copyright 2010 © KhaoticGamerz
Private forum on Forumotion | © phpBB | Free forum support | Contact | Report an abuse | Free forum