Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
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.
Structures Often we want to be able to manipulate logical entities as a whole For example, complex numbers, dates, student records, etc Each of these must.
1 Records C++ Structs Chapter 14 2 What to do with records?  Declaring records  Accessing records  Accessing the field of a record  What is a union?
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
C Language.
1 Lab 4 Westfield High School APCS LAB 4 Parameters, apvectors, structs.
UNIONS IN C.  Union Data Type Union Data Type  Defining of Union Defining of Union  Memory Space Allocation Memory Space Allocation  Example of Union.
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.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structure.
Structures Spring 2013Programming and Data Structure1.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Sort the given string, without using string handling functions.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
EXERCISE IN CLASS CHAPTER 3. PART 1 IDENTIFIER SCENARIO 1 o record1 o 1record o file_3 o return o $tax o Name o name and address o name-and-address o.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions.
1 CSE1301 Computer Programming Lecture 24 Structures (Part 2)
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
Advance Use of Structures CHAPTER 5. C.10 1 Using Structures with Functions » A function can return only one value back. » Some of the processes may yield.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Functions, Pointers, Structures Keerthi Nelaturu.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
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.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Functions & Pointers in C Jordan Erenrich
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
1 CS161 Introduction to Computer Science Topic #15.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
Functions The fruitful & parameterized functions.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
“Studying C programming excluding pointers is meaningless.” d0m3z
Chapter 10-1: Structure.
CSE 303 Concepts and Tools for Software Development
Structs versus Classes
Visit for more Learning Resources
Module 2 Arrays and strings – example programs.
Anatomy of a class Part I
Tejalal Choudhary “C Programming from Scratch” Pointers
Buy book Online -
Buy book Online -
Tejalal Choudhary “C Programming from Scratch” Function & its types
S. Kiran, PGT (CS) KV, Malleswaram
Structure ការណែនាំអំពី Structure
CSI 121 Structured Programming Language Lecture 24 Structures (Part 2)
Return by Reference CSCE 121 J. Michael Moore.
Local Variables, Global Variables and Variable Scope
Topics discussed in this section:
C Programming Lecture-17 Storage Classes
CPS125.
Anatomy of a class Part I
Structures, Unions, and Enumerations
Scope Rules.
Presentation transcript:

Passing Structure to function

 structure to function structure to function  Passing structure to function in C Passing structure to function in C  Passing structure to a function by value  Example Example  Result Result  Passing structure to function by address : Passing structure to function by address :  Example Example  Result Result  No need to pass a structure – Declare structure variable No need to pass a structure – Declare structure variable  Example Example  Result Result

HELLO\0  A structure can be passed to any function from main function or from any sub function.  Structure definition will be available within the function only.  It won’t be available to other functions unless it is passed to those functions by value or by address(reference).  we can declare structure variable as global variable. That means, structure variable should be declared outside the main function. So, this structure will be visible to all the functions in a C program. BACK

 It can be done in below 3 ways. ◦ Passing structure to a function by value ◦ Passing structure to a function by address(reference) ◦ No need to pass a structure – Declare structure variable as global BACK

T he whole structure is passed to another function by value. It means the whole structure is passed to another function with all members and their values. So, this structure can be accessed from called function. BACK

#include struct student { int id; char name[20]; float percentage; }; void func(struct student record); int main() { struct student record; clrscr(); record.id=1; strcpy(record.name, "Raju"); record.percentage = 86.5;

func(record); return 0; } void func(struct student record) { printf(" Id is: %d \n", record.id); printf(" Name is: %s \n", record.name); printf(" Percentage is: %f \n", record.percentage); } BACK

The whole structure is passed to another function by address. It means only the address of the structure is passed to another function. The whole structure is not passed to another function with all members and their values. So, this structure can be accessed from called function by its address. BACK

#include struct student { int id; char name[20]; float percentage; }; void func(struct student *record); main() { struct student record; clrscr(); record.id=1; strcpy(record.name, "Raju");

record.percentage = 86.5; func(&record); getch(); } void func(struct student *record) { printf(" Id is: %d \n", record->id); printf(" Name is: %s \n", record->name); printf(" Percentage is: %f \n", record->percentage); } BACK

Structure variables also can be declared as global variables as we declare other variables in C. So, When a structure variable is declared as global, then it is visible to all the functions in a program. In this scenario, we don’t need to pass the structure to any function separately. BACK

#include struct student { int id; char name[20]; float percentage; }; struct student record; void structure_demo(); main() { record.id=1; strcpy(record.name, "Raju"); record.percentage = 86.5;

structure_demo(); getch(); } void structure_demo() { printf(" Id is: %d \n", record.id); printf(" Name is: %s \n", record.name); printf(" Percentage is: %f \n", record.percentage); } BACK

BACK TO INDEX