 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Programming in C Chapter 10 Structures and Unions
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
Programming In C++ Spring Semester 2013 Practice 2 Programming In C++, Practice By Umer Rana.
C Language.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Sort the given string, without using string handling functions.
C Intro.
Data Types C built-in data types –char, int, float, double, int*, etc. User-defined data types: the programmer can define his/her own data types which.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
CGS 3460 More On File IO. CGS 3460 What is a File n A file is a package of information with a name attached to it. n Files are used for various purposes:
Lecture 26: Structures / struct s/. 2 Lecture Contents: t Basics of structs t Struct type definition ( struct reserved word) t Struct type definition.
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.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time.
EXERCISE Arrays, structs and file processing. Question You own a pet store. You want to keep an inventory of all the pets that you have. Pets available.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
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.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Pointers (Revision). 2 Overview Revision of Pointers Pointers and structs Basic Pointer Arithmetic Pointers and Arrays.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
EECE.2160 ECE Application Programming
LINKED LISTS.
Default Arguments.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Programming Language C Language.
CS150 Introduction to Computer Science 1
Structure (i.e. struct) An structure creates a user defined data type
Variables in C Topics Naming Variables Declaring Variables
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the basic types in C - char, int, float - passed as arguments - returned as results 1

 2007 Pearson Education, Inc. All rights reserved. typedef struct Person{ int age; char gnd; float weight; } PERSON; 2

 2007 Pearson Education, Inc. All rights reserved. the use of the members of a struct. int adjustAge( int oldAge ){ if ( oldAge < 39 ) return( ++oldAge ); else return( oldAge ); /*return (oldAge<39) ? ++oldage : oldage;*/ } void main(){ PERSON Jim; : Jim.age = adjustAge( Jim.age ); : } 3

 2007 Pearson Education, Inc. All rights reserved. typedef struct Book{ int edition; int pages; double weight; char type; int WC; /*weight classification 1 heavy, 0 is not */ } BOOK; Write a function tack an argument double to test the weight of a book and return 1 if the weight greater than 2 and 0 otherwise. The value to be assign to WC member. 4

 2007 Pearson Education, Inc. All rights reserved. int weightC(double w){ if(w > 2) return 1; else return 0; } void main(){ BOOK myBok; : myBoK.wc = weightC(myBok.weight); : } 5 int weightC(double w){ return (w > 2)? 1 : 0; }

 2007 Pearson Education, Inc. All rights reserved. ASS  Consider the student struct: add a new member name it GL /*Grades Later*/  Write a function tack a double to test the average of the student and return a later match the average to be assign to the new member  Test the function 6

 2007 Pearson Education, Inc. All rights reserved. The whole of a struct is returned by a function PERSON get_details() { PERSON temp; printf("Please enter Age, Gender and Weight (Kg): "); scanf("%d %c%f", &temp.age, &temp.gnd, &temp.weight ); return temp; } void main(){ PERSON Jim, Mary, Sid; : Jim = get_details(); : } 7

 2007 Pearson Education, Inc. All rights reserved.  Write a function allow the user to input the details of a BOOK and return it back typedef struct Book{ int edition; int pages; double weight; char type; } BOOK; 8

 2007 Pearson Education, Inc. All rights reserved. BOOK GetBook(){ BOOK tmp; printf(“\nEnter the BOOK information:”); printf(“ [edition, pages, weight, and type]”); scanf(“%d%d%f%c”, &tmp.edition, &tmp.pages. &tmp.weight. &tmp.type); retrun tmp; } void main(){ BOOK myBook; : myBook = GetBook(); : } 9

 2007 Pearson Education, Inc. All rights reserved. ASS  Write a function allow the user to input the details of a STUDENTand return it back  Test the function 10

 2007 Pearson Education, Inc. All rights reserved. Passing a struct as a function argument void show_details( PERSON who ){ printf( "Person's age was: %d\n", who.age ); printf( " Gender was: %c\n", who.gnd ); /* Kg -> lb */ who.weight = who.weight * 2.2; printf( " weight (lb) was: %5.1f\n", who.weight ); } void main(){ PERSON Jim, Mary, Sid; : Jim = get_details(); : show_details(Jim); } 11

 2007 Pearson Education, Inc. All rights reserved.  Write a function tack a BOOK and print the content out 12

 2007 Pearson Education, Inc. All rights reserved. void PrintBook( const BOOK b){ printf(“The BOOK information:\n”); printf(“\n\tedition: %d\n\tpages: %d”, b.edition, b.pages); printf(“\n\tweight: %f \n\ttype: %c”, b.weight, b.type); } void main(){ BOOK myBook = {2, 120, 2.5, ‘A’}; : PrintBook(myBook); : } 13

 2007 Pearson Education, Inc. All rights reserved. ASS  Write a function tack a STUDENT and print the content out  Test the function 14

 2007 Pearson Education, Inc. All rights reserved. Arrays of structs  to store the same package of information for a number of different entities typedef struct Person{ int age; char gnd; float weight; } PERSON; 15

 2007 Pearson Education, Inc. All rights reserved. Arrays of structs PERSON employee[12];  We have an array of 12 elements  each of which is a structure with 3 members.  We can access any member of any element, employee[9].age = 45;  Also we can treat each element as a whole struct show_details(employee[5]); employee[3] = get_details(); 16

 2007 Pearson Education, Inc. All rights reserved.  Consider the following struct BOOK, Create an array of three BOOK s typedef struct Book{ int edition; double weight; char type; } BOOK; 17

 2007 Pearson Education, Inc. All rights reserved. BOOK academic[3]; BOOK academic[3] = { {1,2.5,’a’}, {2,3.5,’n’}, {2,1.5,’a’} }; Read in the information of the second 18

 2007 Pearson Education, Inc. All rights reserved. academic[1] = GetBook(); Printout the information of the first 19

 2007 Pearson Education, Inc. All rights reserved. PrintBook(academic[0]); Printout the content of the array 20

 2007 Pearson Education, Inc. All rights reserved. int i; for(i=0; i<3; i++) PrintBook(academic[i]); 21

 2007 Pearson Education, Inc. All rights reserved. ASS  1. Create an array of four STUDNTs with initial values  2. – Create another array of five STUDNTs – Read in the information of the whole student – Printout the content – Assign the second the information of the second of the array created at 1 – Printout the information of the second 22

 2007 Pearson Education, Inc. All rights reserved. Structs Containing Arrays typedef struct{ int age; char gnd; float weight; char name[20]; } HUMAN; HUMAN ahm = { 45, ‘M’, 95.1, “Ahmad Ali”}; HUMAN ahm = { 45, ‘M’, 95.1, {‘A’, ‘h’, ‘m’, ‘a’, ‘d’, ‘ ‘, ‘A’, ‘l’, ‘i’, ‘\0’}}; 23

 2007 Pearson Education, Inc. All rights reserved. char initial = ahm.name[0]; putchar(initial); Printout the 3 rd char of ahm’s name 24

 2007 Pearson Education, Inc. All rights reserved. char third = ahm.name[2]; printf(“%c”, third); /*putchar(third)*/ allow the user to input the 6 th char of ahm’s name 25

 2007 Pearson Education, Inc. All rights reserved. scanf(“%c”, &ahm.name[5]); print the name char by char 26

 2007 Pearson Education, Inc. All rights reserved. int i; for(i=0; i<20; i++) putchar(ahm.name[i]); i=0; while( ahm.name[i] != ‘\0’) putchar(ahm.name[i++]); Print out the name at once 27

 2007 Pearson Education, Inc. All rights reserved. printf(“%s\n”, ahm.name); puts(ahm.name); allow the user to input the ahm’s name 28

 2007 Pearson Education, Inc. All rights reserved. scanf(“%s”, ahm.name); gets(ahm.name); Which is better? 29

 2007 Pearson Education, Inc. All rights reserved. typedef struct Book{ int edition; double weight; char type; } BOOK; Rewrite the BOOK with a new member title 30

 2007 Pearson Education, Inc. All rights reserved. typedef struct Book{ int edition; double weight; char type; char title[30]; } BOOK; Create a variable of book with initial values 31

 2007 Pearson Education, Inc. All rights reserved. BOOK myBook = {1,2.5,’a’, “C programming” } change the title to “c how to program” 32

 2007 Pearson Education, Inc. All rights reserved. gets(myBook.title); scanf(“%s”, myBook.title); 33

 2007 Pearson Education, Inc. All rights reserved. ASS  Create a Time struct with classification member (“PM”, “AM”)  Write a function to print out the content of the Time  Create a STUDENT struct with a name member up to 30 char  Write two functions to read the information of a student and the second to print out the information. 34

 2007 Pearson Education, Inc. All rights reserved. HUMAN client[2] = { 35, ‘M’, 95.1, “Ahmad Ali”, 25, ‘F’, 50.9, "Salma Zakarea” }; printf( "%s\n", client[1].name ); printf( "%c\n", client[1].name[0] ); 35

 2007 Pearson Education, Inc. All rights reserved. HUMAN client[2] = { 35, ‘M’, 95.1, “Ahmad Ali”, 25, ‘F’, 50.9, "Salma Zakarea” }; HUMAN temp; Assign the values of first client to temp typedef struct{ int age; char gnd; float weight; char name[20]; } HUMAN; 36

 2007 Pearson Education, Inc. All rights reserved. temp = client[0]; temp.age = client[0].age; temp.gnd = client[0].gnd; Temp.weight = client[0].weight; strcpy(temp.name, client[0].name); 37