C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct

Similar documents
Pointers, Dynamic Data, and Reference Types

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Tokens, Expressions and Control Structures

Dynamic Data Structures. CSCI 112: Programming in C

PROGRAMMAZIONE I A.A. 2017/2018

12 CREATING NEW TYPES

C Programming Basics II

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

a data type is Types

Heap Arrays. Steven R. Bagley

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Linked List using a Sentinel

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

MCS 360 Exam 2 11 November In order to get full credit, you need to show your work.

Kurt Schmidt. October 30, 2018

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

ECE 2400 Computer Systems Programming Fall 2018 Topic 7: Concrete Data Types

Exam 3 Chapters 7 & 9

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Dynamic Memory Allocation and Linked Lists

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Writing Functions in C

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Understanding Pointers

Dynamic memory. EECS 211 Winter 2019

Recitation #11 Malloc Lab. November 7th, 2017

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

Dynamic Memory Allocation

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007


Pointers, Pointers, Pointers!

Complex data structures. Cedric Saule

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Lexical and Syntax Analysis. Abstract Syntax

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Basic Types & User Defined Types

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

CSE 3302 Programming Languages Lecture 5: Control

Programming in C S c o t t S c h r e m m e r

Assist. Prof. Dr. Caner ÖZCAN

Memory Allocation. General Questions

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

CS61C : Machine Structures

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

COMP 2355 Introduction to Systems Programming

Low-Level C Programming. Memory map Pointers Arrays Structures

Data Representation and Storage

Arrays, Pointers and Memory Management

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Structures and Pointers

Character Strings. String-copy Example

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

C Programming Review CSC 4320/6320

Think Like a Programmer

Type Checking. Prof. James L. Frankel Harvard University

Data Representation and Storage. Some definitions (in C)

Data and File Structures Laboratory

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Data Structures and Algorithms (DSA) Course 4. Iulian Năstac

BEA TUXEDO System Messages. TxRPC Catalog

Chapter 4: Arrays, Pointers and Strings

Heap Arrays and Linked Lists. Steven R. Bagley

CS201 Some Important Definitions

Dynamic Data Structures (II)

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Data Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values

Lecture 2: C Programm

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Motivation was to facilitate development of systems software, especially OS development.

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Data Structures and Algorithms (DSA) Course 5 Lists. Iulian Năstac

ALL ABOUT POINTERS C/C++ POINTERS

