CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013

Similar documents
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 14, FALL 2012

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

Programming in C. Pointers and Arrays

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

POINTER AND ARRAY SUNU WIBIRAMA

Lesson #8. Structures Linked Lists Command Line Arguments

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

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)

CS2141 Software Development using C/C++ C++ Basics

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Algorithms & Programming. Midterm Examination # 2. Student Name: Student ID Number: Duration of examination:

Kurt Schmidt. October 30, 2018

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

EE 312 Fall 2018 Midterm 1 Version A October 10, 2018

Arrays and Pointers (part 2) Be extra careful with pointers!

Final CSE 131B Spring 2004

Arrays and Pointers (part 2) Be extra careful with pointers!

EL2310 Scientific Programming LAB2: C lab session. Patric Jensfelt, Andrzej Pronobis

CS 108 Computing Fundamentals. October/November Array Bootcamp

Intermediate Programming, Spring 2017*

CS201 - Lecture 1 The C Programming Language

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

PRINCIPLES OF OPERATING SYSTEMS

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

src7-malan/c/array/array/main.c // main.c // Array // David J. Malan // Harvard University // // Demonstrates arrays. 11.

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

PROGRAMMAZIONE I A.A. 2017/2018

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Course organization. Course introduction ( Week 1)

EE 312 Fall 2017 Midterm 1 October 12, 2017

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

Memory Allocation in C

Functions & Memory Maps Review C Programming Language

CSE 124 Discussion (10/3) C/C++ Basics

ECE264 Fall 2013 Exam 1, September 24, 2013

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Array Initialization

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Overview (1A) Young Won Lim 9/25/17

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

CMSC 202. Pointers Dynamic Memory Allocation

Project#3 [40 points] COP 2220 Fall 2017

Binghamton University. CS-211 Fall Pointers

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Character Strings. String-copy Example

A Quick Look at C for C++ Programmers

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

COMP2521. Generic ADTs in C

CSE 333 Midterm Exam 5/10/13

Week 7 Part I. Kyle Dewey. Monday, August 6, 12

S-1. Concepts this lecture. CSE 142 Computer Programming I. Chapter 11. Review: Data Structures. Problem: Account Records.

COMP 2355 Introduction to Systems Programming

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

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

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

LSN 3 C Concepts for OS Programming

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

C Programming Basics II

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Abstract Data Types. Lecture 05 Summary. Abstract Data Types Structures in C 1/26/2009. Slides by Mark Hancock (adapted from notes by Craig Schock)

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Data Representation and Storage

ECE264 Spring 2013 Final Exam, April 30, 2013

Overview (1A) Young Won Lim 9/14/17

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

HW1 due Monday by 9:30am Assignment online, submission details to come

CSCI2467: Systems Programming Concepts

Overview (1A) Young Won Lim 9/9/17

CS240: Programming in C

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

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

Pointers and scanf() Steven R. Bagley

ECE264 Fall 2013 Exam 2, October 24, 2013

Objectives. External declarations Pointers Dynamic data structures using pointers Function pointers

Personal SE. Functions, Arrays, Strings & Command Line Arguments

ECE264 Spring 2013 Exam 1, February 14, 2013

COMP 2355 Introduction to Systems Programming

Exercise Session 2 Simon Gerber

Dynamic memory allocation

Bit Manipulation in C

Structured Programming. Functions and Structured Programming. Functions. Variables

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

CSC231 C Tutorial Fall 2018 Introduction to C

Pointers. Pointers. Pointers (cont) CS 217

Transcription:

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013

RECAP ARRAYS VS. POINTERS

C Parameter Passing Notes We'll say formal parameter vs actual parameter. Formal parameters are place holders in function definition. Actual parameters (aka arguments) actually have a value. In C, all parameters are passed by value. Parameter passing by reference is simulated by passing the address of the variable. scanf("%d", &n) ;! Array names represent the address of the array. In effect, arrays are passed by reference. int UpdateArray (int A[], int n) {! A[0] += 5 ;!...!

Printing an Array The code below shows how to use a parameter array name as a pointer. void printgrades( int grades[ ], int size ) { int i; for (i = 0; i < size; i++) printf( %d\n, *grades ); ++grades; } What about this prototype? void printgrades( int *grades, int size );

Passing Arrays Arrays are passed by reference (its address is passed by value):!!!int sumarray( int A[], int size) ;! is equivalent to!int sumarray( int *A, int size) ;! Use A as an array name or as a pointer. The compiler always sees A as a pointer. In fact, any error messages produced will refer to A as an int *

sumarray int sumarray( int A[ ], int size) { } int k, sum = 0; for (k = 0; k < size; k++) sum += A[ k ]; return sum;

sumarray (2) int sumarray( int A[ ], int size) { int k, sum = 0; for (k = 0; k < size; k++) sum += *(A + k); return sum; } int sumarray( int A[ ], int size) { int k, sum = 0; for (k = 0; k < size; k++) } sum += *A; ++A; } return sum; }

