Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
Programming and Data Structure
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
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.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
Week #10 Kyle Dewey. Overview Meeting times Typos Project #3 tidbits Exam Review.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Programming in C Pointer Basics.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Week 3 Part I Kyle Dewey. Overview Odds & Ends Constants Errors Functions Expressions versus statements.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Object-Oriented Programming in C++
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:
Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CPS120: Introduction to Computer Science Lecture 15A Structures.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Arrays and Strings in Assembly
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
char first[10]="monkey"; char second[10]="fish"; char* keep;
User-Written Functions
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Motivation and Overview
Introduction to Computer Science / Procedural – 67130
C Programming Tutorial – Part I
Week 7 Part 2 Kyle Dewey.
Programming in C Pointer Basics.
Built-In (a.k.a. Native) Types in C++
Programming in C Pointer Basics.
Programming in C Pointer Basics.
C++ Pointers and Strings
Programming in C Pointers and Arrays.
C++ Pointers and Strings
Presentation transcript:

Week 7 Part I Kyle Dewey

Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)

Code Wrap-Up

Array Initialization // fine int arr1[3] = { 1, 2 3 };... // compiler issues warning int arr2[2] = { 4, 5, 6 };... // arr3 contains { 1, 0, 0 }; int arr3[3] = { 1 };

Pointers vs. Arrays

Massive point of confusion that even C veterans mess up on C allows for pointers to be treated like arrays: char* string = “moo”; // string is a pointer string [ 0 ]; // returns ‘m’

Pointers vs. Arrays However, C also has an explicit array type char* string1 = “moo”; // pointer char string2[] = “cow”; // array

Pointers vs. Arrays Pointers can point anywhere, as long as it is of the correct type char character; char* string1; string1 = “moo”; string1 = “cow”; string1 = &character;

Pointers vs. Arrays Variables of the array type can only point to what they were initialized to char string[] = “foobar”; string[ 0 ]; // returns ‘f’ string = “foo”; // compiler error

Pointers vs. Arrays Variables of the array type must be initialized to something gcc gives an error ch allows this but crashes if you try to do anything with it char string[]; // compiler error

...what? Questions: Why introduce a special type that is more restrictive than a pointer? Why can’t they be reassigned? Why is this useful?

Internal Representation A pointer is a variable that holds a memory address The array type is actually an address in and of itself Effectively a constant

Internal Representation Since it acts like a constant, it cannot be reassigned When we say: char string[] = “moo”; printf( “%s”, string );...the compiler replaces all occurrences of string with the actual memory address where “moo” is stored

Internal Representation When we say: char* string = “moo”; printf( “%s”, string );...the compiler will first look up what the value of string currently is, and pass that value along to printf as a memory address There is an extra step here

Analogy With the array type, it’s like: #define CONSTANT 5 printf( “%i”, CONSTANT ); With the pointer type, it’s like: int x = 5; printf( “%i”, x );

Decay Array types can decay to a pointer type This can be seen with functions: void foo( int* pointer ); int main() { int arr[] = { 1, 2, 3 }; foo( arr ); // legal }

What to Remember Pointers can act like arrays, but arrays cannot act like pointers When the compiler starts complaining about * versus [], this could be why

Structs

Problem We want to represent a phone book Each entry has: Name Phone number Address

Question Which type(s) is/are appropriate for: Name? Phone Number? Address?

Possible Representation Use parallel arrays Each array holds one kind of item Index N refers to all information for entry #N char** name; char** address; int* phoneNumber;

Problem Poor separation of concerns We have to pass around everything related to one person, which is annoying and error prone void printPerson( char* name, char* address, int phone );

Another Solution Use structures, aka. struct s Put all data relevant to one entry in one place struct person { char* name; char* address; int phone; };

Structs struct person { char* name; char* address; int phone; }; void printPerson( struct person p );

Accessing Structs Use the dot (.) operator void printPerson( struct person p ) { printf( “Name: %s\n”, p.name ); printf( “Address: %s\n”, p.address ); printf( “Phone: %i\n”, p.phone ); } struct person { char* name; char* address; int phone; };

Modifying Structs The dot (.) operator can be used along with assignment struct person { char* name; char* address; int phone; }; struct person p; p.name = “foo”; p.address = “123 Fake Street”; p.phone =

Initializing Structs For a struct definition like so: struct pair { int x; int y; }; We can do: struct pair p = { 2, 3 }; struct pair p2 = {.x = 2,.y = 3 }; (Doesn’t work in ch, but it does in gcc)

Pointers to Structs Structs can also be accessed via pointers Can access like so: struct person p; struct person* pointer = &p; (*p).name = “foo”; (*p).address = (*p).name; (*p).phone =

Pointers to Structs Structs can also be accessed via pointers Can also access with the more readable arrow operator struct person p; struct person* pointer = &p; p->name = “foo”; p->address = p->name; p->phone =

Struct Semantics Consider again: void printPerson( struct person p ); When structs are passed, the whole thing is copied Note that this is a shallow copy

Shallow Copy struct person { char* name; char* address; int phone; }; “Bob”“124 Fake Street” nameaddress phone

Shallow Copy “Bob”“124 Fake Street” nameaddress phone phone nameaddress

Question struct foo { int x; }; void bar( struct foo f ) { f.x = 10; } int main() { struct foo f; f.x = 5; bar( f ); // what’s f.x? return 0; }

Question struct foo { char* x; }; void bar( struct foo f ) { f.x = “moo”; } int main() { struct foo f; f.x = “cow”; bar( f ); // what’s f.x? return 0; }

Question struct foo { int x; }; void bar( struct foo* f ) { f->x = 10; } int main() { struct foo f; f.x = 5; bar( &f ); // what’s f.x? return 0; }

Question struct foo { char* x; }; void bar( struct foo* f ) { f->x = “moo”; } int main() { struct foo f; f.x = “cow”; bar( &f ); // what’s f.x? return 0; }

Structs and Pointers Oftentimes programmers will prefer pointers to structs as opposed to just structs Avoids extra copying Possibly appropriate

typedef

Defines a new type that is an alias for another type

Example struct foo { int x; }; void bar( struct foo f ) { f.x = 10; } Before typedef...

Example struct foo { int x; }; typedef struct foo Foo; void bar( Foo f ) { f.x = 10; } After typedef

More Examples typedef long double ld; typedef unsigned long ul; typedef int SuperAwesome;

Uses Shorten type names A point of abstraction // for one computer typedef EightBytes int; // for another computer typedef EightBytes long;

Bubble Sort (If time)

Bubble Sort Another sorting algorithm Basic idea: Go through a list of numbers Compare them pairwise If a pair is out of order, swap them Keep doing this until no swaps occur

Example We want to sort according to integer <=

Example firstsecond Swap occurred?: False

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: False

Example firstsecond Swap occurred?: False

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: False

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: True

Example firstsecond Swap occurred?: False

Example firstsecond Swap occurred?:True

Example firstsecond Swap occurred?:True

Example firstsecond Swap occurred?:True

Example firstsecond Swap occurred?:True

Example firstsecond Swap occurred?:True

Example firstsecond Swap occurred?:True

Example firstsecond Swap occurred?:False

Example firstsecond Swap occurred?:False

Example firstsecond Swap occurred?:False

Example firstsecond Swap occurred?:False

Example firstsecond Swap occurred?:False

Example firstsecond Swap occurred?:False

Code