Structure.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Kernighan/Ritchie: Kelley/Pohl:
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
Programming in C Pointer Basics.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
C Structures and Memory Allocation
CS1010 Programming Methodology
CS1010 Programming Methodology
Lesson #8 Structures Linked Lists Command Line Arguments.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 8 Arrays, Strings and Pointers
Structures, Unions, Enumerations
UNIT 5 C Pointers.
Functions and Pointers
CS1010 Programming Methodology
Hassan Khosravi / Geoffrey Tien
Pointers.
Functions Dr. Sajib Datta
Pointers.
Module 2 Arrays and strings – example programs.
Pointers.
Functions and Pointers
INC 161 , CPE 100 Computer Programming
Pointers and References
14th September IIT Kanpur
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Programming in C Pointer Basics.
Dynamic memory allocation and Intraprogram Communication
Object Oriented Programming COP3330 / CGS5409
Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx  In this presentation, we will illustrate the difference between a pointer.
Instructor: Ioannis A. Vetsikas
Pointers  Week 10.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Pointers Problem Solving & Program Design in C Eighth Edition
Lecture 18 Arrays and Pointer Arithmetic
Programming in C Pointer Basics.
INC 161 , CPE 100 Computer Programming
Classes and Objects.
Programming in C Pointer Basics.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers.
Linked List.
Simulating Reference Parameters in C
Initializing variables
Structures CSE 2031 Fall February 2019.
POINTER CONCEPT 4/15/2019.
Submitted By : Veenu Saini Lecturer (IT)
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
Programming in C Pointer Basics.
CSCE 206 Lab Structured Programming in C
Pointers and References
C Structures and Memory Allocation
POINTER CONCEPT 8/3/2019.
Week 9 - Monday CS222.
Presentation transcript:

Structure

Structure – aggregate type The form of a structure is struct struct_name { var_type var1; var_type var2; . . . }; where struct is the keyword for defining a structure struct_name is the name for this structure (since there may be more than one) var1, var2, ... are the specific variables that make up the structure

Structure To create a structure variable of the type struct_name, we would use this: struct struct_name variable_name; We are allocating memory for a variable called variable_name. The type of variable is a structure, specifically a struct_name structure. This is analogous to creating other types of variables,for example,int sum;

We can think of a structure as being a template for an object. For example, we may wish to create a structure for storing information about a person: struct person { char name[20]; int age; double weight; }; In our program, we might create two variables using this structure: struct person John = {"John Smith", 25, 170.5}; struct person Mary = {"Mary Jones", 32, 120};

Accessing Structure Members Once a structure variable is created, we can access specific members using a period, known as the structure member operator. struct person John = {"John Smith", 25, 170.5}; printf("%s is %d years old.", John.name, John.age);

Initializing Structure Variables As we have learned, we can declare the variable and later initialize it: int x; x = 5; We can do the same with structures: struct person UTAstudent; strcpy(UTAstudent.name, “Mary Steele"); UTAstudent.age = 21; UTAstudent.weight = 115;

Initializing Structure Variables WARNING: It would have been illegal to do the following: #include <stdio.h> #include <string.h> struct person { char name[15]; int age; }; int main(void) struct person John, Mary; John = {"John Smith", 25}; /* too late to do this way */ Mary = {"Mary Jones", 32}; printf("%s is %d\n", John.name, John.age); printf("%s is %d\n", Mary.name, Mary.age); }

Array as Structure Elements We can have arrays as elements of a structure also: struct arrayStruct { int data[2][3]; double values[4]; }; int main(void) struct arrayStruct numbers = { {{1, 2, 3}, {4, 5, 6}}, {10, 20, 30 ,40} }; int i, k; for(i = 0; i < 2; i++) for(k = 0; k < 3; k++) printf("%d ", numbers.data[i][k] ); for(i = 0; i < 4; i++) printf("%f ", numbers.values[i] ); }

Arrays of Structure Instead of creating many structure variables individually, we can create an array of structures. #include <stdio.h> struct id { char name[20]; int age; }; int main(void) struct id person[2] = { {"John Smith", 25}, {"Mary Jones", 32} }; int i; for(i = 0; i < 2; i++) printf("%s, %d\n", person[i].name, person[i].age ); }

Operations on Structures Very few operations may operate on a structure as a whole. The assignment operator The address operator sizeof() //may not be what you expected! http://peeterjoot.wordpress.com/2009/11/11/cstructure- alignment-padding/ Note: you cannot use == or != to compare two structures. If you want to compare them, you need to write your own function to compare the value of each member.