CHARACTERS & STRINGS & POINTERS

Strings revisited Recall that a string is represented as an array of characters terminated with a null (\0) character. As we ve seen, arrays and pointers are closely related. A string constant may be declared as either as in char[ ] or char * char hello[ ] = Hello Bobby ; or (almost) equivalently char *hi = Hello Bob ; A typedef could also be used to simplify coding typedef char* STRING; STRING hi = Hello Bob ;

Arrays of Pointers Since a pointer is a variable type, we can create an array of pointers just like we can create any array of any other type. Although the pointers may point to any type, the most common use of an array of pointers is an array of char* to create an array of strings.

Boy s Names A common use of an array of pointers is to create an array of strings. The declaration below creates an initialized array of strings (char *) for some boys names. The diagram below illustrates the memory configuration. char *name[] = { Bobby, Jim, Harold }; name: 0 1 2 B o b b y \0 J i m \0 H a r o l d \0

Command Line Arguments Command line arguments:./a.out breakfast lunch dinner! These arguments are passed to your program as parameters to main. int main( int argc, char *argv[ ] )! argc is the number of command line arguments argv is an array of argc strings argv[0]is always the name of your executable program. The rest of argv[] are the remaining strings on the command line.

Command Line Arguments (2) Example, with this command at the Linux prompt: we get myprog hello world 42! argc = 4! argv[0] = "myprog"! argv[1] = "hello"! argv[2] = "world"! argv[3] = "42"! Note: argv[3] is a string NOT an integer. Convert using atoi(): int answer = atoi( argv[3] );!

STRUCTS & POINTERS

Reminder You can t use a pointer until it points to something Just declaring a variable to be a pointer is not enough int *name; /* pointer declaration */ int age = 42; *name += 12; printf( My age is %d\n, *name);

Pointers to Pointers A pointer may point to another pointer. Consider the following declarations int age = 42;!! /* an int */! int *page = &age;!/* a pointer to an int */! int **ppage = &page;/* pointer to pointer to int */! Draw a memory picture of these variable and their relationships What type and what value do each of the following represent?! age, page, ppage, *page, *ppage, **ppage!

pointers2pointer.c int main ( )! {!!/* a double, a pointer to double,!!** and a pointer to a pointer to a double */!!double gpa = 3.25, *pgpa, **ppgpa;!!!!/* make pgpa point to the gpa */!!pgpa = &gpa;!!!!/* make ppgpa point to pgpa (which points to gpa) */!!ppgpa = &pgpa;!!!!// what is the output from this printf statement?!!printf( "%0.2f, %0.2f, %0.2f", gpa, *pgpa, **ppgpa);!!!!return 0;! }!

typedef struct student { char name[50]; char major [20]; double gpa; } STUDENT; Pointers to struct STUDENT bob = {"Bob Smith", "Math", 3.77}; STUDENT sally = {"Sally", "CSEE", 4.0}; STUDENT *pstudent; /* pstudent is a "pointer to struct student" */ pstudent = &bob; /* make pstudent point to bob */ /* use -> to access the members */ printf ("Bob's name: %s\n", pstudent->name); printf ("Bob's gpa : %f\n", pstudent->gpa); /* make pstudent point to sally */ pstudent = &sally; printf ("Sally's name: %s\n", pstudent->name); printf ("Sally's gpa: %f\n", pstudent->gpa);

Pointer in a struct The data member of a struct can be a pointer #define FNSIZE 50! #define LNSIZE 40! typedef struct name! {!!char first[ FNSIZE + 1 ];!!char last [ LNSIZE + 1 ];! } NAME;!! typedef struct person! {!!NAME *pname;!// pointer to NAME struct!!int age;!!double gpa;! } PERSON;!

Pointer in a struct (2) Given the declarations below, how do we access bob s name, last name, and first name? Draw a picture of memory represented by these declarations NAME bobsname = { Bob, Smith };! PERSON bob;! bob.age = 42;! bob.gpa = 3.4;! bob.pname = &bobsname;!!

Self-referencing structs Powerful data structures can be created when a data member of a struct is a pointer to a struct of the same kind. The simple example on the next slide illustrates the technique.

teammates.c typedef struct player { char name[20]; struct player *teammate;/* can t use TEAMMATE yet */ } TEAMMATE; TEAMMATE *team, bob, harry, john; team = &bob; /* first player */ strncpy(bob.name, bob, 20); bob.teammate = &harry; /* next teammate */ strncpy(harry.name, harry, 20); harry.teammate = &john; /* next teammate */ strncpy(john.name, bill, 20); john.teammate = NULL: /* last teammate */

teammates.c (cont d) /* typical code to print a (linked) list */ /* follow the teammate pointers until ** NULL is encountered */ // start with first player TEAMMATE *t = team; // while there are more players... while (t!= NULL) { printf( %s\n, t->name); } // next player t = t->teammate;

NEXT TIME Dynamic memory allocation