# Pointers (continued), arrays and strings

Size: px
Start display at page:

Transcription

1 Pointers (continued), arrays and strings 1

2 Last week We have seen pointers, e.g. of type char *p with the operators * and & These are tricky to understand, unless you draw pictures 2

3 Pointer arithmetic You can use + and - with pointers. The semantics depends on the type of the pointer: adding 1 to a pointer will go to the next location, given the size and the data type that it points to. For example, if int *ptr; char *str; then ptr + 2 means ptr + 2 * sizeof(int) str + 2 means str + 2 because sizeof(char) is 1 3

4 Using pointers as arrays The way pointer arithmetic works means that a pointer to the head of an array behaves like an array. Suppose int a[10] = {1,2,3,4,5,6,7,8,9,19}; int *p = (int *) &a; // the address of the head of a // treated as pointer to an int Now p + 3 points to a[3] So we use addition to pointer p to access the array 4

5 Pointer arithmetic for strings What is the output of char *msg = "hello world"; char *t = msg + 6; printf("t points to the string %s.", t); This will print t points to the string world. 5

6 Arrays vs pointers Arrays and pointers behave similarly, but are very different in memory Consider int a[]; int *p A difference: a will always refer to the same array, whereas p can point to different arrays over time 6

7 Using pointers as arrays Suppose int a[10] = {1,2,3,4,5,6,7,8,9,10}; This cast is needed because a is an Then integer array, so &a is a pointer to int sum = 0; int[], not pointer for (int i = 0; i!= 10; i++) { to an int. sum = sum + a[i]; An alternative } would be to write *p = &(a[0]) can also be implemented using pointer arithmetic int sum = 0; for (int *p = (int *)&a; p!= &(a[10]); p++) { sum = sum + *p; } Instead of p!= &(a[10]) we could also write p!= ((int *)&a)+10 But nobody in their right mind would 7

8 A problem with pointers:... int i; int j; int *x;... // lots of code omitted i = 5; j++ // what is the value of i here? (*x)++; // what is the value of i here? 5 5 or 6, depending on whether *x points to i 8

9 Two pointers are called aliases if they point to the same location int i = 5; int *x = &i; int *y = &i; // x and y are aliases now (*x)++; // now i and *y have also changed to 6 Keeping track of pointers, in the presence of potential aliasing, can be really confusing, and really hard to debug... 9

10 Recap so far We have seen pointers, e.g. of type char *p with the operations * and & These are tricky to understand, unless you draw pictures We can have aliasing, where two names, say *p and c, can refer to the same variable (location in memory) We can use pointer arithmetic, and e.g. write *(p+1), and use this to access arrays Confusingly, the meaning of addition for pointers depends on their type, as +1 for pointers of type int * really means +sizeof(int) 10

11 The potential of pointers: inspecting raw memory To inspect a piece of raw memory, we can cast it to a unsigned char * and then inspect the bytes float f = 3.14; unsigned char *p = (unsigned char *) &f; printf("the representation of float %f is", f); for (int i = 0; i < sizeof(float); i++, p++);) { printf("%d", *p); } printf("\n"); 11

12 Turning pointers into numbers intptr_t defined in stdint.h is an integral type that is guaranteed to be wide enough to hold pointers. int *p; // p points to an int intptr_t i = (intptr_t) p; // the address as a number p++; i++; // Will i and p be the same? // No! i++ increases by 1, p++ with sizeof(int) There is also an unsigned version of intptr_t: uintptr_t 12

13 Strings 13

14 Strings Having seen arrays and pointers, we can now understand C strings char *s = "hello world\n"; C strings are char arrays, which are terminated by a special null character, aka a null terminator, which is written as \0 There is a special notation for string literals, between double quotes, where the null terminator is implicit. As other arrays, we can use both the array type char[] and the pointer type char * for them. 14

