Basic Pointers. CSCI 112: Programming in C

Similar documents
Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Computer Programming

Writing Functions in C

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Goals of this Lecture

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

Object oriented programming C++

Pointers. Memory. void foo() { }//return

SYSC 2006 C Winter 2012

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

EL2310 Scientific Programming

Dynamic Data Structures. CSCI 112: Programming in C

Personal SE. Computer Memory Addresses C Pointers

Lecture 04 Introduction to pointers

Introduction to Computer Science Midterm 3 Fall, Points

CA31-1K DIS. Pointers. TA: You Lu

(7-1) Modular Programming H&K Chapter 6. Instructor - Andrew S. O Fallon CptS 121 (February 21, 2018) Washington State University

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

[0569] p 0318 garbage

Reference operator (&)

Pointers, Dynamic Data, and Reference Types

DECLARAING AND INITIALIZING POINTERS

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

CSCI E-119 Section Notes. Section 5 Solutions

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

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

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

What is an algorithm?

a data type is Types

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Language comparison. C has pointers. Java has references. C++ has pointers and references

More on C programming

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Dynamic Memory Allocation

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

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

Chapter 9. Pointers and Dynamic Arrays

Pointers and scanf() Steven R. Bagley

CS 2461: Computer Architecture I

Pointer. Pointer variable

Variables, Memory and Pointers

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

Implementing Abstractions

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Variation of Pointers

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Pointers. Pointer References

Pointer Data Type and Pointer Variables

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Functions. Cedric Saule

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

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

pointers + memory double x; string a; int x; main overhead int y; main overhead

REFERENCES, POINTERS AND STRUCTS

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

Pointers and Terminal Control


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

Intro to C: Pointers and Arrays

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Introduction to Computers and Programming

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

Lecture 16 Introduction To Structures

CS349/SE382 A1 C Programming Tutorial

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Recitation 2/18/2012

Arrays, Strings, & Pointers

Pointers. Reference operator (&) ted = &andy;

Computer Systems C S Cynthia Lee

QUIZ How do we implement run-time constants and. compile-time constants inside classes?


Computers Programming Course 6. Iulian Năstac

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

CS113: Lecture 4. Topics: Functions. Function Activation Records

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Constants, References

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

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Pointers II. Class 31

Programming Abstractions

CSCI 171 Chapter Outlines

EECE.2160: ECE Application Programming Fall 2017 Exam 3 December 16, 2017

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Chapter 1 Getting Started

Lab 3. Pointers Programming Lab (Using C) XU Silei

Arrays and Pointers (part 1)

Pointers. Mr. Ovass Shafi (Assistant Professor) Department of Computer Applications

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

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

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Transcription:

Basic Pointers CSCI 112: Programming in C

What the #$@# is a pointer? Pointers are variables Instead of holding values, they hold the address of another variable. They point to where that other variable is stored.

Some new syntax A pointer variable is declared using a * A pointer variable points to specific type intpointer, char pointer, etc. A pointer to an int: int *intpointer; A pointer to a char: char *charpointer;

What does a pointer do? A pointer variable holds an address of another variable Ex: an int* holds address of an int We don t assign a value to a pointer we assign it an address!

What does a pointer do? A pointer variable holds an address of another variable Ex: an int* holds address of an int We don t assign a value to a pointer we assign it an address! Remember the address of operator from scanf? (&) int myint; int *myintpointer; // Declare an int variable // Declare an int pointer variable myintpointer = &myint; // Point "myintpointer" to the memory location of // "myint"

0x01 0x02 0xAE 0xAF We start out with a bunch of unallocated memory cells. The hexadecimal numbers above each cell represent the address.

0x01 0x02 0xAE 0xAF x int x; Declare an int variable, and allocate a space in memory for it.

0x01 0x02 0xAE 0xAF 34 x int x; x = 34; Assign 34 to x. The value 34 goes into the memory cell for x.

0x01 0x02 0xAE 0xAF 34 x *point Declare an int pointer called point. Allocate space in memory for this variable. int x; x = 34; int *point;

0x01 0x02 0xAE 0xAF 34 0x01 x *point Assign the address of x to point. int x; x = 34; int *point; point = &x; We can now think of *point as being a link to x

0x01 0x02 0xAE 0xAF 34 0x01 x *point Assign the address of x to point. int x; x = 34; int *point; point = &x; We can now think of *point as being a link to x *point is pointing to where x is stored!

Pointer operations: the * symbol There are three uses for it: Multiplication int x = 6 * 7; Indicating that a variable is of a pointer type int *p = &x; Unary: follows the pointer (often called dereferencing ) int z = *p;

Working with pointers // 1 int x = 34; int *point = &x; // 2 int *point2 = point; // 3 int y = *point; In (1), we assign a value to an int variable x, then point the variable point to the address of x.

Working with pointers // 1 int x = 34; int *point = &x; // 2 int *point2 = point; // 3 int y = *point; In (1), we assign a value to an int variable x, then point the variable point to the address of x. Quick tip: to print the value of a pointer variable, use the %p format specifier!

Working with pointers // 1 int x = 34; int *point = &x; // 2 int *point2 = point; // 3 int y = *point; In (2), we create another int pointer, point2, and set it equal to point. This means *point and *point2 both point to the same place x!

A picture is worth 436 words 0x01 0x02 0xAE 0xAF 34 0x01 x *point2 *point point and point2 both reference the same address/variable (x) int x; x = 34; int *point; point = &x; int *point2; point2 = point;

Working with pointers // 1 int x = 34; int *point = &x; // 2 int *point2 = point; // 3 int y = *point; In (3), we declare a new int variable y, and assign the value pointed to by point to it.

Wait, what? A pointer variable itself only holds an address (of the variable it points to) But using the unary operator (*) we can follow the pointer and retrieve the value of the variable being pointed to! int y = *point; So if point is pointing to x, the above snippet is actually assigning the value of x to y (which is of the int type)

A picture is worth 436 words 0x01 0x02 0xAE 0xAF 34 0x01 x y *point point is an int pointer to x We declare the int variable y, and space is allocated in memory for it. int x; x = 34; int *point; point = &x; int y;

A picture is worth 436 words 0x01 0x02 0xAE 0xAF 34 34 0x01 x y *point We dereference point, and assign its value to y. END RESULT: y == x int x; x = 34; int *point; point = &x; int y; y = *point;

Dereferencing works for assignment, too int x; int *mypointer; // This next line assigns 34 to the variable mypointer points to // Equivalent to writing "x = 34;" *mypointer = 34;

Use case: output parameters We re used to functions returning a value int x = my_function();

Use case: output parameters We re used to functions returning a value int x = my_function(); We can also pass a pointer as an argument to function, and the function can write the result into that variable: void increment_int(int *value) { *value = *value + 1; } int x = 6; increment_int(x); // x now equals 7