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

Similar documents
CS3157: Advanced Programming. Announcement

High Performance Computing in C and C++

Kurt Schmidt. October 30, 2018

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Intermediate Programming, Spring 2017*

CSCI 171 Chapter Outlines

First of all, it is a variable, just like other variables you studied

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

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Pointers, Dynamic Data, and Reference Types

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Lecture 8 Dynamic Memory Allocation

Compiling and Running a C Program in Unix

today cs3157-fall2002-sklar-lect05 1

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Short Notes of CS201

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

CS201 - Introduction to Programming Glossary By

Programs in memory. The layout of memory is roughly:

Lectures 5-6: Introduction to C

CS 222: Pointers and Manual Memory Management

PRINCIPLES OF OPERATING SYSTEMS

C Programming Basics II

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS349/SE382 A1 C Programming Tutorial

Dynamic Data Structures. CSCI 112: Programming in C

Dynamic Memory: Alignment and Fragmentation

Dynamic memory allocation

C Structures & Dynamic Memory Management

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CS 61c: Great Ideas in Computer Architecture

Final CSE 131B Spring 2004

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

C-LANGUAGE CURRICULAM

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Data Representation and Storage. Some definitions (in C)

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

Dynamic memory. EECS 211 Winter 2019

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Lectures 5-6: Introduction to C

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Data Representation and Storage

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

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

Memory Management. CSC215 Lecture

OBJECTIVE QUESTIONS: Choose the correct alternative:

CS201 Some Important Definitions

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

In Java we have the keyword null, which is the value of an uninitialized reference type

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Programming. Structures, enums and unions

Object-Oriented Programming

Arrays and Pointers. CSE 2031 Fall November 11, 2013

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Quick review pointer basics (KR ch )

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CS 11 C track: lecture 5

Dynamic memory allocation (malloc)

Dynamic Memory Allocation

M1-R4: Programing and Problem Solving using C (JULY 2018)

Character Strings. String-copy Example

Pointers. Introduction

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Memory, Arrays & Pointers

o Code, executable, and process o Main memory vs. virtual memory

This exam is to be taken by yourself with closed books, closed notes, no calculators.

Memory Allocation. General Questions

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

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Advanced Pointer Topics

POINTER AND ARRAY SUNU WIBIRAMA

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

CS61, Fall 2012 Section 2 Notes

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Memory (Stack and Heap)

( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING UNIT V 2 MARKS

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

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

Procedures, Parameters, Values and Variables. Steven R. Bagley

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Class Information ANNOUCEMENTS

DAY 3. CS3600, Northeastern University. Alan Mislove

CSC231 C Tutorial Fall 2018 Introduction to C

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

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

edunepal_info

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Transcription:

today advanced data types (1) typedef. mon 23 sep 2002 homework #1 due today homework #2 out today quiz #1 next class 30-45 minutes long one page of notes topics: C advanced data types dynamic memory allocation structured data types (array, struct) defining your own types using typedef typedef short int smallnumber; typedef unsigned char byte; typedef char String[100]; smallnumber x; byte b; String name; cs3157-fall2002-sklar-lect06 1 cs3157-fall2002-sklar-lect06 2 advanced data types (2) typedef. advanced data types (3) enum. defining your own boolean: define new integer-like types as enumerated types: typedef char boolean; #define FALSE 0 #define TRUE 1 generally works, but beware: check = x > 0; if ( check == TRUE ) {...} if x is positive, check will be non-zero, but may not be enum weather { rain, snow=2, sun=4 }; typedef enum { Red, Orange, Yellow, Green, Blue, Violet } Color; look like C identifiers (names) are listed (enumerated) in definition treated like integers start with 0 (unless you set value) can add, subtract e.g., color + weather cannot print as symbol automatically (you have to write code to do the translation) cs3157-fall2002-sklar-lect06 3 cs3157-fall2002-sklar-lect06 4

advanced data types (4) enum. advanced data types (5) data objects. just fancy syntax for an ordered collection of integer constants: typedef enum { Red, Orange, Yellow } Color; is like #define Red 0 #define Orange 1 #define Yellow 2 here s another way to define your own boolean: typedef enum {False, True} boolean; C does not have Objects in the OOP sense (like Java and C++ do) but C has data objects i.e., variables short int x; char ch; float pi = 3.1415; float f, g; scope variables defined in { } block are active only in block e.g., local variables defined outside a block are global (persist during program execution) static variables may be declared outside a block, but are not globally visible cs3157-fall2002-sklar-lect06 5 cs3157-fall2002-sklar-lect06 6 advanced data types (6) data objects. advanced data types (7) usage. variables must be declared before they are used we have used variables within main() and within functions global variables are declared outside main() and outside any function, usually at the top of the program file, after any # s (preprocessor directives) can be seen anywhere local variables are declared within a program block or function they can only be seen inside the block in which they are defined function arguments are local to the function they are passed to a variable is conceptually a container that can hold a value default value is (mostly) undefined you should treat it as a random number the compiler may warn you about uninitialized variables, but not as reliably as Java variables are always passed by value, but you can pass the address of a variable to a function: scanf( "%d%f", &x, &f ); cs3157-fall2002-sklar-lect06 7 cs3157-fall2002-sklar-lect06 8

advanced data types (8) sizes. dynamic memory allocation (1). every data object in C has: a name and data type (specified in definition) an address (its relative location in memory) a size (number of bytes of memory it occupies) visibility (which parts of program can refer to it) lifetime (period during which it exists) Unlike scripting languages and Java, all C data objects have a fixed size over their lifetime except dynamically created objects size of object is determined when object is created: global data objects at compile time (data) local data objects at run-time (stack) dynamic data objects by programmer (heap) malloc() allocates a block of memory: void *malloc( size_t size ); lifetime of the block is until memory is freed, with free(): void free( void *ptr ); example: int *dynvec, num_elements; printf( "how many elements do you want to enter? " ); scanf( "%d", &num_elements ); dynvec = (int *)malloc( sizeof(int) * num_elements ); cs3157-fall2002-sklar-lect06 9 cs3157-fall2002-sklar-lect06 10 dynamic memory allocation (2). dynamic memory allocation (3). memory leaks memory allocated that is never freed: char *combine( char *s, char *t ) { u = (char *)malloc( strlen(s) + strlen(t) + 1 ); if ( s!= t ) { strcpy( u, s ); strcat( u, t ); return u; } else { return 0; } } /* end of combine() */ u should be freed if return 0; is executed but you don t need to free it if you are still using it! note: malloc() does not initialize data you can allocate and initialize with calloc : void *calloc( size_t nmemb, size_t size ); calloc allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. you can also change size of allocated memory blocks with realloc : void *realloc( void *ptr, size_t size ); realloc changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. these are all functions in stdlib.h for more information: unix$ man malloc cs3157-fall2002-sklar-lect06 11 cs3157-fall2002-sklar-lect06 12

structured data types (1). structured data types (2) arrays. structured data types are available as: object property array [ ] enumerated; indexed from 0 struct names and types of fields union made up of multiple elements, but only one exists at a time; each element could be a native data type, a pointer or a struct arrays are defined by specifying an element type and number of elements statically: int vec[100]; char str[30]; float m[10][10]; dynamically: int *dynvec, num_elements; printf( "how many elements do you want to enter? " ); scanf( "%d", &num_elements ); dynvec = (int *)malloc( sizeof(int) * num_elements ); for an array containing N elements, indeces are 0..N-1 stored as a linear arrangement of elements often similar to pointers cs3157-fall2002-sklar-lect06 13 cs3157-fall2002-sklar-lect06 14 structured data types (3) arrays. structured data types (4) arrays. C does not remember how large arrays are (i.e., no length attribute, unlike Java) given: int x[10]; x[10] = 5; /* error! */ ERROR! because you have only defined x[0]..x[9] and the memory location where x[10] is can become something else... sizeof x gives the number of bytes in the array sizeof x[0] gives the number of bytes in one array element thus you can compute the length of x via: int length_x = sizeof x / sizeof x[0]; when an array is passed as a parameter to a function: the size information is not available inside the function array size is typically passed as an additional parameter printarray( x, length_x ); or as part of a struct (best practice; object-like) typedef struct { int x[10]; int length_x; } Array; Array ax; ax.length_x = 10; printarray( ax ); or globally #define VECSIZE 10 int x[vecsize]; cs3157-fall2002-sklar-lect06 15 cs3157-fall2002-sklar-lect06 16

structured data types (5) arrays. structured data types (6) arrays. array elements are accessed using the same syntax as in Java: array[index] C does not check whether array index values are sensible (i.e., no bounds checking) e.g., x[-1] or vec[10000] will not generate a compiler warning! if you re lucky, the program crashes with Segmentation fault (core dumped) C references arrays by the address of their first element array is equivalent to &array[0] you can iterate through arrays using pointers as well as indexes: int *v, *last; int sum = 0; last = &x[length_x-1]; for ( v = x; v <= last; v++ ) sum += *v; cs3157-fall2002-sklar-lect06 17 cs3157-fall2002-sklar-lect06 18 structured data types (7) arrays. structured data types (8) arrays. example: #include <stdio.h> #define MAX 12 int main( void ) { int x[max]; /* declare 12-element array */ int i, sum; for ( i=0; i<max; i++ ) { x[i] = i; } /* here, what is value of i? of x[i]? */ sum = 0; for ( i=0; i<max; i++ ) { sum += x[i]; } printf( "sum = %d\n",sum ); } /* end of main() */ another example: #include <stdio.h> #define MAX 10 int main( void ) { int x[max]; /* declare 10-element array */ int i, sum, *p; p = &x[0]; for ( i=0; i<max; i++ ) { *p = i + 1; p++; } p = &x[0]; sum = 0; for ( i=0; i<max; i++ ) { sum += *p; p++; } printf( "sum = %d\n",sum ); } /* end of main() */ cs3157-fall2002-sklar-lect06 19 cs3157-fall2002-sklar-lect06 20

structured data types (9) 2D arrays. structured data types (10) struct. 2-dimensional arrays int weekends[52][2]; [0][0] [0][1] [1][0] [1][1] [2][0] [2][1] [3][0]... weekends you can use indices or pointer math to locate elements in the array weekends[0][1] weekends+1 weekends[2][1] is same as *(weekends+2*2+1), but NOT the same as *weekends+2*2+1 (which is an integer)! struct is similar to a field in a Java object definition it s a way of grouping multiple data types together components can be any type (but not recursive) accessed using the same syntax struct.field int main() { struct { int x; char y; float z; } rec; rec.x = 3; rec.y = a ; rec.z = 3.1415; printf( "rec = %d %c %f\n",rec.x,rec.y,rec.z ); } /* end of main() */ cs3157-fall2002-sklar-lect06 21 cs3157-fall2002-sklar-lect06 22 structured data types (11) struct. structured data types (12) struct. variables of struct types can be declared in two ways: using a tag associated with the struct definition wrapping the struct definition inside a typedef example: int main() { struct record { int x; char y; float z; }; struct record rec; rec.x = 3; rec.y = a ; rec.z = 3.1415; printf( "rec = %d %c %f\n",rec.x,rec.y,rec.z ); } /* end of main() cs3157-fall2002-sklar-lect06 23 another example: int main() { typedef struct { int x; char y; float z; } Record; Record rec; rec.x = 3; rec.y = a ; rec.z = 3.1415; printf( "rec = %d %c %f\n",rec.x,rec.y,rec.z ); } /* end of main() cs3157-fall2002-sklar-lect06 24

structured data types (13) struct. structured data types (14) struct. overall size of struct is the sum of the elements, plus padding for alignment given previous 3 examples: sizeof( rec ) 12 but, it depends on the size and order of content (e.g., ints need to be aligned on word boundaries, since size of char is 1 and size of int is 4): struct { char x; int y; char z; } s1; /* x y z */ /* ---- ---- ---- */ /* sizeof s1 -> 12 */ struct { char x, y; int z; } s2; /* xy z */ /* ---- ---- */ /* sizeof s2 -> 8 */ pointers to structs are common especially useful with functions (as arguments to functions or as function type) two notations for accessing elements: (*sp).field or sp->field (note: *sp.field doesn t work) struct xyz { int x, y, z; }; struct xyz s; struct xyz *sp;... s.x = 1; s.y = 2; s.z = 3; sp = &s; (*sp).z = sp->x + sp->y; cs3157-fall2002-sklar-lect06 25 cs3157-fall2002-sklar-lect06 26 structured data types (15) extended example p1. structured data types (16) extended example p2. #include <stdio.h> #include <string.h> #define NAME_LEN 40 struct person { char name[name_len+1]; float height; struct { /* nested structure */ int day; int month; int year; } birthday; }; void printperson( struct person * ); /* prototype */ int main( void ) { struct person suzanne; /* declare one */ struct person class[120]; /* declare an array */ /* store info in one */ strcpy( suzanne.name,"suzanne" ); suzanne.height = 60; suzanne.birthday.day = 16; suzanne.birthday.month = 5; suzanne.birthday.year = 1988; /* store info in the array */ strcpy( class[0].name,"alex" ); class[0].height = 48; class[0].birthday.day = 9; class[0].birthday.month = 5; class[0].birthday.year = 1995; strcpy( class[1].name,"jen" ); class[1].height = 55; cs3157-fall2002-sklar-lect06 27 cs3157-fall2002-sklar-lect06 28

structured data types (17) extended example p3. class[1].birthday.day = 14; class[1].birthday.month = 4; class[1].birthday.year = 1992; /* print them... */ printperson( &suzanne ); printperson( &class[0] ); printperson( &class[1] ); } /* end of main() */ void printperson( struct person *p ) { printf( "name = [%s]\n",p->name ); printf( "height = %5.2f inches\n",p->height ); printf( "birthday = %02d/%02d/%4d\n",p->birthday.day, p->birthday.month,p->birthday.year ); } cs3157-fall2002-sklar-lect06 29