15 String problems Working with C strings is highly error prone! There are two problems 1. As for any array, there are no array bounds checks so it s the programmer s responsibility not to go outside the array bounds 2. It is also the programmer s responsibility to make sure that the string is properly terminated with a null character. If a string lacks its null terminator, e.g. due to problem 1, then standard functions to manipulate strings will go off the rails. 15

16 Safer strings and array? There is no reason why programming language should not provide safe versions of strings (or indeed arrays). Other languages offer strings and arrays which are safer in that: 1. Going outside the array bounds will be detected at runtime (e.g. Java) 2. Which will be resized automatically if they do not fit (e.g. Python) 3. The language will ensure that all strings are null-terminated (e.g. C++, Java and Python) More precisely, the programmer does not even have to know how strings are represented, and whether null-terminator exists and what they look like: the representation of strings is completely transparant/invisible to the programmer. Moral of the story: if you can, avoid using standard C strings. E.g. in C++, use C++ type strings; in C, use safer string libraries. 16

17 A final string pecularity Strig literals, as in char *msg = "hello, world"; are meant to be constant or read-only: you are not supposed to change the character that made up a string literal. Unfortunately, this does not mean that C will prevent this. It only means that the C standard defines changing a character in a string literal as having undefined behaviour E.g. char *t = msg + 6; *t = ; ; Has undefined behaviour, i.e. anything may happen. Compilers can emit warnings if you change string literals, e.g. gcc -Wwrite-strings 17

18 Recap We have seen The different C types primitive types (unsigned) char, short, int, long, long long, float... implicit conversions and explicit conversions (casts) between them arrays int[] pointers int * with the operations * and & C strings, as special char arrays Their representation How these representations can be broken, i.e. how we can inspect and manipulate the underlying representation (e.g. with casts) Some things that can go wrong e.g. due to access outside array bounds or integer under/overflow 18

### Pointers (continued), arrays and strings

Pointers (continued), arrays and strings 1 Last week We have seen pointers, e.g. of type char *p with the operators * and & These are tricky to understand, unless you draw pictures 2 Pointer arithmetic

### Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Hacking in C Pointers Radboud University, Nijmegen, The Netherlands Spring 2019 Allocation of multiple variables Consider the program main(){ char x; int i; short s; char y;... } What will the layout of

### Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1 C is quirky, flawed, and an enormous success. While accidents of history surely helped,

### Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Princeton University Computer Science 217: Introduction to Programming Systems Data Types in C 1 Goals of C Designers wanted C to: Support system programming Be low-level Be easy for people to handle But

### First of all, it is a variable, just like other variables you studied

Pointers: Basics 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 address (rather than the value)

### Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory

### Basic Types and Formatted I/O

Basic Types and Formatted I/O C Variables Names (1) Variable Names Names may contain letters, digits and underscores The first character must be a letter or an underscore. the underscore can be used but

### Arrays and Pointers in C. Alan L. Cox

Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including

### Memory, Arrays & Pointers

