Tutorial 6. Memory Addresses and Pointers. scanf() function. Perils of Pointers. Function Pointers. Declarations vs. Definitions
|
|
- Dwain Mitchell
- 5 years ago
- Views:
Transcription
1 Tutorial 6 Memory Addresses and Pointers scanf() function Perils of Pointers Function Pointers Declarations vs. Definitions CS 136 Spring 2018 Tutorial 6 1
2 Memory Addresses and Pointers A pointer can be declared by placing an indirection operator (*) before the identifier. C requires you to specify the type of the value stored at an address. int i = 42; int *p = &i; Note that in this example we are declaring the variable named p of the type int *. You can get the starting address of where the value of an identifier is stored in memory by using the address operator (&). CS 136 Spring 2018 Tutorial 6 2
3 Memory Addresses and Pointers To get the value of what a pointer "points at", we use the indirection operator (*). This is often known as the deference operator and it is the inverse of the address operator (&). // To print the value p is pointing at printf("the value of i is: %d", *p); CS 136 Spring 2018 Tutorial 6 3
4 Pointers as Function Parameters Allows functions to mutate variables that live outside the function. Allows functions to avoid copying large structures. CS 136 Spring 2018 Tutorial 6 4
5 Example: Mutating Values in Functions Compare these two segments of code void inc(int p) { p += 1; } int main(void) { int x = 42; inc(x); printf("%d\n", x); } Output: 42 void inc(int *p) { *p += 1; } int main(void) { int x = 42; inc(&x); printf("%d\n", x); } Output: 43 CS 136 Spring 2018 Tutorial 6 5
6 Example: Call Stacks with Pointers Trace the content of the Call Stack for the following program 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } CS 136 Spring 2018 Tutorial 6 6
7 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } p1: p2:.x = 3.y = 4.x = 5.y = 6 CS 136 Spring 2018 Tutorial 6 7
8 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 3.y = 4 p2: [addr_2].x = 5.y = 6 CS 136 Spring 2018 Tutorial 6 8
9 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_3 j: addr_4 temp:? return address: swap_posn:9 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 3 [addr_3].y = 4 p2: [addr_2].x = 5 [addr_4].y = 6 CS 136 Spring 2018 Tutorial 6 9
10 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 * i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_3 j: addr_4 temp: 3 return address: swap_posn:9 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 3 [addr_3].y = 4 p2: [addr_2].x = 5 [addr_4].y = 6 CS 136 Spring 2018 Tutorial 6 10
11 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 * j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_3 j: addr_4 temp: 3 return address: swap_posn:9 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 4 p2: [addr_2].x = 5 [addr_4].y = 6 CS 136 Spring 2018 Tutorial 6 11
12 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 4 p2: [addr_2].x = 3 [addr_4].y = 6 CS 136 Spring 2018 Tutorial 6 12
13 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 4 p2: [addr_2].x = 3 [addr_4].y = 6 CS 136 Spring 2018 Tutorial 6 13
14 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_5 j: addr_6 temp:? return address: swap_posn:10 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 4 [addr_5] p2: [addr_2].x = 3 [addr_4].y = 6 [addr_6] CS 136 Spring 2018 Tutorial 6 14
15 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 * i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_5 j: addr_6 temp: 4 return address: swap_posn:10 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 4 [addr_5] p2: [addr_2].x = 3 [addr_4].y = 6 [addr_6] CS 136 Spring 2018 Tutorial 6 15
16 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 * j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_5 j: addr_6 temp: 4 return address: swap_posn:10 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 6 [addr_5] p2: [addr_2].x = 3 [addr_4].y = 6 [addr_6] CS 136 Spring 2018 Tutorial 6 16
17 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 6 [addr_5] p2: [addr_2].x = 3 [addr_4].y = 4 [addr_6] CS 136 Spring 2018 Tutorial 6 17
18 Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } End of Stack Trace p1: [addr_1].x = 5 [addr_3].y = 6 [addr_5] p2: [addr_2].x = 3 [addr_4].y = 4 [addr_6] CS 136 Spring 2018 Tutorial 6 18
19 scanf() function Reads data and stores it based on the parameter format. Returns the number of values that have been successfully read. It returns EOF if it hits the end of the input. For example: (See Seashell) char char1; char char2; printf("enter a character:"); scanf("%c", &char1); // can read whitespace printf("the input is %c\n", char1); scanf(" %c", &char2); // ignores whitespace printf("the input is %c\n", char2); CS 136 Spring 2018 Tutorial 6 19
20 Perils of Pointers: Expired Scopes What will this program output when foo runs? When it doesn t run? // Get a bad address int *weird(int a) { return &a; } int main(void) { int p_content; int *p = weird(5); // #JUSTFOOTHINGS void foo(void) { int x = 1; } } //foo(); p_content = *p; printf("%d\n", p_content); CS 136 Spring 2018 Tutorial 6 20
21 Example: Perils of Pointers: Stack Trace Trace the content of the Call Stack for the following program 1 int *foo(int a) { 2 return &a; 3 } 4 5 int main(void) { 6 int x = 42; 7 int *p = foo(x); 8 printf("%d\n", *p); 9 } CS 136 Spring 2018 Tutorial 6 21
22 Example: Perils of Pointers: Stack Trace 1 int *foo(int a) { 2 return &a; 3 } 4 5 int main(void) { 6 int x = 42; 7 int *p = foo(x); 8 printf("%d\n", *p); 9 } x: 42 p:? CS 136 Spring 2018 Tutorial 6 22
23 Example: Perils of Pointers: Stack Trace 1 int *foo(int a) { 2 return &a; 3 } 4 5 int main(void) { 6 int x = 42; 7 int *p = foo(x); 8 printf("%d\n", *p); 9 } foo: a: 42 [addr] return address: 7 x: 42 p:? CS 136 Spring 2018 Tutorial 6 23
24 Example: Perils of Pointers: Stack Trace 1 int *foo(int a) { 2 return &a; 3 } 4 5 int main(void) { 6 int x = 42; 7 int *p = foo(x); 8 printf("%d\n", *p); 9 } End of Stack Trace x: 42 p: addr CS 136 Spring 2018 Tutorial 6 24
25 Example: Perils of Pointers: Stack Trace x: 42 p: addr Notice that the stack frame for main has a pointer to addr. However, the stack frame which contains the contents of addr has been popped. Accessing the contents of this memory will result in undefined behaviour. CS 136 Spring 2018 Tutorial 6 25
26 Function Pointers Functions are not first-class values in C. Function pointers, on the other hand, are. A function pointer stores the (starting) address of a function, which is an address in the code section of memory. The syntax to declare a function pointer with name fpname is: return_type (*fpname)(param1_type, param2_type,...) A function pointer can only point to a function that already exists. CS 136 Spring 2018 Tutorial 6 26
27 Problem: find_max Implement the function find_max(fp, lower, upper) on Seashell. The function does the following: // find_max(fp, lower, upper) returns the // maximum value of fp(x) in the range // [lower, upper] // requires: upper > lower CS 136 Spring 2018 Tutorial 6 27
28 Declarations vs. Definitions // Declaration: bool is_impossible(int power_level); // Definition: bool is_impossible(int power_level) { return (power_level > 9000); } CS 136 Spring 2018 Tutorial 6 28
29 Why? Reusability. Maintainability. Abstraction. CS 136 Spring 2018 Tutorial 6 29
Tutorial 5. Call Stack and Stack Frames. Memory Addresses and Pointers. Content vs Address of Pointers. scanf() function. Perils of Pointers
Tutorial 5 Call Stack and Stack Frames Memory Addresses and Pointers Content vs Address of Pointers scanf() function Perils of Pointers Function Pointers CS 136 Winter 2018 Tutorial 5 1 Call Stack and
More informationIntroduction to Pointers in C. Address operator. Pointers. Readings: CP:AMA 11, 17.7
Introduction to Pointers in C Readings: CP:AMA 11, 17.7 CS 136 Fall 2017 06: Pointers 1 Address operator C was designed to give programmers low-level access to memory and expose the underlying memory model.
More informationTutorial 5. Overflow. Data Types. Structures. Call stack. Stack frames. Debugging Tips. CS 136 Winter 2019 Tutorial 5 1
Tutorial 5 Overflow Data Types Structures Call stack. Stack frames. Debugging Tips CS 136 Winter 2019 Tutorial 5 1 Integer Overflow: Introduction Any variable in C takes up a certain amount of memory (bits).
More informationMidterm Review. Short Answer. Short Answer. Practice material from the Winter 2014 midterm. Will cover some (but not all) of the questions.
Midterm Review Practice material from the Winter 2014 midterm. Will cover some (but not all) of the questions. Midterm content: Everything up to / including modules. CS 136 Spring 2018 Tutorial 7 1 List
More informationC Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function
More informationCS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.
CS 31 Discussion 1A, Week 4 Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m. Today s focus Notes from the project 2 grading Function call predefined function define a function
More informationCMPE-013/L. Introduction to C Programming. Unit testing
CMPE-013/L Introduction to C Programming Gabriel Hugh Elkaim Winter 2015 Example Unit testing Testing architecture // Declare test constants testinput some input testexpoutput precalculated output // Calculate
More informationAgenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures
The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF
More informationCS113: Lecture 5. Topics: Pointers. Pointers and Activation Records
CS113: Lecture 5 Topics: Pointers Pointers and Activation Records 1 From Last Time: A Useless Function #include void get_age( int age ); int age; get_age( age ); printf( "Your age is: %d\n",
More informationStorage class and Scope:
Algorithm = Logic + Control + Data Data structures and algorithms Data structures = Ways of systematically arranging information, both abstractly and concretely Algorithms = Methods for constructing, searching,
More informationCSCI 2132 Software Development. Lecture 17: Functions and Recursion
CSCI 2132 Software Development Lecture 17: Functions and Recursion Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 15-Oct-2018 (17) CSCI 2132 1 Previous Lecture Example: binary
More informationa data type is Types
Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental
More informationCSCI 2132 Software Development. Lecture 18: Functions
CSCI 2132 Software Development Lecture 18: Functions Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 18-Oct-2017 (18) CSCI 2132 1 Previous Lecture Example: binary search Multidimensional
More informationPointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables
Chapter 11 Pointers The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into bytes, with each byte capable
More information11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {
This document contains the questions and solutions to the CS107 midterm given in Spring 2016 by instructors Julie Zelenski and Michael Chang. This was an 80-minute exam. Midterm questions Problem 1: C-strings
More informationCOP 3223 Introduction to Programming with C - Study Union - Spring 2018
COP 3223 Introduction to Programming with C - Study Union - Spring 2018 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................
More informationCOP 3223 Introduction to Programming with C - Study Union - Fall 2017
COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................
More informationCS24 Week 3 Lecture 1
CS24 Week 3 Lecture 1 Kyle Dewey Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time) Key Minor Points const Motivation
More informationWednesday, October 15, 14. Functions
Functions Terms void foo() { int a, b;... bar(a, b); void bar(int x, int y) {... foo is the caller bar is the callee a, b are the actual parameters to bar x, y are the formal parameters of bar Shorthand:
More informationC Model: Memory & Control Flow
C Model: Memory & Control Flow Readings: CP:AMA 6.1 6.4, 7.1 7.3, 7.6, Appendix E Course Notes: Memory Appendix the ordering of topics is different in the text some portions of the above sections have
More informationProgramming in C++: Assignment Week 1
Programming in C++: Assignment Week 1 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com February 24,
More information/* EXAMPLE 1 */ #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j);
You try /* EXAMPLE 3 */ #include int main(void) { char ch = 'c'; char *chptr = &ch; int i = 20; int *intptr = &i; float f = 1.20000; float *fptr = &f; char *ptr = "I am a string"; /* EXAMPLE
More informationPointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island
Pointers and Structure Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Pointer Variables Each variable in a C program occupies space in
More informationChapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook
Chapter 5 Section 5.1 Introduction to Strings CS 50 Hathairat Rattanasook "Strings" In computer science a string is a sequence of characters from the underlying character set. In C a string is a sequence
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationGabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming
1 2 3 4 CMPE 013/L Pointers and Functions Gabriel Hugh Elkaim Spring 2013 Pointers and Functions Passing Pointers to Functions Normally, functions operate on copies of the data passed to them (pass by
More informationSlide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 1 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 1 slide 2/43
More informationAgenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture
More information#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;
1. Short answer questions: (a) Compare the typical contents of a module s header file to the contents of a module s implementation file. Which of these files defines the interface between a module and
More informationConstants, References
CS 246: Software Abstraction and Specification Constants, References Readings: Eckel, Vol. 1 Ch. 8 Constants Ch. 11 References and the Copy Constructor U Waterloo CS246se (Spring 2011) p.1/14 Uses of const
More informationLinked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links
Linked-List Basic Examples A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links Accessed via a pointer to the first node of the list Subsequent
More informationSlides adopted from T. Ferguson Spring 2016
CSE3 Introduction to Programming for Science & Engineering Students Mostafa Parchami, Ph.D. Dept. of Comp. Science and Eng., Univ. of Texas at Arlington, USA Slides adopted from T. Ferguson Spring 06 Pointers
More informationTHE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I
THE GOOD, BAD AND UGLY ABOUT POINTERS Problem Solving with Computers-I The good: Pointers pass data around efficiently Pointers and arrays 100 104 108 112 116 ar 20 30 50 80 90 ar is like a pointer to
More informationChapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University C: A High-Level Language! Gives
More informationLecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions
Lecture 02 Summary C/Java Syntax Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 1 2 By the end of this lecture, you will be able to identify the
More informationChapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified by Chris Wilcox, Yashwant Malaiya Colorado State University C: A High-Level Language
More informationIntro to C: Pointers and Arrays
Lecture 4 Computer Science 61C Spring 2017 January 25th, 2017 Intro to C: Pointers and Arrays 1 Administrivia Teaching Assistants: Let s try that again. Lectures are recorded. Waitlist/Concurrent Enrollment
More informationC/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)
C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 By the end of this lecture, you will be able to identify the
More informationC/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for
C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 1 By the end of this lecture, you will be able to identify
More informationChapter 11 Introduction to Programming in C
C: A High-Level Language Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Gives
More informationFunctions CMPE-013/L. Functions. Functions What is a function? Functions Remember Algebra Class? Gabriel Hugh Elkaim Winter 2014.
CMPE-013/L Program Structure Gabriel Hugh Elkaim Winter 2014 main()... eat();... drink();... eat()... return; drink()... be_merry(); return; be_merry()... return; Definition What is a function? are self
More informationWhy Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...
Why Pointers Pointers They provide the means by which functions can modify arguments in the calling function. They support dynamic memory allocation. They provide support for dynamic data structures, such
More informationAPS105. Pointers. Memory RAM 11/5/2013. Pointers. Need a way to refer to locations of things. a pointer: Address. Example: In C
APS105 Pointers Textbook Chapter5 1 A Broken Swap Example: a function to swap two values void swap(int x, int y) int temp = x; x = y; y = temp; int main() int a = 1, b = 2; swap(a,b); printf( %d %d\n,a,b);
More informationChapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C C: A High-Level Language Gives symbolic names to values don t need to know which register or memory location Provides abstraction of underlying hardware operations
More informationUNIT III (PART-II) & UNIT IV(PART-I)
UNIT III (PART-II) & UNIT IV(PART-I) Function: it is defined as self contained block of code to perform a task. Functions can be categorized to system-defined functions and user-defined functions. System
More informationCS24 Week 4 Lecture 1
CS24 Week 4 Lecture 1 Kyle Dewey Overview Additional use of const in C++ List ADT Array Lists Linked Lists Additional Use of const We ve seen const already in two positions: What is pointed to is constant
More informationPointers. Mr. Ovass Shafi (Assistant Professor) Department of Computer Applications
Pointers Introduction: A variable is a named memory location that holds some value. Each variable has some address associated with it. Till now we only worked on the values stored in the variables and
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationDynamic Memory & ADTs in C. The heap. Readings: CP:AMA 17.1, 17.2, 17.3, The primary goal of this section is to be able to use dynamic memory.
Dynamic Memory & ADTs in C Readings: CP:AMA 17.1, 17.2, 17.3, 17.4 The primary goal of this section is to be able to use dynamic memory. CS 136 Winter 2018 10: Dynamic Memory & ADTs 1 The heap The heap
More informationDynamic Memory & ADTs in C
Dynamic Memory & ADTs in C Readings: CP:AMA 17.1, 17.2, 17.3, 17.4 The primary goal of this section is to be able to use dynamic memory. CS 136 Winter 2018 10: Dynamic Memory & ADTs 1 The heap The heap
More informationG52CPP C++ Programming Lecture 9
G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And
More informationCS31 Discussion 1E Spring 17 : week 08
CS31 Discussion 1E Spring 17 : week 08 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju Project 5 - Map cipher to crib Approach 1: For each pair of positions, check two letters in cipher
More informationHigh Performance Programming Programming in C part 1
High Performance Programming Programming in C part 1 Anastasia Kruchinina Uppsala University, Sweden April 18, 2017 HPP 1 / 53 C is designed on a way to provide a full control of the computer. C is the
More informationRunning a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2
CMPT 125 Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance John Edgar 2 Edit or write your program Using a text editor like gedit Save program with
More informationC for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.
C for Java Programmers 1 Last Week Very short history of C Overview of the differences between C and Java The C language (keywords, types, functies, etc.) Compiling (preprocessor, compiler, linker) C for
More informationCS240: Programming in C
CS240: Programming in C Lecture 5: Functions. Scope of variables. Program structure. Cristina Nita-Rotaru Lecture 5/ Fall 2013 1 Functions: Explicit declaration Declaration, definition, use, order matters.
More informationFunctions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur
Functions CS10001: Programming & Data Structures Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 Recursion A process by which a function calls itself
More informationChapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C C: A High-Level Language Gives symbolic names to values don t need to know which register or memory location Provides abstraction of underlying hardware operations
More informationFunctions. Arash Rafiey. September 26, 2017
September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic
More informationBSM540 Basics of C Language
BSM540 Basics of C Language Chapter 9: Functions I Prof. Manar Mohaisen Department of EEC Engineering Review of the Precedent Lecture Introduce the switch and goto statements Introduce the arrays in C
More informationC Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,
More informationCS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers
CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers Instructor: Justin Hsia 6/20/2012 Summer 2012 Lecture #3 1 Review of Last Lecture C Basics Variables, Functions, Flow Control,
More informationCS107, Lecture 9 C Generics Function Pointers
CS107, Lecture 9 C Generics Function Pointers Reading: K&R 5.11 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All
More informationCGS 3460 Summer 07 Midterm Exam
Short Answer 3 Points Each 1. What would the unix command gcc somefile.c -o someotherfile.exe do? 2. Name two basic data types in C. 3. A pointer data type holds what piece of information? 4. This key
More informationCOMP 2355 Introduction to Systems Programming
COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Today Class syntax, Constructors, Destructors Static methods Inheritance, Abstract
More informationComputer Programming Lecture 12 Pointers
Computer Programming Lecture 2 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Topics Introduction to Pointers Pointers and
More informationREFERENCES, POINTERS AND STRUCTS
REFERENCES, POINTERS AND STRUCTS Problem Solving with Computers-I https://ucsb-cs16-sp17.github.io/ Pointer assignment 2 int *p1, *p2, x; p1 = &x; p2 = p1; Q: Which of the following pointer diagrams best
More informationRecitation 7 Caches and Blocking. 9 October 2017
15-213 Recitation 7 Caches and Blocking 9 October 2017 Agenda Reminders Revisiting Cache Lab Caching Review Blocking to reduce cache misses Cache alignment Reminders Cache Lab is due Thursday! Exam1 is
More informationC++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes
C++ C and C++ 5. Overloading Namespaces Classes Alastair R. Beresford University of Cambridge Lent Term 2007 To quote Bjarne Stroustrup: C++ is a general-purpose programming language with a bias towards
More informationOutline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging
Outline Computer programming Debugging Hints Gathering evidence Common C errors "Education is a progressive discovery of our own ignorance." Will Durant T.U. Cluj-Napoca - Computer Programming - lecture
More informationScope: Global and Local. Concept of Scope of Variable
Concept of Scope of Variable : Computer Architecture I Instructor: Prof. Bhagi Narahari Dept. of Computer Science Course URL: www.seas.gwu.edu/~bhagiweb/cs135/ In assembly, who has access to a memory location/variable?
More informationCMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013 TOPICS TODAY Reminder: MIDTERM EXAM on THURSDAY Pointer Basics Pointers & Arrays Pointers & Strings Pointers & Structs
More informationprintf("this program adds the value 10 to a given integer number.\n\n");
PA1 Sample Solution Program 1 void add10(int *n); //Prototype int n; printf("this program adds the value 10 to a given integer number.\n\n"); printf("please enter an integer number: "); scanf("%d", &n);
More information3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type
Pointer Basics What is a pointer? pointer = memory address + type C Language III CMSC 313 Sections 01, 02 A pointer can contain the memory address of any variable type A primitive (int, char, float) An
More informationLanguage comparison. C has pointers. Java has references. C++ has pointers and references
Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified
More informationGabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming
1 2 3 CMPE 013/L and Strings Gabriel Hugh Elkaim Spring 2013 4 Definition are variables that can store many items of the same type. The individual items known as elements, are stored sequentially and are
More informationCharacters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,
Strings Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters, decimal digits, special characters and escape
More informationCOP 3223 Introduction to Programming with C - Study Union - Fall 2017
COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................
More information10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013
Midterm Topic Review Pointer Basics C Language III CMSC 313 Sections 01, 02 1 What is a pointer? Why Pointers? Pointer Caution pointer = memory address + type A pointer can contain the memory address of
More informationC programming basics T3-1 -
C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of
More informationProgramming. Functions and File I/O
Programming Functions and File I/O Summary Functions Definition Declaration Invocation Pass by value vs. pass by reference File I/O Reading Writing Static keyword Global vs. local variables 2 Functions
More informationA brief intro to pointers for the purposes of passing reference parameters
A brief intro to pointers for the purposes of passing reference parameters With respect to functions, we have only talked about pass by value parameters. Today we will discuss pass by reference parameters.
More informationCS 33. Introduction to C. Part 4. CS33 Intro to Computer Systems IV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.
CS 33 Introduction to C Part 4 CS33 Intro to Computer Systems IV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. Lifetime int count; int main() { func();... func(); // what s printed by func?
More informationCSE 333 Lecture 7 - final C details
CSE 333 Lecture 7 - final C details Steve Gribble Department of Computer Science & Engineering University of Washington Today s topics: - a few final C details header guards and other preprocessor tricks
More informationCS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12
CS16 Week 2 Part 2 Kyle Dewey Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors Type Coercion / Casting Last time... Data is internally
More informationChapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Dynamic Data Structures Instructor: Öğr. Gör. Okan Vardarlı Copyright 2004 Pearson Addison-Wesley. All rights reserved. 12-1 Dynamic Data Structure Usually, so far, the arrays and strings we
More informationVariables, Pointers, and Arrays
Variables, Pointers, and Arrays Prof. David August COS 217 http://www.cs.princeton.edu/courses/archive/fall06/cos217/ 1 Overview of Today s Lecture Pointers o Differences between value, variable, and pointer
More informationProgramming Studio #9 ECE 190
Programming Studio #9 ECE 190 Programming Studio #9 Concepts: Functions review 2D Arrays GDB Announcements EXAM 3 CONFLICT REQUESTS, ON COMPASS, DUE THIS MONDAY 5PM. NO EXTENSIONS, NO EXCEPTIONS. Functions
More informationCMPE-013/L. Introduction to C Programming
CMPE-013/L Introduction to C Programming Bryant Wenborg Mairs Spring 2014 Advanced Language Concepts Unions Function pointers Void pointers Variable-length arguments Program arguments Unions Unions Definition
More informationCMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC
CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop 11/13/07 1 TAC: Intermediate Representation Language Specific Language + Machine Independent Machine Dependent Front End AST Intermediate Code
More informationChapter 11: Compiler II: Code Generation
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005 Chapter 11: Compiler II: Code Generation www.idc.ac.il/tecs Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken This
More informationProgramming Language B
Programming Language B Takako Nemoto (JAIST) 17 December Takako Nemoto (JAIST) 17 December 1 / 17 A tip for the last homework 1 Do Exercise 9-5 ( 9-5) in p.249 (in the latest edtion). The type of the second
More informationMore File Operations. Lecture 17 COP 3014 Spring april 18, 2018
More File Operations Lecture 17 COP 3014 Spring 2018 april 18, 2018 eof() member function A useful member function of the input stream classes is eof() Stands for end of file Returns a bool value, answering
More informationParameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265
Parameter passing Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C implements call-by-value parameter passing int a =
More informationDAY 3. CS3600, Northeastern University. Alan Mislove
C BOOTCAMP DAY 3 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh and Pascal Meunier s course at Purdue Memory management 2 Memory management Two
More informationType checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E
Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E if E then S while E do S S ; S Type checking of statements The purpose
More informationCS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015
CS 141 - Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015 You may take this test with you after the test, but you must turn in your answer sheet. This test has the following sections:
More informationChapter 4. Section 4.4 Passing Parameters to Functions. CS 50 Hathairat Rattanasook
Chapter 4 Section 4.4 Passing Parameters to Functions CS 50 Hathairat Rattanasook Passing parameters Pass by Value: The variable is copied during the function call and a local copy of the variable exists
More informationLecture 2: C Programm
0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes
More informationPointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions
CMPE-013/L Gabriel Hugh Elkaim Winter 2014 Passing Pointers to Functions Normally, functions operate on copies of the data passed to them (pass by value) int x = 2, y = 0; Value of variable passed to function
More information