Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

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

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

Introduction to C. Systems Programming Concepts

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

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

Arrays and Pointers in C. Alan L. Cox

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

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Variation of Pointers

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

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

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

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

Pointers in C/C++ 1 Memory Addresses 2

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Algorithms & Data Structures

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

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

Dynamic Memory Allocation

CS 61c: Great Ideas in Computer Architecture

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Programming. Data Structure

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

CS61C : Machine Structures

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

CS61C : Machine Structures

Fundamental of Programming (C)

[0569] p 0318 garbage

ECET 264 C Programming Language with Applications

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

Memory, Data, & Addressing II CSE 351 Spring

ECET 264 C Programming Language with Applications

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Homework #3 CS2255 Fall 2012

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

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

FUNCTIONS POINTERS. Pointers. Functions

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

HW1 due Monday by 9:30am Assignment online, submission details to come

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Intermediate Programming, Spring 2017*

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

DECLARAING AND INITIALIZING POINTERS

Lecture 04 Introduction to pointers

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

INTRODUCTION TO COMPUTER SCIENCE - LAB

Programming, numerics and optimization

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

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

Dynamic memory allocation (malloc)

ISA 563 : Fundamentals of Systems Programming

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Introduction to C/C++ Programming

More about BOOLEAN issues

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Principles of C and Memory Management

ECE 15B COMPUTER ORGANIZATION

CS61C : Machine Structures

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Pointers. Introduction

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

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

C++ for Java Programmers

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Dynamic Data Structures. CSCI 112: Programming in C

Lecture 8 Dynamic Memory Allocation

Arrays and Pointers (part 1)

Content. In this chapter, you will learn:

Intro to C: Pointers and Arrays

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

C Arrays, Strings, More Pointers Instructor: Steven Ho

CS61C : Machine Structures

CS61C : Machine Structures

C Pointers Pearson Education, Inc. All rights reserved.

Data Representation and Storage. Some definitions (in C)

Memory, Data, & Addressing II

Topics Introduction to Microprocessors

