SlideShare a Scribd company logo
C             Programming
                       Language
               By:
    Yogendra Pal
yogendra@learnbywatch.com
  Dedicated to My mother and Father
t
                                                               y
         Keep your notebook with you.

Write important point and questions that comes in your mind

     Solve Mind band exercise.


                                                               C
                                       Rewind when not clear


              Ask Questions by call or SMS or by mail


Keep Watching Keep Learning

THIS IS STRUCTURES


                                   2
Structure
•   Collection of one or more variable.
•   Can be of different data types.
•   Grouped together under a single name.
•   Example:
    – Student can be described as a set of attributes.
       • Name
       • Address
       • Roll no

                              3
Structure…
• Point is a combination of two cordinate.
  – (x , y)
• Rectangle is a pair of points.
  – (x1, y1, x2, y2)




                          4
Structure
• Structure declaration
       struct point {
                int x;
                int y;
       };
  – Use struct keyword to declare a structure.
  – point is the name of structure.
  – x and y are the member of the point structure.
  – Remember semi-colon after curly bracket.
                           5
Structure…
• Structure member name can be same of:
  – Normal variable name in the program.
  – Member name in other structure.
  – Context is different.
• Structure declaration defines a data type.
• A structure declaration do not reserve storage.



                          6
Structure…
• Structure can also be declared as:-
       struct point {
                 int x;
                 int y;
       } a, b, c;
• a, b, c are the variables of point type.
• These variables reserves memory space.


                          7
Structure…
        struct point a;
• Defines a variable of point type.
• Initialization:
        struct point a = {12, 14};

• Initialize x to 12 and y to 14.
• Remember:-
        struct point a;
        a = {12,14};

                             8
Structure…
• You can also initialize each member separately as:-
         a.x = 12;
         a.y = 14;
• You can access structure members by
   – Structure_name.member;
         scanf(“%d %d”, &a.x, &a.y);
         printf(“%d %d”, a.x, a.y);



                           9
Nested Structures
• A rectangle can be declare as a pair of points.
        struct rect {
                struct point p1;
                struct point p2;
        };
• Here, rect structure contains two point structures.
        struct rect r1 = {12, 14, 15, 16};
        printf(“%d %d”, r1.p1.x, r1.p1.y);


                           10
Mind Bend
• Declare a structure named student with the
  following members:-
  – Name [char pointer],
  – address [char pointer],
  – roll_no [int].
• Define a variable s1 in main().
• Initialize all member and print them in main().
• You can also declare address as a separate
  structure.

                              11
Structures and Function
• Pass structures to a function can be done by:-
  – Pass components separately.
  – Pass an entire structure.
  – Pass a pointer to a structure.




                            12
Pass Components Separately
• To initialize a structure dynamically:-
         struct point makepoint (int x, int y) {
                  struct point temp;
                  temp.x = x;
                  temp.y = y;
                  return temp;
         }

• Take 2 integers and return a point structure.


                               13
Pass Structure
• Next we can pass structures to functions as:-
  struct point addpoint (struct point p1, struct point p2) {
           p1.x += p2.x;
           p1.y += p2.y;
           return p1;
  }
• Return value and arguments are structures.




                                14
Pass Pointer to Structure
• For a large structure it is more efficient to pass
  a pointer to structure.
           struct point *pp;
• Define a pointer pp to a structure type point.
           struct point *pp, p1;
           pp = &p1;
           (*pp).x = 12;
           (*pp).y = 14;



                               15
Pass Pointer to Structure
• Parantheses is required because preference of
  structure operator “.” Is higher than indiraction
  operator “*”.
             (*pp).y = 14;

• *pp.y means *(pp.y) which is illegal because y
  is not a pointer.
• Instead (*pp).y, pp->y can also be used.

                             16
typedef
• Create new data type names.
             typedef int Length;

• Create a new name (Length) for int.
  – typedef data_type new_name;
• Length now can be used instead of int in
  declaration and casting.
• Use typedef with Structures.

                           17
To get complete benefit of this tutorial solve all the quiz on
                       www.learnbywatch.com

              For any problem in this tutorial mail me at
                    yogendra@learnbywatch.com
                        with the subject “C”

                     For Other information mail at
                       info@learnbywatch.com


Keep Watching Keep Learning
NEXT IS UNIONS, BITWISE, BIT-FIELDS AND ENUM


                                    18

More Related Content