1 Memory, Arrays & Pointers Memory int main() { char c; int i,j; double x; c i j x 2 Arrays Defines a block of consecutive cells int main() { int i; int a[3]; i a[0] a[1] a[2] Arrays - the [ ] operator

### CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static

### A brief introduction to C programming for Java programmers

A brief introduction to C programming for Java programmers Sven Gestegård Robertz September 2017 There are many similarities between Java and C. The syntax in Java is basically

### 2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1 For Your Amusement C is quirky, flawed, and an enormous success. While accidents of history

### C - Basics, Bitwise Operator. Zhaoguo Wang

C - Basics, Bitwise Operator Zhaoguo Wang Java is the best language!!! NO! C is the best!!!! Languages C Java Python 1972 1995 2000 (2.0) Procedure Object oriented Procedure & object oriented Compiled

### VARIABLES AND TYPES CITS1001

VARIABLES AND TYPES CITS1001 Scope of this lecture Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Primitive types Every piece of data

### Limitations of the stack

The heap hic 1 Limitations of the stack int *table_of(int num, int len) { int table[len+1]; for (int i=0; i

### Section Notes - Week 1 (9/17)

Section Notes - Week 1 (9/17) Why do we need to learn bits and bitwise arithmetic? Since this class is about learning about how computers work. For most of the rest of the semester, you do not have to

### Aliasing restrictions of C11 formalized in Coq

Aliasing restrictions of C11 formalized in Coq Robbert Krebbers Radboud University Nijmegen December 11, 2013 @ CPP, Melbourne, Australia Aliasing Aliasing: multiple pointers referring to the same object

### C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

C++ Data Types Contents 1 Simple C++ Data Types 2 2 Quick Note About Representations 3 3 Numeric Types 4 3.1 Integers (whole numbers)............................................ 4 3.2 Decimal Numbers.................................................

### Memory, Data, & Addressing II CSE 351 Spring

Memory, Data, & Addressing II CSE 351 Spring 2018 http://xkcd.com/138/ Review Questions 1) If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) a) 64

### Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

### Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Declaration Fundamental Data Types All variables must be declared before being used. Tells compiler to set aside an appropriate amount of space in memory to hold a value. Enables the compiler to perform

### Programming refresher and intro to C programming

Applied mechatronics Programming refresher and intro to C programming Sven Gestegård Robertz sven.robertz@cs.lth.se Department of Computer Science, Lund University 2018 Outline 1 C programming intro 2

### C Introduction. Comparison w/ Java, Memory Model, and Pointers

CS 261 Fall 2018 Mike Lam, Professor C Introduction Comparison w/ Java, Memory Model, and Pointers Please go to socrative.com on your phone or laptop, choose student login and join room LAMJMU The C Language

### Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Overview of C Basic Data Types Constants Variables Identifiers Keywords Basic I/O NOTE: There are six classes of tokens: identifiers, keywords, constants, string literals, operators, and other separators.

### Pointers, 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

### CSE 307: Principles of Programming Languages

CSE 307: Principles of Programming Languages Variables and Constants R. Sekar 1 / 22 Topics 2 / 22 Variables and Constants Variables are stored in memory, whereas constants need not be. Value of variables

### Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of

### Heap Arrays. Steven R. Bagley

Heap Arrays Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index a[42] = 35; Variables and arrays have a type int, char, double,

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

Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Adapted from the slides Revisões sobre Programação em C, Sérgio Crisóstomo Compilation #include int main()

### Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions

### C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Department of Computer Science College of Engineering Boise State University September 11, 2017 1/21 Pointers A pointer is a variable that stores the address of another variable. Pointers are similar to

### CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java basics: Compilation vs Interpretation Program structure Statements Values Variables Types Operators and Expressions

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

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

### Lecture Notes on Types in C

Lecture Notes on Types in C 15-122: Principles of Imperative Computation Frank Pfenning, Rob Simmons Lecture 20 April 2, 2013 1 Introduction In lecture 18, we emphasized the things we lost by going to

### Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Announcements assign due tonight No late submissions Labs start this week Very helpful for assign1 Goals for Today Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic

### C 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,

### Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Last week Data can be allocated on the stack or on the heap (aka dynamic memory) Data on the stack is allocated automatically when we do a function call, and removed when we return f() {... int table[len];...

### Chapter 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

### Fundamental of Programming (C)

Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers

### Contents of Lecture 3

Contents of Lecture 3 Repetition of matrices double a[3][4]; double* b; double** c; Terminology Linkage Types Conversions Jonas Skeppstedt (js@cs.lth.se) Lecture 3 2014 1 / 33 A global matrix: double a[3][4]

### Heap Arrays and Linked Lists. Steven R. Bagley

Heap Arrays and Linked Lists Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index Variables and arrays have a type Create our

### Page 1. Today. Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right? Compiler requirements CPP Volatile

Last Time Today Compiler requirements CPP Volatile Advanced C What C programs mean int my_loop (int base) { int index, count = 0; for (index = base; index < (base+10); index++) count++; urn count; my_loop:

### EL2310 Scientific Programming

Lecture 11: Structures and Memory (yaseminb@kth.se) Overview Overview Lecture 11: Structures and Memory Structures Continued Memory Allocation Lecture 11: Structures and Memory Structures Continued Memory

### Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso Why C? Test on 21 Android Devices with 32-bits and 64-bits processors and different versions

### Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

hapter 1 INTRODUTION SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. Objectives You will learn: Java features. Java and its associated components. Features of a Java application and applet. Java data types. Java

### Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Chapter 2 (Dynamic variable (i.e. pointer), Static variable) August_04 A2. Identify and explain the error in the program below. [4] #include int *pptr; void fun1() { int num; num=25; pptr= &num;

### CS Programming In C

CS 24000 - Programming In C Week Two: Basic C Program Organization and Data Types Zhiyuan Li Department of Computer Science Purdue University, USA 2 int main() { } return 0; The Simplest C Program C programs

### CS 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,

### when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

I. Methods week 4 a method consists of a sequence of instructions that can access the internal data of an object (strict method) or to perform a task with input from arguments (static method) when you

### Agenda. 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

### Variables. Store information needed by the program

Variables Store information needed by the program Must have a TYPE int - can only store a number without a fractional part float, double - can store any number, with or without a fractional part (double

### 2 rd class Department of Programming. OOP with Java Programming

1. Structured Programming and Object-Oriented Programming During the 1970s and into the 80s, the primary software engineering methodology was structured programming. The structured programming approach

### false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

### Exercise: Using Numbers

Exercise: Using Numbers Problem: You are a spy going into an evil party to find the super-secret code phrase (made up of letters and spaces), which you will immediately send via text message to your team

### LAB A Translating Data to Binary

LAB A Translating Data to Binary Create a directory for this lab and perform in it the following groups of tasks: LabA1.java 1. Write the Java app LabA1 that takes an int via a command-line argument args[0]

### Principles of C and Memory Management

COMP281 Lecture 8 Principles of C and Memory Management Dr Lei Shi Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic, Functions Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic,

### CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

### Pierce Ch. 3, 8, 11, 15. Type Systems

Pierce Ch. 3, 8, 11, 15 Type Systems Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using

### C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

C-Programming CSC209: Software Tools and Systems Programming Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Adapted from Dan Zingaro s 2015 slides. Week 2.0 1 / 19 What

### Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a

### Topic 9: Type Checking

Recommended Exercises and Readings Topic 9: Type Checking From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6 and

### Topic 9: Type Checking

Topic 9: Type Checking 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6

### Java and C CSE 351 Spring

Java and C CSE 351 Spring 2018 https://xkcd.com/801/ Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: get_mpg: pushq

### Important From Last Time

Important From Last Time Embedded C Pros and cons Macros and how to avoid them Intrinsics Interrupt syntax Inline assembly Today Advanced C What C programs mean How to create C programs that mean nothing

### LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

LESSON 5 ARITHMETIC DATA PROCESSING The arithmetic data types are the fundamental data types of the C language. They are called "arithmetic" because operations such as addition and multiplication can be

### Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Important From Last Time Today Embedded C Pros and cons Macros and how to avoid them Intrinsics Interrupt syntax Inline assembly Advanced C What C programs mean How to create C programs that mean nothing

### THE FUNDAMENTAL DATA TYPES

THE FUNDAMENTAL DATA TYPES Declarations, Expressions, and Assignments Variables and constants are the objects that a prog. manipulates. All variables must be declared before they can be used. #include

### Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That 1. Introduction You have seen situations in which the way numbers are stored in a computer affects a program. For example, in the

### Hacking in C. The C programming language. Radboud University, Nijmegen, The Netherlands. Spring 2018

Hacking in C The C programming language Radboud University, Nijmegen, The Netherlands Spring 2018 The C programming language Invented by Dennis Ritchie in the early 70s First Hello World program written

### false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

### CSI33 Data Structures

Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2018 Outline Outline 1 Chapter 8: A C++ Introduction For Python Programmers Expressions and Operator Precedence

### CSC 1107: Structured Programming

CSC 1107: Structured Programming J. Kizito Makerere University e-mail: www: materials: e-learning environment: office: alt. office: jkizito@cis.mak.ac.ug http://serval.ug/~jona http://serval.ug/~jona/materials/csc1107

### Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Tester vs. Controller Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG For effective illustrations, code examples will mostly be written in the form of a tester

### In Java we have the keyword null, which is the value of an uninitialized reference type

+ More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

### Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Memory and Addresses Memory is just a sequence of byte-sized storage devices. 1 The bytes are assigned numeric addresses, starting with zero, just like the indexing of the cells of an array. It is the

### Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

### Important From Last Time

Important From Last Time Embedded C Ø Pros and cons Macros and how to avoid them Intrinsics Interrupt syntax Inline assembly Today Advanced C What C programs mean How to create C programs that mean nothing

### Elementary Programming

Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal

### Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will

### Lecture 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

### ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

ECEN 449 Microprocessor System Design Review of C Programming 1 Objectives of this Lecture Unit Review C programming basics Refresh programming skills 2 Basic C program structure # include main()

### PRINCIPLES OF OPERATING SYSTEMS

PRINCIPLES OF OPERATING SYSTEMS Tutorial-1&2: C Review CPSC 457, Spring 2015 May 20-21, 2015 Department of Computer Science, University of Calgary Connecting to your VM Open a terminal (in your linux machine)

### CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays 1 What are Arrays? Arrays are our first example of structured data. Think of a book with pages numbered 1,2,...,400.

### CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Objectives: CPS122 Lecture: From Python to Java last revised January 4, 2017 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

### Programming in C and C++

Programming in C and C++ Types, Variables, Expressions and Statements Neel Krishnaswami and Alan Mycroft Course Structure Basics of C: Types, variables, expressions and statements Functions, compilation

### Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Entry Point of Execution: the main Method Elementary Programming EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG For now, all your programming exercises will be defined within the

### false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

### Full file at

Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

### Pointers, Arrays, and Strings. CS449 Spring 2016

Pointers, Arrays, and Strings CS449 Spring 2016 Pointers Pointers are important. Pointers are fun! Pointers Every variable in your program has a memory location. This location can be accessed using & operator.

### Gabriel 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

### Introduction. Programming in C++ Pointers and arrays. Pointers in C and C++ Session 5 - Pointers and Arrays Iterators

Session 5 - Pointers and Arrays Iterators Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Introduction Pointers and arrays: vestiges

### Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c And in Conclusion, 2 Processor Control Datapath Components of a Computer PC Registers Arithmetic

### 1d: tests knowing about bitwise fields and union/struct differences.

Question 1 1a: char ptr[] = Hello World ; char a = ptr[1], b = *(ptr+6); Creates an array of 12 elements, 11 visible letters and a character value 0 at the end. i true ii true iii false iv false v true

### Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 2 slide

### CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

### Week 3 Lecture 2. Types Constants and Variables

Lecture 2 Types Constants and Variables Types Computers store bits: strings of 0s and 1s Types define how bits are interpreted They can be integers (whole numbers): 1, 2, 3 They can be characters 'a',

### CS 61C: Great Ideas in Computer Architecture Introduction to C

CS 61C: Great Ideas in Computer Architecture Introduction to C Instructors: Vladimir Stojanovic & Nicholas Weaver http://inst.eecs.berkeley.edu/~cs61c/ 1 Agenda C vs. Java vs. Python Quick Start Introduction

### Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #33 Pointer Arithmetic In this video let me, so some cool stuff which is pointer arithmetic which helps you to