Variables, Pointers, and Arrays

Slides:



Advertisements
Similar presentations
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.
Advertisements

Kernighan/Ritchie: Kelley/Pohl:
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.
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.
1 Variables, Pointers, and Arrays Professor Jennifer Rexford COS 217
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.
Pointers CSE 2451 Rong Shi.
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!
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.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
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.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
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.
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.
Lecture 5 Pointers 1. Variable, memory location, address, value
Intro to Pointers in C CSSE 332 Operating Systems
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
Computer Skills2 for Scientific Colleges
CSE 220 – C Programming Pointers.
C Programming Tutorial – Part I
Lecture-5 Arrays.
CSE 303 Concepts and Tools for Software Development
CNG 140 C Programming (Lecture set 10)
INC 161 , CPE 100 Computer Programming
Programming Languages and Paradigms
Lecture 6 C++ Programming
CSC 253 Lecture 8.
CSC 253 Lecture 8.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Lecture 18 Arrays and Pointer Arithmetic
Programming in C Pointer Basics.
Computer Skills2 for Scientific Colleges
Chapter 16 Pointers and Arrays
Simulating Reference Parameters in C
Initializing variables
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
C++ Pointers and Strings
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
CS150 Introduction to Computer Science 1
EECE.2160 ECE Application Programming
Chapter 9: Pointers and String
Programming in C Pointers and Arrays.
Pointers and dynamic objects
CS1100 Computational Engineering
CS31 Discussion 1H Fall18: week 6
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:

Variables, Pointers, and Arrays CS 217 http://www.cs.princeton.edu/courses/archive/fall05/cos217/

Overview of Today’s Lecture Pointers Differences between value, variable, and pointer Using pointers to do call-by-reference in C Struct Multiple values grouped together Dereferencing to access individual elements 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’

Values, Variables, and Pointers 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; memory ‘s’ ‘M’ x ‘o’ ‘o’ ‘t’ ‘h’ p &x ‘y’

Example Program Output #include <stdio.h> main() { 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); } Output Value of x is M Address of x is 4290770463 Address of p is 4290770456

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

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; } main() { ... swap(a,b); No! 7 3 x y 3 7 x y 7 3 a b 7 3 a b

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; } main() { ... swap(&a,&b); x y x y Yes 7 3 a b 3 7 a b

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 x 217 A

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

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

Arrays in C int a[5]; a 217 226 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!

Arrays and Pointers int a[5]; a 217 int *p; p 226 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;

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

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

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

Quaint usage of pointer arithmetic Add up the elements of an array: #define N 1000 int addup(int a[N]) { int sum, *p; for (p=a; p<a+N; p++) sum += *p; return sum; } More straighforwardly: int addup(int a[N]) { int sum, i; for (i=0; i<N; i++) sum += a[i]; return sum; }

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(...) { printArray(fib, 5);

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(...) { printArray(fib, 5); } All these declarations are equivalent!

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 … 77 31 94 5 186

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(...) { reverse(fib, 5);

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”; Equivalent H e l o \0 mystring yourstring • Different

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

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); mystring H e l o \0

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

Boxes and Arrows In designing and analyzing your data structures, draw pictures! Example: you want an array of strings z e r o \0 • • o n e \0 • t w o \0 NULL 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

Summary of Today’s Class C variables Pointer Struct Array String First programming assignment (due Sun Oct 2 at 8:59pm) Use of pointers is optional Using a global variable instead is okay Reading for next week Chapters 2, 3, and 4 of The C Programming Language Chapter 4 of Practice of Programming