PPTX
Pointers in c - Mohammad Salman
PPT
Structure in C
PPTX
Access specifier
PPT
C material
PPTX
arrays and pointers
PPTX
C Programming: Structure and Union
PPTX
Pointers in C Programming
PPTX
Pointers in c - Mohammad Salman
Structure in C
Access specifier
C material
arrays and pointers
C Programming: Structure and Union
Pointers in C Programming

What's hot (20)

PDF
GE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PPTX
Array Of Pointers
PPTX
Introduction to c programming
PPTX
Pointers in c++
PPSX
INTRODUCTION TO C PROGRAMMING
PDF
Data Structures
PPTX
C keywords and identifiers
PDF
C Programming Storage classes, Recursion
PPTX
Python programming
PDF
VIT351 Software Development VI Unit3
PPTX
Basic of compiler
PPTX
C language ppt
PPT
Pointers C programming
PPTX
Recursive Function
PPTX
String.pptx
PPTX
BASIC SLICING AND ADVANCED INDEXING IN NUMPY.pptx
PPTX
Type casting
PPTX
PPTX
PPTX
ESIT135: Unit 3 Topic: functions in python
GE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
Array Of Pointers
Introduction to c programming
Pointers in c++
INTRODUCTION TO C PROGRAMMING
Data Structures
C keywords and identifiers
C Programming Storage classes, Recursion
Python programming
VIT351 Software Development VI Unit3
Basic of compiler
C language ppt
Pointers C programming
Recursive Function
String.pptx
BASIC SLICING AND ADVANCED INDEXING IN NUMPY.pptx
Type casting
ESIT135: Unit 3 Topic: functions in python
Ad

Viewers also liked (7)

PPTX
sumeet1
PPT
presentation on ferrite core memory
PPTX
Parallel structure
PPT
1.prallelism
PPT
Presentation on storage device
PPTX
Computer memory
PPTX
TYPES OF MEMORIES AND STORAGE DEVICE AND COMPUTER
sumeet1
presentation on ferrite core memory
Parallel structure
1.prallelism
Presentation on storage device
Computer memory
TYPES OF MEMORIES AND STORAGE DEVICE AND COMPUTER
Ad

Similar to Structures (20)

PPTX
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
PPTX
Fundamentals of Structure in C Programming
PPTX
CHAPTER -4-class and structure.pptx
PDF
PDF
1. structure
PPTX
Structure in C
PPTX
Class and object C++.pptx
PPTX
Structure and Union .pptx program in C with detailed example
PPTX
Explanation about Structures in C language.pptx
PPTX
Structure.pptx Structure.pptxStructure.pptxStructure.pptxStructure.pptxStruct...
PDF
C# 7 development
PPTX
Pf cs102 programming-10 [structs]
PPT
Object Oriented Programming Examples with explanation
PPTX
Structures in C
PDF
Classes and object
PPT
358 33 powerpoint-slides_7-structures_chapter-7
PPTX
Structure in C language
PPTX
Structured Languages
PDF
637225564198396290.pdf
PPTX
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
Fundamentals of Structure in C Programming
CHAPTER -4-class and structure.pptx
1. structure
Structure in C
Class and object C++.pptx
Structure and Union .pptx program in C with detailed example
Explanation about Structures in C language.pptx
Structure.pptx Structure.pptxStructure.pptxStructure.pptxStructure.pptxStruct...
C# 7 development
Pf cs102 programming-10 [structs]
Object Oriented Programming Examples with explanation
Structures in C
Classes and object
358 33 powerpoint-slides_7-structures_chapter-7
Structure in C language
Structured Languages
637225564198396290.pdf
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx

More from Learn By Watch (20)

PPTX
Tutorial 9 fm
PPTX
Phase modulation
PPTX
Demodulation of fm pll detector
PPTX
Demodulation of fm slope and balanced slope detector
PPTX
In direct method of fm generation armstrong method
PPTX
Direct method of fm generation hartley oscillator method
PPTX
Carson's rule
PPTX
Spectrum and power of wbfm
PPTX
Narrow band frequency modulation nbfm
PPTX
General expression of fm signal
PPTX
Angle modulation
PPTX
Frequency division multiplexing
PPTX
Vsb modulation
PPTX
Demodulation of ssb synchronous detector
PPTX
Generarion of ssb phase discrimination method
PPTX
Generarion of ssb frequency discrimination method
PPTX
Ssb modulation
PPTX
Demodulation of dsb sc costas receiver
PPTX
Quadrature carrier multiplexing qam
PPTX
Demodulation of am synchronous detector
Tutorial 9 fm
Phase modulation
Demodulation of fm pll detector
Demodulation of fm slope and balanced slope detector
In direct method of fm generation armstrong method
Direct method of fm generation hartley oscillator method
Carson's rule
Spectrum and power of wbfm
Narrow band frequency modulation nbfm
General expression of fm signal
Angle modulation
Frequency division multiplexing
Vsb modulation
Demodulation of ssb synchronous detector
Generarion of ssb phase discrimination method
Generarion of ssb frequency discrimination method
Ssb modulation
Demodulation of dsb sc costas receiver
Quadrature carrier multiplexing qam
Demodulation of am synchronous detector

