1 Variables, Pointers, and Arrays Professor Jennifer Rexford COS 217

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Programming and Data Structure
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
Kernighan/Ritchie: Kelley/Pohl:
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.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Introduction to C Programming CE
0 Chap. 5 Pointers and Arrays 5.1Pointers and Adresses 5.2Pointers and Function Arguments 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Pointers CSE 2451 Rong Shi.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
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”
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
CISC220 Spring 2010 James Atlas Lecture 04: Pointers, Functions, Memory Management, ADTs, Classes, Hardware, Software, Graphics, Networking, AI, Databases,
Computer Organization and Design Pointers, Arrays and Strings in C
Computer Skills2 for Scientific Colleges
INC 161 , CPE 100 Computer Programming
Lecture 6 C++ Programming
Computer Skills2 for Scientific Colleges
Chapter 16 Pointers and Arrays
Initializing variables
C++ Pointers and Strings
Variables, Pointers, and Arrays
CS1100 Computational Engineering
CSCE 206 Lab Structured Programming in C
Arrays and Pointers CSE 2031 Fall May 2019.
C++ Pointers and Strings
Arrays and Pointers CSE 2031 Fall July 2019.
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

1 Variables, Pointers, and Arrays Professor Jennifer Rexford COS 217

2 Overview of Today’s Lecture Pointers  Differences between value, variable, and pointer  Using pointers to do “call-by-reference” in C Arrays  List of elements of the same type  Relationship between arrays and pointers  Example program to reverse an array Strings  Array of characters ending in ‘\0’ Struct  Multiple values grouped together  Dereferencing to access individual elements

3 Values, Variables, and Pointers ‘s’ ‘M’ ‘o’ ‘t’ ‘h’ ‘y’ memory x p&x Value  E.g., ‘M’ Variable  A named box that holds a value  E.g., char x = ‘M’; Pointer value  Address of the box  E.g., &x Pointer variable  A box holding the address of the box  E.g., char* p = &x;

4 Example Program Output  Value of x is M  Address of x is  Address of p is #include int main(void) { char x = ‘M’; char* p = &x; printf(“Value of x is %c\n”, x); printf(“Address of x is %u\n”, p); printf(“Address of p is %u\n,” &p); return 0; } What is this? *p

5 Values vs. Variables int n; n = 217; n = n + 9; 3 = n; &n a pointer value &3 217 n ? n 226 n ?? What is this? *(&n)

6 Call by Value is Not Enough Function parameters are transmitted by value  Values copied into “local variables” void swap(int x, int y) { int t; t = x; x = y; y = t; } int main(void) {... swap(a,b);... } No! 7 3 x y 7 3 a b 3 7 x y 7 3 a b

7 Call by Reference Using Pointers Use pointers to pass variables “by reference” void swap(int *x, int *y) { int t; t = *x; *x = *y; *y = t; } int main(void) {... swap(&a,&b);... } Yes x y 7 3 a b x y 3 7 a b

8 Arrays in C int a[5]; a a is a value of type “pointer to int” What is “ a ” in the picture above? a is the pointer constant, not the five consecutive memory locations!

9 Arrays and Pointers int a[5]; int *p; a p p = a; a is a value of type “pointer to int” (int *) p is a variable of type “pointer to int” (int *) OK: p = a; if (a == p)...; a[i] = p[j]; Wrong: a = p; 3 = i;

10 C Does Not Do Bounds Checking! int a[5]; a[0] = 217; a[3] = 226; a a[-1] = 55; a[7] = 320; Unpleasant if you happened to have another variable before the array variable a, or after it!

11 Arrays and Pointers int a[5]; int *p, *q; a q p[1]= 44; 44 p p = a; q = p + 2; q[-1] = 43; q[2] = 46;

12 Pointer Arithmetic int a[5]; a p Subscript: a[i] “means” *(a+i) int *p; p = a + 2; Note: arithmetic scales by data size (e.g., int of 4 bytes) 4 bytes

13 Quaint usage of pointer arithmetic Add up the elements of an array: int a[100]; int sum, *p;... for (p=a; p<a+100; p++) sum += *p; More straightforwardly: int a[100]; int sum, i;... for (i=0; i<100; i++) sum += a[i];

14 Array Parameters to Functions void printArray(int *p, int n) { int i; for (i=0; i<n; i++) printf(“%d\n”,p[i]); } int fib[5] = {1, 1, 2, 3, 5}; int main(void) { printArray(fib, 5); }

15 Array Params  Pointer Params void printArray(int *p, int n) {... } void printArray(int p[5], int n) {... } void printArray(int p[ ], int n) {... } void printArray(int p[1000], int n) {... } int main(void) { printArray(fib, 5); } All these declarations are equivalent!

16 Example Program: Reverse Array Reverse the values in an array  Inputs: integer array a, and number of elements n  Output: values of a stored in reverse order Algorithm  Swap the first and last elements in the array  Swap the second and second-to-last elements ……

17 Example of Array by Reference void reverse (int a[], int n) { int l, r, temp; for (l=0, r=n-1; l<r; l++, r--) { temp = a[l]; a[l] = a[r]; a[r] = temp; } int main(void) { reverse(fib, 5); }

18 Strings A string is just an array of characters (pointer to character), terminated by a ‘\0’ char (a null, ASCII code 0). char mystring[6] = {’H’,’e’,’l’,’l’,’o’,’\0’}; char mystring[6] = “Hello”; char mystring[] = “Hello”; char *yourstring = “Hello”; Different Equivalent Hello\0 mystring yourstring Hello\0

19 Char Array and Pointer Manipulation char mystring[] = “Hello”; char *yourstring = “Hello”; Hello\0 mystring yourstring Hello\0 mystring[0] = ‘J’; yourstring[0] = ‘C’; J C yourstring = mystring; y yourstring[4] = ‘y’; mystring = yourstring;

20 Printing a String printf(“%s”,mystring); int i; for (i=0; mystring[i]; i++) putchar(mystring[i]); or, char *p; for (p=mystring; *p; p++) putchar(*p); Hello\0 mystring

21 String Termination char mystring[] = “Hello”; Hello\0 mystring \0 mystring[2] = 0; equivalently, mystring[2]=’\0’; printf(“%s\n”,mystring); He mystring[2] = ‘x’; mystring[5] = ‘!’; printf(“%s\n”,mystring); What will happen? !x

22 Computing the Length of a String int strlen(char* s) { char* p = s; while (*p) p++; return (p – s); } Hello\0 mystring sp

23 Boxes and Arrows In designing and analyzing your data structures, draw pictures! Example: you want an array of strings NULL zero\0 one two char *query[4] = {”zero”,”one”,”two”,NULL}; how to parse it: *(query[4]) postfix operators bind tighter than prefix; whenever you’re not sure, just put the parentheses in

24 Structures A struct value is a bunch of values glued together struct pair { int number; char grade; }; A struct variable is a box holding a struct value struct pair x; x.number = 217; x.grade = ’A’; 217 A A x

25 Pointers to structs struct pair {int number; char grade;}; struct pair x; x.number=217; x.grade=’A’; 217 A x int n = (*p).number; char g = (*p).grade; A 217 n g struct pair *p; p = &x; p

26 Dereferencing Fields struct pair {int number; char grade;} *p; 217 A p *p 217 A int n = (*p).number; char g = (*p).grade; A 217 n g int n = p->number; char g = p->grade; Easier-to-use notation

27 Summary of Today’s Class C variables  Pointer  Array  String  Struct Readings  See Course Schedule on Web page!