What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
C Programming Lecture 23 Enumeration Types Structures.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Programming in C Chapter 10 Structures and Unions
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
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.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers in C Rohit Khokher
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Tutorial #5 Summer while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
21 August (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Data Types data type:a collection of.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Structures and Enumerations Introduction Structure definitions Nested structure Referring and initializing structure elements Passing structures to a function.
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Pointers and Dynamic Arrays
Structure, Unions, typedef and enumeration
typedef typedef int Index; typedef char Letter; Index i; i = 17;


C Structures, Unions, and Enumerations
2017 Jan Sun Mon Tue Wed Thu Fri Sat

Chapter: 7-12 Final exam review.
Jan Sun Mon Tue Wed Thu Fri Sat
Sun Mon Tue Wed Thu Fri Sat
C Structures, Unions, Bit Manipulations and Enumerations
C++ Pointers and Strings
QUIZ.
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Sun Mon Tue Wed Thu Fri Sat

2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
C++ Pointers and Strings
TIMELINE NAME OF PROJECT Today 2016 Jan Feb Mar Apr May Jun
Presentation transcript:

What do I need to Know For My Assignment?

C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using * not in declaration. int* ptr; This declares a variable that points to an integer

C Pointer Review The & or “address of” operator gives us a pointer to the variable. Int i; The type of &i is *int

C Pointer Review The * or “dereferencing” operator, will return the variable that a pointer is pointing to Int* i *i is of type int Remember for structures that (*struct).value == struct->value

C Pointer Review A char* can point to a string of any length, because really we are storing the value of the address of the first character. In C a string is a set of characters until the termination character ‘/0’ Arrays are also pointers, only their length is pre-defined

C Pointer Review Given double b[5];double *bPtr; bPtr = b; bPtr = &b[0] bPtr + 1 == *(bPtr + 3) == bPtr[2] ==

C Pointer Review Void* ptr; This is a void pointer, meaning it can point to anything. We can use this to fake polymorphism, i.e. making a linked list using void pointers means we can actually store anything we want in the list

C Pointer Review When a pointer points to nothing it is said to have NULL value NEVER DEREFERENCE NULL VALUES We can check to see if a pointer is null easily Int * i; if (i==NULL) printf( “Obviously True”);

C Pointer Review With the exception of arrays and functions, C is always pass by value. This means that the value of your parameter is passed into the function. Therefore changing the value in your function will have no effect This is why we must use pass by reference for many functions.

C Pointer Review Void foo1(int i) { i = 0; } Void foo2(int *i){*i=0;} Int main(void) { int k = 10; foo?(k); printf(“k = %d”,k); return 0; }

Data Structures Data Structures are, as the name implies, a structure to hold data. They all have 3 basic components; constructors, selectors and mutators

Constructors Constructors are the part of the structure used to “construct” a variable.

Selectors Selectors are procedures that selects and returns the stored data

Mutators Mutators are procedures that will mutate the data into an easier more useful form

Our Tools in C Typedef lets you rename a type to something else Typedef Typedef int age;

Our Tools in C Enum is a tag that lets us define enumerations Enum {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; !!!!!This does not let you use the type!!!!! To use the type, we must typedef Typedef Enum {Mon, Tue, Wed, Thu, Fri, Sat, Sun} Day;

Our Tools in C Enum looks much like a typedef, because it essentially gives new names for numbers It really looks like Enum {Mon = 0, Tue = 1, Wed = 2, Thu = 3, Fri = 4, Sat = 5, Sun = 6}; We can, should we choose, define a type Typedef enum {apple = 5, pear = 7} Fruit;

Our Tools in C Struct lets us build more complicated types, much like records Typedef struct { int studentNo char* name } Student; Once again the above is shorthand

Our Tools in C Structs can have other structures, arrays and pointers Putting a pointer to the same structure allows recursive data types like lists, trees, etc.

Our Tools in C To make memory space for our types it is often necessary to use the function malloc, to allocate memory. Int *i; automatically sets enough space for an integer, but should we want a pointer to complicated data structures we need malloc Malloc takes the number of bytes and returns a pointer to enough space

Our Tools in C Sizeof is an operator that returns the size of anything, like the number of bytes necessary for a malloc Char c; (sizeof c) == 8 For arrays remember to find the size of the whole array Int i [length]]; (sizeof i) * length

Date Example Lets now create a data type in C for the Date We will store, month, day of the month and day of the week

Date Example We will do this by creating two enumerations for Month and Day typedef enum { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } Month; typedef enum { MON, TUE, WED, THU, FRI, SAT, SUN } Day;

Date Example Now we combine them into the data structure Typedef struct { Month month, Day day, int dayNum} Date;

Date Example Now we add a constructor Date* newDate(Month mo, Day da, int i){ Date *d = malloc(sizeof( Date)); if (d = null) fprintf(stderr, “Failure Making New Date”); else {d->month = mo; d->day = da; d->dayNum = I;} return d; }

Date Example Now we add selectors Month getMonth(Date *d) { return d->month;} Day getDay(Date *d) {return d->day;}

Date Example Now we add a mutator Date* tommorow(Date *d){ return newDate( (d->month + ((d->dayNum+1) % 30))% 11, (d->day + 1)%6, (d->dayNum+1)%30 ); }