Structures and Functions We can pass either a copy of a structure or a pointer to a structure to a function. Structures can also be the return type of a function. However, passing copies of structures requires more memory, which may be an issue in some situations.

struct person fx(struct person a) { struct person old = {"Tom", 20}; #include <stdio.h> #include <string.h> struct person { char name[50]; int age; }; struct person fx(struct person a); int main(void) { struct person UTAstudent = {"Mary Steele", 25}; struct person newa, temp; printf("%s is %d years old\n", UTAstudent.name, UTAstudent.age); UTAstudent.age = 99; strcpy(UTAstudent.name, "Bob"); newa = fx(UTAstudent); printf("%s is %d years old\n", newa.name, newa.age); temp = UTAstudent; UTAstudent = newa; newa = temp; printf("was UTAstudent: %s is %d years old\n", UTAstudent.name, UTAstudent.age); printf("was Tom: %s is %d years old\n", newa.name, newa.age); } struct person fx(struct person a) { struct person old = {"Tom", 20}; old.age += a.age; return old; } Output: Mary Steele is 25 years old Mary Steele is 99 years old Bob is 99 years old Tom is 119 years old was UTAstudent: Tom is 119 years old was Tom: Bob is 99 years old

#include <stdio.h> struct automobile { char make[50]; char model[50]; int year; }; void fx(struct automobile c[], int rows); void fx2(struct automobile d); int main(void) { struct automobile cars[] = {{"Ford", "Mustang", 2010}, {"Chevy", "Tahoe", 2004}, {"Chevy", "Impala", 1966} }; struct automobile mycar = {"Mazda", "Mazda3", 2009}; int rows = sizeof(cars)/sizeof(cars[0]); int i; printf("%d %s %s\n", cars[0].year, cars[0].make, cars[0].model); fx(cars, rows); fx2(mycar); for(i = 0; i < rows; i++) fx2(cars[i]); } void fx2(struct automobile d) { printf("I own a %s %s\n", d.make, d.model); } void fx(struct automobile c[], int rows) int i; for(i = 0; i < rows; i++) printf("%s %s\n", c[i].make, c[i].model);

Pointers as Structure Members We can have pointers as members of structures as well as pointers to structures. This requires that we pay careful attention to the precedence of the dereference and structure member operators. We should keep in mind the following precedence rules: ◦ . member selection via object name left-to-right ◦ -> member selection via pointer left-to-right ◦ * dereference right-to-left The dereference operator has lower precedence than the member selection operators.

Pointers as Structure Members The structure member operator has higher precedence than the dereference operator. struct node { int x; int *ptr; }; int main(void) { struct node temp; int age = 45; temp.x = 99; temp.ptr = &age; printf("%d, %d\n", temp.x, *temp.ptr); /* prints: 99, 45 */ printf("%d, %d\n", temp.x, *(temp.ptr) ); /* equivalent */ }

Pointers to Structure struct node { int x; int *ptr; }; int main(void) { struct node temp; struct node *sp = &temp; int age = 45; temp.x = 99; temp.ptr = &age; printf("using sp: %d, %d\n", (*sp).x, *(*sp).ptr); printf("using sp: %d, %d\n", sp->x, *sp->ptr); /* alternate */ }

Pointers to Structure (cont.) Here’s how the previous code works: /* sp is a pointer to a node structure. the parentheses are necessary to dereference the pointer before accessing the member at the address */ printf("using sp: %d, %d\n", (*sp).x, *(*sp).ptr); /* the -> replaces the (*sp) notation */ printf("using sp: %d, %d\n", sp->x, *sp->ptr); prints using sp: 99, 45

Structure pointer as structure member Not legal to have a structure of the same type as a member inside the declaration of the structure, however, a pointer to a structure is allowed. struct node{ int data; struct node y; } Wrong! struct node{ int data; struct node * y; } Correct. Here linked list comes!

Another Structure as Structure Member #include <stdio.h> #include <string.h> struct b { char name[50]; int age; }; struct a { int year; struct b inner; int main(void) { struct b s1; struct a s2; strcpy(s1.name, "cat"); s1.age = 99; printf("%s, %d\n", s1.name, s1.age); s2.year = 2012; strcpy(s2.inner.name, "dog"); s2.inner.age = 1234; printf("%d, %s, %d\n", s2.year, s2.inner.name, s2.inner.age); s2.inner = s1; }