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.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Programming in C Chapter 10 Structures and Unions
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
C Language.
Arrays and Strings.
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.
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.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
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.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Introduction to Programming Lecture 31. Operator Overloading.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
1 C++ Syntax and Semantics The Development Process.
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
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.
Compiler Construction
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Built into qsort is a function that can swap two given array elements.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
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.
Section 3.1 Chapter 3 Objectives: Identify Ordered Pair Identify a Relation Identify the Domain and Range of a Relation.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
 Review structures  Program to demonstrate a structure containing a pointer.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
C Tokens Identifiers Keywords Constants Operators Special symbols.
Review C Language Features –control flow –C operators –program structure –data types –I/O and files Problem Solving Abilities.
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.
Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
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:
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Structures Combining data types into a logical groupings.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
 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.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
Precalculus Section 1.5 Perform basic operations with complex numbers The basic imaginary unit is i. i = i 2 = -1 A complex number is any number that can.
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.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Lesson #8 Structures Linked Lists Command Line Arguments.
Chapter 10-1: Structure.
Pointers, Enum, and Structures
Visit for more Learning Resources
Computer science C programming language Lesson 5
Buy book Online -
Derived types.
Type Systems CSE 340 – Principles of Programming Languages Fall 2016
5th Chapter Pointers in C++.
Compiler Construction
C Programming Lecture-8 Pointers and Memory Management
Structures In C Programming By Rajanikanth B.
A simple function.
Percentage Composition
CSCE 206 Lab Structured Programming in C
Compiler Construction
Intermediate Pointers & Basic Structures
Presentation transcript:

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 are formed with the C built-in data types. To do this, we use the C structure. A structure is a composite data type defined by the user.

Structure Complex number is not part of the C data structure. But we can define a structure to represent complex numbers. Define a structure called complex which consists of a real field and imag field, both of them are doubles. struct complex { double real; double imag; } Declare three complex numbers: struct complex a, b, c; real and imag are called the member (or field) of the structure complex.

Structure Declaration Suppose that we want to represent an atomic element, including such features as its conventional name, scientific symbol, atomic weight, and mass. struct element { char name[10]; char symbol[5]; float aWgt; float mass; } Note that each field can be arbitrary data types, including structure.

Declare and Initialize a Structure Example: int n, ns[5]; float r, ts[4]; struct element e1, es[3]; strcpy( e1.name, "hydrogen"); strcpy( e1.symbol, "H"); e1.aWgt = 1.0; e1.mass = 3.0; To access each field of a structure, use the structure's name followed by a period '.' and the name of the field.

Structure Assignment struct element e1 = {"hydrogen", "H", 1.0, 3.0}; struct element es[3]; es[0] = e1; struct element f(...) {... return e1; } Structure as a whole can be used in assignment (as long as data type match), and returned from a function. However, any other operation must be on a field by field basis.

Structure, example #include main() { struct element { char name[10]; char symbol[5]; float aWgt; float mass; }; struct element es[3]; struct element e1 = {"Hydrogen", "H", 1.0, 3.0}; es[0] = e1; printf("%s, %s, %f, %f\n", es[0].name, e1.symbol, es[0].aWgt, e1.mass); }

Pointer to Structure typedef struct computer { char brand[7]; char *cpu; int ram_size; } E_Brain; E_Brain computer1, *ptr; ptr = &computer1; (*ptr).ram_size = 640; strcpy((*ptr).brand, "IBM"); (*ptr).cpu = "pentium";

Pointer Operator The following member access syntax forms are equivalent: (*ptr).cpu ptr -> cpu with: E_Brain computer1, *ptr; ptr = &computer1; we can also say ptr -> ram_size = 640; strcpy(ptr->brand, "IBM"); ptr -> cpu = "pentium";

Typedef Construct C typedef construct associates a built-in or user-defined type with a new (type) name. typedef oldname newname; struct pair { double real; double imaginary; }; typedef struct pair complex; typedef int* int_ptr; complex a b c; int_ptr p, q;

Reading/Home Working Read Chapter 10, page 490 to 525. Work on Problems –Section 10.1, page 496, exercise 1, 3, 5. –Section 10.2, page 503, exercise 1. –Section 10.5, page 517, exercise 1, 5, 11. Check your answers in the back of the textbook. Do not hand in.