0% found this document useful (0 votes)
32 views15 pages

Ch-6 Struct

This document discusses data structures in C programming. It explains that structures allow grouping of common variables into a single collection. A structure defines a custom data type that reserves contiguous memory for related variables of different data types. The document outlines how to define a structure with the struct keyword, declare structure variables, access member variables with the dot operator, and initialize structure variables with an initializer list within curly braces. It also discusses creating arrays of structures to store multiple lists.

Uploaded by

ERMIAS Amanuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views15 pages

Ch-6 Struct

This document discusses data structures in C programming. It explains that structures allow grouping of common variables into a single collection. A structure defines a custom data type that reserves contiguous memory for related variables of different data types. The document outlines how to define a structure with the struct keyword, declare structure variables, access member variables with the dot operator, and initialize structure variables with an initializer list within curly braces. It also discusses creating arrays of structures to store multiple lists.

Uploaded by

ERMIAS Amanuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Structure

Structure
• So far we have seen a variables whose data types are very simple.
They are a numbers of wither integer or float point with a specific
range.
• There types of variables, who only have a single value to their name,
are known as basic variable or primitives,
• Sometime it is advantageous to group common variables in to a single
collection. For example a date would require a day, month and year.
• Array is a data structure which holds multiple numbers of objects having
the same basic property(data types) in a contiguous memory slots.
• It is possible to reserve contiguous memory space for aggregates of
elements of different data types each. A data type which is created to
reserve such type of memory space is called user defined data type.
• Structure definition is a user defined variable type which is a grouping of
one or more variables. Structure definition could be a date which may be
made up of three int members.
• Before creating a structure variable you must create a structure
definition.
Defining structure
• Is giving a compiler a blue print for creating your type.
• Note: when you create a variable of any kind, you must give it a
unique name that is different than its type.
• Syntax

struct structname
{
datatype1 variable1;
datatype2 variable2;
};
• Writing a structure definition begins with the word struct flowed by
the type to be and ended with a structure block that is ultimately
terminated with a semicolon.
• Within the structure block you declare all the member variables you
want associated with that type but don’t try to initialize them.
• The data members (synonym for member variables) of a structure
won’t be created until a variable based on the structure is crated. It is
just a description of a storage unit.
• That storage unit isn’t reserved until you create a variable with it.
• Structure definition has the same type of scoping as a variable.
• Example: defining a structure Struct date
struct student {
{ int day;
int id; int month;
char name[15]; int year;
}; };
Declaring and using struct data types
• Once you have defines a structure you can create a variable from it
just as you would any other variable.
date birthday;
• The above declaration statement would create a variable called
birthday whose type is the structure date. The variable contains three
parts: day, month and year.
What is the difference between the following
int I;
student std1;
Initializing structure variables
• To initialize a structure variable's members, you follow the original
declaration with the assignment operator[=].
• Next you will define an initialization block which is a list of initializers
separated by commas and enclosed in curly braces.
• Lastly, you end it with a semi colon.
• These values are assigned to member variables in the order that they
occur.
Examples
date nco_birthday ={19,8,1979};
student std1 ={“Abebe”,”RTCH-0000/00”};
• The first one creates a variable called nco_birthday and initializes it to a list of
values.
• If you try to assign more values then are the member variable, you will get a
compiler error.
• It is possible to use any expressions that you normally would, but the expression
must result in a value.
int myday = 19;
int mymnoth = 5;
date nco_birthday = {myday, mymonth+3, 2001-22};
• date nco_birthday ={19,8,1979};
Is not equal to the following
• date nco_birthday;
• nco_birthday ={19,8,1979};

• You must access each member individually;


Accessing members of a structure variable
• You can use a member variable in any place you’d use a normal
variable, but you must specify it by the structure variable’s name as
well as the member variable’s name using the member operator.
• To specify that you want a member of a specific structure variable,
you use the structure member operator which is the period(dot).
• Use the structure’s name, follow with the period and end with the
member.
• EXAMPLES!
Variable with Definition
• The syntax of struct also allows you to create variables based on a
structure definition without using two separate statements.
struct {
struct
inttagx,y;
{
}point;
member(s),
Has 2 members.

=
}variable;
B.c. the structure definition is not named it cannot be used
• Example:
elsewhere a point
to create variable
a variable of right after
the same the type.
struct pointtag structure is defined:
struct pointtag
struct pointtag {
int x,y;
{ };
int x,y;
}point; pointtag point;
Possible options

struct pointtag
struct pointtag
{ {
int x,y; int x,y;
}point1,point2; }point1={0,0},point2{0,0};
Array of struct
• What if lots of lists are neede?
END

You might also like