BBM 201 DATA STRUCTURES

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Section - Computer Science. int main() {! int a=10,b=20;! printf("a:%d B:%d\n",a,b);! a=(a+b)-(b=a);! printf("a:%d B:%d\n",a,b);!

Common Misunderstandings from Exam 1 Material

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

EL6483: Brief Overview of C Programming Language

Quiz 0 Review Session. October 13th, 2014

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Procedural programming with C

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Discussion 3 Richard Guo Advanced C 01/28/09

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Lexical and Syntax Analysis

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Memory Organization. The machine code and data associated with it are in the code segment

Basic Types, Variables, Literals, Constants

Input And Output of C++

Transcription:

C-types: basic & constructed C basic types: int, char, float, C constructed types: pointer, array, struct

Memory Management Code Global variables in file (module) Local static variables in functions Dynamic memory: Function parameters Local variables in functions Static memory Stack memory Dynamic memory: User allocated data objects using malloc - address returned to a pointer variable in static/stack Heap memory 13/11/2016 DFR - C Data Types 2

References Reference to a variable/function: name Reference to an array element: index Reference to a memory object: address The value of a pointer is an address Pointers may refer to Variables, functions: ptr = &A (& = address operator) Heap allocated data objects ptr = malloc(sizeof(type)) 13/11/2016 DFR - C Data Types 3

Code & Data Objects Code Object: a program / function Data Object: not always a variable!!! A literal integer e.g. 2 A literal char e.g. A A literal string e.g. ABC A constant e.g. const int k A variable e.g. int j values A heap object e.g. malloc(sizeof(int)) NB: constants & variables have names (reference) 13/11/2016 DFR - C Data Types 4

C Data Types Basic (atomic) data types char, int, real: float, double integers: short, long, long long, signed, unsigned enum (enumeration) constants Constructed data type instances (variables) pointer typename * ptr; array collection of same type elements typename X[size]; struct collection of different type elements struct tag { members } X; members are: type1 name1; type2 name2; 13/11/2016 DFR - C Data Types 5

C Data Types: Instances Variables type instances basic types int j; char c; float x; Variables type instances constructed types int * pint; /* pointer to an int */ int array[size]; /* index: 0..(size-1) */ struct listelem record; /* struct variable */ The general form is <type_name> <variable_name>; or <type_definition> <variable_name>; 13/11/2016 DFR - C Data Types 6

C Data Types: struct - use Variables struct definition example struct listelem { /* 2 fields */ int value; /* integer field */ struct listelem * next; /* struct pointer field */ }; struct listelem listnode; /* struct variable */ listnode.value = 3; /* field assignment */ listnode.next = NULL; if ( listnode.value == 3 ) { } /* using a field */ 13/11/2016 DFR - C Data Types 7

C Data Types: typedef A typedef creates an alias for another type name E.g. typedef int listref; /* listref is alias for int */ For constructed types: typedef int intarray[size]; /* intarray is the type */ typedef struct listelem * listref; /* pointer to a struct */ typedef struct listelem { /* listelem is a tag */ int value; listref next; /* not: struct listelem * */ } listelem; /* listelem is the type */ 13/11/2016 DFR - C Data Types 8

C Data Types: typedef struct typedef struct listelem * listref; /* pointer to a struct */ typedef struct listelem { /* listelem is a tag */ int value; listref next; } listelem; /* listelem is the type */ listelem listnode; /* struct variable */ listnode.value = 3; /* field assignment */ listnode.next = NULL; if ( listnode.value == 3 ) { } /* using a field */ 13/11/2016 DFR - C Data Types 9

typedef / struct: difference NB! typedef struct listelem * listref; /* pointer to a struct */ typedef struct listelem { /* listelem is a tag */ int value; listref next; } listelem; /* listelem is the type */ struct listelem { /* listelem is a tag */ int value; listref next; } listnode; /* listnode variable */ 13/11/2016 DFR - C Data Types 10

C Data Types: struct pointer typedef struct listelem * listref; /* pointer to a struct */ typedef struct listelem { /* listelem is a tag */ int value; listref next; } listelem; /* listelem is the type */ listref plistelem; /* struct pointer */ plistelem = malloc(sizeof(listelem)); /* struct instance heap */ plistelem value = 3; /* field assignment */ plistelem next = NULL; if (plistelem value == 3 ) { } /* using a field */ 13/11/2016 DFR - C Data Types 11

C Data Types: linked list typedef struct listelem * listref; /* pointer to a struct */ typedef struct listelem { /* listelem is a tag */ int value; listref next; } listelem; /* listelem is the type */ listref pnew; /* struct pointer */ pnew = malloc(sizeof(listelem)); /* struct instance heap */ pnew value = 3; /* field assignment */ pnew next = NULL; linkin(pnew); /* add to linked list */ 13/11/2016 DFR - C Data Types 12

C Data Types: linked list typedef struct listelem * listref; /* pointer to a struct */ typedef struct listelem { /* listelem is a tag */ int value; listref next; } listelem; /* listelem is the type */ listref L; /* struct pointer */ L value next value next value next value next Use cons(h,t) to construct new lists (recursive version) 13/11/2016 DFR - C Data Types 13

C Data Types: linked list Comments recursive sequence code listref L; /* reference to the list (ptr) */ create_e(value) returns a pointer of type listref to a new list element created with malloc in the heap the next field in the list element is of type listref (pointer) The value of a pointer is an address (remember this!) List elements are always added and removed at the HEAD (local head!) of the list in the recursive version. cons(h,t) 13/11/2016 DFR - C Data Types 14

C Data Types: struct summary typedef struct listelem * listref; /* pointer to a struct */ typedef struct listelem { /* listelem is a tag */ int value; listref next; } listelem; /* listelem is the type */ /* struct variable */ /* struct pointer */ listelem listnode; listref plistelem; listref = malloc(sizeof(listelem)) listnode.value = 3; plistelem value = 3; listnode.next = NULL; plistelem next = NULL; 13/11/2016 DFR - C Data Types 15

OO: struct object or class In OO languages, the struct has become an object struct object: attributes + methods Creation is done via a constructor Allocation depends on the language (some use the heap) The list would become a list object + a collection of list element objects Elements are created with new A list is a predefined class list list element* 13/11/2016 DFR - C Data Types 16