C - Func(on, Pointer, Array. Zhaoguo Wang

THE FUNDAMENTAL DATA TYPES

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

CS 222: Pointers and Manual Memory Management

Heap Arrays and Linked Lists. Steven R. Bagley

Introduction to Computers and Programming

CS 2461: Computer Architecture I

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

fopen() fclose() fgetc() fputc() fread() fwrite()

CS Programming In C

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

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

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

CSC 1600 Memory Layout for Unix Processes"

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

Transcription:

Incoming Exam CS 0 Introduction to Pointers Debzani Deb Next Monday (th March), we will have Exam # Closed book Sit with an empty space in either side of you Calculators that have text-allowing is not permitted Covers Chapter - (Lecture Materials, Readings, Quiz, Linux basics, Makefile, Eclipse, Pointer & Labs) ou should be able to answer correctly 80-8% of the exam, if you get hold of the above materials The rest - 0% will be challenging Email me/ta any question you have or see us at our office times Don t just wait for the last minute What is a Pointer? A pointer is a variable which points to a place in computer s It contains address as its value ( POINTER = ADDRESS) Pointer variable: a variable dedicated to hold addresses A variable name directly references a value (ie content of a cell) A pointer indirectly references a value Referencing a value through a pointer is called indirection 00 content Address 08 07 08 Pointers and Addresses Memory can be conceptualized as a linear sequence of addressable locations Variable reference the content of a location Pointers have a value of the address of a location Addr Addr Addr Addr Addr Addr Addr Addr Content Content Content Content Size of Variable Types High Speed Memory (RAM) Everything in a computer program must fit into high speed High speed is simply a string of bytes Lots and Lots and Lots of bytes, but just that bytes Some number of bytes are allocated every time you define a variable 0778 0 Gb of RAM

So What Is The Size Of Different Types? If you ran a short program with a bunch of these statements printf( sizeof(int) = %d\n, sizeof(int)); sizeof(int) = sizeof(long) = sizeof(long int) = sizeof(long long int) = 8 sizeof(char) = sizeof(float) = sizeof(double) = 8 sizeof(long double) = So What Is The Size Of Different Types? Depends on hardware, brand of OS and compiler Here is Visual Studio Net on Windows p Professional (Differences are marked in red) sizeof(int) = sizeof(long) = sizeof(long int) = (long long illegal on Winp C++Net) sizeof(char) = sizeof(float) = sizeof(double) = 8 sizeof(long double) = 8 Now, on to pointers! 7 8 Again What is Pointer? A variable containing the address of another variable (Hence it points to the other location) There are things and pointers to things 0FAB8 int x=,y=,z=; thing_ptr thing 0FAB0 0FAB name address Reference thing thing_ptr *thing_ptr Explanation Direct value of the variable thing Direct value of variable thing_ptr Indirect value of thing_ptr Value Pointer to location containing 0FABC Address shown in hex 9 0 0FAB8 0FAB0 0FAB 0FABC int x=,y=,z=; 0FAB8 0FAB0 0FAB 0FABC int x=,y=,z=; Note, I have shown the value of the variable in, not the actual representation!

0FAB8 0FAB0 0FAB 0FABC int x=,y=,z=; name address 0FAB8 0FAB0 0FAB 0FABC int x=,y=,z=; 0FAB So, what is the size of a pointer? sizeof(int *) = sizeof(long long int *) = sizeof(float *) = sizeof(double *) = sizeof (char * ) = Pointers to different types are all the same size Once again, their size may be dependent on your hardware, OS, or even compiler Now, back to my diagram 0FAB8 0FAB0 0FAB 0FABC int x=,y=,z=; 0FAB 0FAB8 0FAB0 0FAB 0FABC int x=,y=,z=; printf( %d\n,x); 0FAB 0FAB8 0FAB0 0FAB 0FABC int x=,y=,z=; printf( %d\n,x); 0FAB 7 8

0FAB8 0FAB0 0FAB 0FABC int x=,y=,z=; printf( %d\n,x); printf( %d\n,*); 0FAB 0FAB8 0FAB0 0FAB 0FABC int x,y,z; int * printf( %d\n,x); printf( %d\n,*); 0FAB 9 0 Pointer Declaration Operators Related To Pointers int * p; // pointer is declared p is a pointer pointing at an variable of type int * tells the compiler that the variable named p is to be a pointer The name of the pointer variable is p (not *p) The size of the addressed by a pointer depends on the type of the object mentioned in declaration int * p; // p points at consecutive bytes double * p; // p points at 8 consecutive bytes & address-of printf( %p\n,&variable); * dereference printf( %d\n, * pointer_variable) int x = ; int * p; p = &x; Address of Operator (&) // Variable defined // Pointer declared // Pointer p is set up to point at // the variable x Address of variable x is assigned to pointer p &is an unary address operator which returns the address of variable &xis read as address of x About Asterisk (*) In declaration int * p * tells the compiler that a new variable is to be a pointer (read as pointer to ) In Regular use It provides the content of the location specified by a pointer It mean follow the pointer x = * p It can also stand on the left side of an assignment * p = 0

Typical Uses Of Operators int thing; /* declare an integer (a thing) */ thing = ; int * thing_ptr; /* declare a pointer to an integer */ Don t need _ptr, just helps readability At this moment, it is pointing to a random values since it has not been initialized yet Such garbage can be harmful thing_ptr = NULL; thing_ptr is a null pointer means pointing to nothing thing_ptr = &thing; /* store address in pointer, points to thing */ Cont *thing_ptr = ; /* set thing to */ *thing_ptr is a thing thing_ptr is a pointer Of course, thing_ptr could be set to point to other variables besides thing thing_ptr = &something_else; Illegal and Strange Uses *thing is illegal thing is not a pointer variable &thing_ptr is legal, but strange a pointer to a pointer Pointing to the Same Thing int something; int *first_ptr; /* one pointer */ int *second_ptr; /* another pointer */ something=; first_ptr = &something; second_ptr = first_ptr; something=; *first_ptr=; *second_ptr=; All identical results (Only one integer) 7 8 p = &x; // p points at x now Again uses of & and * 00 07 00 00 y = * p; 087 // y has the value pointed out by the pointer p * p= ; // was inserted to the // place pointed out by p y int 00 07 00 00 9