'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Programming in C Chapter 10 Structures and Unions
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
C Slides and captured lecture (video and sound) are available at:
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Computer Science 210 Computer Organization Pointers.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Advanced Data types and Sorting
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
Introduction to Programming 3D Applications CE Lecture 12 Structure Pointers and Memory Management in C.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Structures. Structures (1) u Structures are C’s way of grouping collections of data into a single manageable unit. –This is also the fundamental element.
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
POINTERS.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Pointers *, &, array similarities, functions, sizeof.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
LINKED LISTS.
User-Written Functions
Computer Science 210 Computer Organization
CS1010 Programming Methodology
Functions, Part 2 of 2 Topics Functions That Return a Value
CS1010 Programming Methodology
Pointers.
CS1010 Discussion Group 11 Week 12 – Structured Structures.
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Computer Science 210 Computer Organization
Lecture 9 Structure 1. Concepts of structure Pointers of structures
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Functions, Part 2 of 3 Topics Functions That Return a Value
Pointers.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures Use of structure pointers Structures and arrays Assigning structures

Purpose of structures Computing applications often need data of different kinds to be associated. A typical place where this occurs is in a record. For example a database record for a particular student might contain the student number, full name, course-code and year of entry.

Record declaration struct student { char studno[12]; char name[50]; char course[7]; int startyear; };

More examples struct coord { float x,y; }; struct complex { float real,imaginary; }; The data might have different types but does not have to be of different types. Structures also help with the naming of related parts of a record object.

Allocating memory to records The above declarations don't create any space for data storage, but they provide a template for data of the appropriate types to be allocated. Let's allocate some storage to data variables: struct coord origin, cursor; /* 2 x-y coordinates called origin and cursor */ struct student sdn_year2[50]; /* array for 50 students */

Defining a new data type Better to have one word e.g. COORD to declare the type of data than 2 words e.g. struct coord. This needs a typedef statement: struct coord { float x,y; }; typedef struct coord COORD; /* COORD now same as struct coord */ COORD origin, cursor; /* declare 2 x-y coordinates called origin and cursor */

Use of the dot structure member access operator Members x and y of origin and cursor can now be accessed as origin.x, origin.y, cursor.x and cursor.y e.g. origin.x=3.5; origin.y=2.0;

Having defined a new data type We can declare stuctured data with a single word in a similar way that we use other built-in data types such as float and int. This simplification is useful as the name of the data type is going to appear in many places within our program, e.g. for declaring types of local data within functions or function parameters. It may not seem the case, but having many smaller functions which communicate cleanly is going to make programming much, much easier.

Functions w hich communicate using structured parameters float distance(COORD a, COORD b){ /* calculate distance between a and b */ float z,vert,horiz; /* z = distance, vert = y1 - y2, horiz = x1 - x2 */ horiz=a.x - b.x; /* the horizontal distance */ vert=a.y - b.y; /* the vertical distance */ /* calculate z as the hypotenuse of a right angle triangle */ z=sqrt((horiz*horiz) + (vert*vert)); /* Pythagorus theorem: */ return z; /* z*z = x*x + y*y */ }

Functions which communicate using structured return values COORD getcoord(void){ /* note returned type is COORD */ /* prompts for and returns a coordinate */ COORD temp; printf("Enter x and y coordinates \n"); scanf("%f%f",&temp.x,&temp.y); return temp; }

A program using distance and getcoord part 1 #include /* needed to use sqrt() square root */ struct coord { float x,y; }; /* declare structure coord as having x and y members */ typedef struct coord COORD; /* COORD is now same as struct coord */ COORD origin, cursor; /* declare 2 x-y coordinates called origin and cursor */ /* function prototypes */ float distance(COORD a, COORD b); COORD getcoord(void);

A program using distance and getcoord part 2 int main(void){ COORD origin, cursor; float separation; printf("enter details for origin:\n"); origin=getcoord(); printf("enter details for cursor:\n"); cursor=getcoord(); separation=distance(origin,cursor); printf("the distance between origin and cursor is %f\n",separation); return 0; }

Use of structure pointers Members x and y of coordinates a and b can also be accessed through pointers to a and b so that if pointer p stores the address of a: p=&a; then p->x directly accesses member x of a or a.x.

Rewrite the above program using pointers /* declarations above here of headers, struct coord and typedef COORD same as before so not repeated */ float distance(COORD *a, COORD *b); /* a and b are now pointers to COORD */ void getcoord(COORD *t); /* inputs coordinate through COORD* pointer t */

Rewrite using pointers slide 2 int main(void){ COORD origin, cursor, *orig,*curs; orig=&origin; curs=&cursor; /* store addresses in pointers orig and curs */ float separation; printf("enter details for origin:\n"); getcoord(orig); printf("enter details for cursor:\n"); getcoord(curs); separation=distance(orig,curs); printf("the distance between origin and cursor %f\n", separation); getch(); return 0; }

Rewrite using pointers slide 3 float distance(COORD *a, COORD *b){ /* calculate distance between a and b */ float z,vert,horiz; /* z = distance, vert = y1 - y2, horiz = x1 - x2 */ horiz=a->x - b->x; /* horizontal distance note -> pointer syntax */ vert=a->y - b->y; /* the vertical distance */ /* calculate z as the hypotenuese of a right angle triangle */ z=sqrt((horiz*horiz) + (vert*vert)); /* pythagorus theorem: */ return z; /* z*z = x*x + y*y */ } void getcoord(COORD *t){ /* inputs x-y coordinate using pointers */ printf("please enter x and y coordinates\n"); scanf("%f%f",&t->x,&t->y); /* -> has higher precedence than & */ }

Structures and arrays Having individual names for 10 coordinates is very awkward. We can declare the data as an array: typedef struct coord { float x,y; } COORD; COORD graph[10]; We can then assign members of our structure array like this: graph[9].x = 12.5; graph[9].y = 7.3; /* assign values to tenth element [9] of graph array */

Or we could input values into all coordinates using a loop int i; for(i=0;i<10;i++){ printf("Enter x value for coordinate%d\n",i+1); scanf("%f",&graph[i].x); printf("Enter y value for coordinate%d\n",i+1); scanf("%f",&graph[i].y); }

Or using structure pointer notation int i; for(i=0;i<10;i++){ printf("Enter x value for coordinate%d\n",i+1); scanf("%f",&(graph+i)->x); /* -> has higher precedence than & */ printf("Enter y value for coordinate%d\n",i+1); scanf("%f",&(graph+i)->y); }

A structure containing an array typedef struct student { char name[30]; float mark; } STUDENT; STUDENT you; printf ("enter name and mark\n"); scanf ("%s%f",you.name,&you.mark); /* you.name is the address of the name array */ /* so no &ampersand needed to get address*/ /* BUT you.mark is not an array so needs & */ printf ("hello %s your mark is %f\n",you.name,you.mark); printf ("The first letter of your name is %c\n",you.name[0]);

Assigning structures Unlike arrays, structures can be copied as a complete entity using a single assignment. This saves time. So instead of writing: strcpy(temp.name,you.name); /* use function strcpy to copy a string */ temp.mark = you.mark; /* can assign mark in one go */ we can write: temp=you; /* copy structure in one go */