Structures

  • 1. C Programming Language By: Yogendra Pal yogendra@learnbywatch.com Dedicated to My mother and Father
  • 2. t y Keep your notebook with you. Write important point and questions that comes in your mind Solve Mind band exercise. C Rewind when not clear Ask Questions by call or SMS or by mail Keep Watching Keep Learning THIS IS STRUCTURES 2
  • 3. Structure • Collection of one or more variable. • Can be of different data types. • Grouped together under a single name. • Example: – Student can be described as a set of attributes. • Name • Address • Roll no 3
  • 4. Structure… • Point is a combination of two cordinate. – (x , y) • Rectangle is a pair of points. – (x1, y1, x2, y2) 4
  • 5. Structure • Structure declaration struct point { int x; int y; }; – Use struct keyword to declare a structure. – point is the name of structure. – x and y are the member of the point structure. – Remember semi-colon after curly bracket. 5
  • 6. Structure… • Structure member name can be same of: – Normal variable name in the program. – Member name in other structure. – Context is different. • Structure declaration defines a data type. • A structure declaration do not reserve storage. 6
  • 7. Structure… • Structure can also be declared as:- struct point { int x; int y; } a, b, c; • a, b, c are the variables of point type. • These variables reserves memory space. 7
  • 8. Structure… struct point a; • Defines a variable of point type. • Initialization: struct point a = {12, 14}; • Initialize x to 12 and y to 14. • Remember:- struct point a; a = {12,14}; 8
  • 9. Structure… • You can also initialize each member separately as:- a.x = 12; a.y = 14; • You can access structure members by – Structure_name.member; scanf(“%d %d”, &a.x, &a.y); printf(“%d %d”, a.x, a.y); 9
  • 10. Nested Structures • A rectangle can be declare as a pair of points. struct rect { struct point p1; struct point p2; }; • Here, rect structure contains two point structures. struct rect r1 = {12, 14, 15, 16}; printf(“%d %d”, r1.p1.x, r1.p1.y); 10
  • 11. Mind Bend • Declare a structure named student with the following members:- – Name [char pointer], – address [char pointer], – roll_no [int]. • Define a variable s1 in main(). • Initialize all member and print them in main(). • You can also declare address as a separate structure. 11
  • 12. Structures and Function • Pass structures to a function can be done by:- – Pass components separately. – Pass an entire structure. – Pass a pointer to a structure. 12
  • 13. Pass Components Separately • To initialize a structure dynamically:- struct point makepoint (int x, int y) { struct point temp; temp.x = x; temp.y = y; return temp; } • Take 2 integers and return a point structure. 13
  • 14. Pass Structure • Next we can pass structures to functions as:- struct point addpoint (struct point p1, struct point p2) { p1.x += p2.x; p1.y += p2.y; return p1; } • Return value and arguments are structures. 14
  • 15. Pass Pointer to Structure • For a large structure it is more efficient to pass a pointer to structure. struct point *pp; • Define a pointer pp to a structure type point. struct point *pp, p1; pp = &p1; (*pp).x = 12; (*pp).y = 14; 15
  • 16. Pass Pointer to Structure • Parantheses is required because preference of structure operator “.” Is higher than indiraction operator “*”. (*pp).y = 14; • *pp.y means *(pp.y) which is illegal because y is not a pointer. • Instead (*pp).y, pp->y can also be used. 16
  • 17. typedef • Create new data type names. typedef int Length; • Create a new name (Length) for int. – typedef data_type new_name; • Length now can be used instead of int in declaration and casting. • Use typedef with Structures. 17
  • 18. To get complete benefit of this tutorial solve all the quiz on www.learnbywatch.com For any problem in this tutorial mail me at yogendra@learnbywatch.com with the subject “C” For Other information mail at info@learnbywatch.com Keep Watching Keep Learning NEXT IS UNIONS, BITWISE, BIT-FIELDS AND ENUM 18