CS 261 - Winter 2011 Further Introduction to the C programming language.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Programming and Data Structure
Structure.
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.
Written by: Dr. JJ Shepherd
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Road Map Introduction to object oriented programming. Classes
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)
CS Winter 2011 Introduction to the C programming language.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
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:
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
 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.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CS 261 – Data Structures Introduction to C Programming.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Working With Objects Tonga Institute of Higher Education.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Review 1 List Data Structure List operations List Implementation Array Linked List.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object Oriented Software Development 4. C# data types, objects and references.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Written by: Dr. JJ Shepherd
1 CS161 Introduction to Computer Science Topic #15.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Learning Objectives Pointers as dada members
User-Written Functions
Introduction to the C programming language
C Programming Tutorial – Part I
Quiz 11/15/16 – C functions, arrays and strings
Student Book An Introduction
Basic notes on pointers in C
Object Oriented Programming COP3330 / CGS5409
Chapter 3 Introduction to Classes, Objects Methods and Strings
Built-In (a.k.a. Native) Types in C++
Java Programming Language
Class Circle { Float xc, yc, radius;
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Introduction to Pointers
Presentation transcript:

CS Winter 2011 Further Introduction to the C programming language

Why C ? C is simple language, makes it easier to focus on important concepts Java is high level language, C is low level, important you learn both Lays groundwork for many other langauges

Comparing Java and C No classes, inheritance, or polymorphism Functions are the major mechanism for encapsulation Arrays and structures are the major data storage mechanisms Pointers!! Lots of pointers.

Function definitions Functions look a lot like methods you are used to in Java, but are not part of a class return-type function-name (arguments) { declarations; /* must come first!! */ function-body; }

Variables and Declarations When you declare a variable, space is set aside somewhere in memory for the bits that hold the variable representation. int i; double d; struct arrayBag b;

Wait a moment, what is a struct? A struct is like a class with no methods, just data fields. A bundle of data struct arrayBag { int count; EleType data[100]; };

Wait a moment, what is EleType? EleType is a symbolic name we will use to represent the type of value we will store in a container. It is defined by a symbolic constant. # define EleType double

How big is a struct? As large as the combined data fields. printf(“size of arrayBag %d\n”, sizeof(struct ArrayBag)); Produces “size of arrayBag 808”

What the heck is sizeof? Sizeof is a pseduo-function that takes as argument a TYPE, and tells you how many bytes instances of the type need. printf(“size of arrayBag %d\n”, sizeof(struct ArrayBag));

Then what the heck is printf? printf is a simple printing function. Takes a formatting string and a list of values %d print integers %f %g print floats and doubles %c print characters %s print strings % print a percent sign

Two level scope There are two levels of scope Variables declared outside of any function are global (use sparingly) Variables declared inside of function are local. Declarations must be listed first, before statements. You end up passing more inforation in arguments than you do in Java

Parameters are by-value Arguments to functions are passed by value Means the parameter is initialized with a COPY of the argument Will simulate by-reference using pointers

Declaring a structure When you declare a variable you must use the struct keyword struct arrayBag foo; No constructors, no initialization, you must use explicit initialization routine

Access to data fields Access to data fields uses the same dot notation you are used to: struct arrayBag myData; myData.count = 3; (but often combined with pointers…)

Pointers Pointers in C are explicit (implicit in Java) A pointer is simply a value that can refer to another location in memory. 42 A valueA pointer

Pointer values vs. thing pointed to Important to distinguish between the value of the pointer and the value of the thing the pointer points to 42

Common Example Struct arrayBag myBag; initBag(&myBag); addBag(&myBag, 3.1) addBag(&myBag, 2.7); removeBag(&myBag, 3.1); Printf(“bag now has %d elements”, bagSize(&myBag));

Structures and arguments Remember, arguments are passed by value If you pass a structure, the ENTIRE structure is copied into the space for the actual argument value Wasteful, therefore we use pointers are arguments

What’s with the & ?? Need to declare a structure. Have to have space someplace Passing a structure is inefficient (parameters passed by value, remember?) Pass a pointer, faster, changes inside the function reflected outside.

Inside the function void initBag (struct arrayBag * b) { b->count = 0; } …. Struct arrayBag myBag; initBag (&myBag);

What is happening? Count EleType data[100]

What is with the arrow? The arrow syntax ( -> ) is a shorthand for two actions - dereference the pointer, and access a field. It says, give me the named field in the thing I’m pointing to b->count = 0; /* same as (*b).count */

Structures and by-ref params Very common idiom: struct arrayBag a; /* note, value, not ptr*/ arrayBagInit (&a); /* pass by ref */ arrayBagAdd (&a, );

Lets finish yesterdays worksheet void addBag (struct arrayBag * b, EleType v) { /* add a new element to the bag */ assert(b->count < 100); b->data[b->count] = v; b->count++; }

Test a value Int testBag (struct arrayBag * b, EleType v) { /* see if a value is in the bag */ int i; for (i = 0; i count; i++) { if (EQ(v, b->data[i])) return 1; } return 0; }

What a moment, what is EQ? A macro (simple function) used to define equal testing. Makes it easy to change later. # define EQ(a, b) (a == b)

What about remove? Can’t have an “empty” location in our bag. Since bags are not ordered, could either slide things down or just copy last value. Lets do latter now, will see former in a few moments.

remove Void removeBag (struct arrayBag * b, EleType v) { /* find and remove first occurrence of v */ int i; for (i = 0; i count; i++) { if (EQ(v, b->data[i])) { b->data[i] = b->data[b->count-1]; b->count--; return; /* break out of loop */ }

Questions What if v occurs more than once in the bag? What if v is found in the last location? What is the big-Oh running time of remove?

Bigger Questions What if our problem requires a collection that only needs to hold five values? What if our problem requires a collection that needs to hold 200 values? What is we don’t know the size of the collection we will need? Leads to next topic