Binghamton University. CS-211 Fall Pointers

Similar documents
Binghamton University. CS-211 Fall Pointers

Binghamton University. CS-211 Fall Pointers vs. Arrays

Binghamton University. CS-211 Fall Pointers vs. Arrays

CMSC202 Computer Science II for Majors

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

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

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

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

Course organization. Course introduction ( Week 1)

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

a data type is Types

Homework #3 CS2255 Fall 2012

[0569] p 0318 garbage

MYcsvtu Notes LECTURE 34. POINTERS

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

Tutorial 1: C-Language

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

Programming in C. Pointers and Arrays

Memory, Data, & Addressing II CSE 351 Spring

Arrays and Pointers in C. Alan L. Cox

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

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

Variation of Pointers

Syntax and Variables

Lecture 05 POINTERS 1

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

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

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

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

FUNCTIONS POINTERS. Pointers. Functions

CS31 Discussion 1E Spring 17 : week 08

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Procedural Programming

Kurt Schmidt. October 30, 2018

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:

Dynamic Memory Allocation and Command-line Arguments

Pointers. 10/5/07 Pointers 1

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

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

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

ELEC 377 C Programming Tutorial. ELEC Operating Systems

DECLARAING AND INITIALIZING POINTERS

Object oriented programming C++

Programming in C - Part 2

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CS C Primer. Tyler Szepesi. January 16, 2013

Introduction to Programming Using Java (98-388)

EL2310 Scientific Programming

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

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Intermediate Programming, Spring 2017*

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

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

EL2310 Scientific Programming

Reference operator (&)

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

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

Overview (1A) Young Won Lim 9/14/17

C Arrays and Pointers

C introduction: part 1

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

C Pointers. 7.2 Pointer Variable Definitions and Initialization

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Overview (1A) Young Won Lim 9/9/17

CS 161 Exam II Winter 2018 FORM 1

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

C Pointers. 6th April 2017 Giulio Picierro

Exam 3 Chapters 7 & 9

Overview (1A) Young Won Lim 9/25/17

What have we learned about when we learned about function parameters? 1-1

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

Arrays, Strings, & Pointers

Introduction to Scientific Computing and Problem Solving

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

CS 231 Data Structures and Algorithms, Fall 2016

Object-Oriented Programming

Important From Last Time

Pointers. Pointer References

Systems Programming and Computer Architecture ( )

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Pointers in C/C++ 1 Memory Addresses 2

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

CS 241 Data Organization Pointers and Arrays

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

Important From Last Time

Pointers and scanf() Steven R. Bagley

CSE413 Midterm. Question Max Points Total 100

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

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):

CS 107 Lecture 2: Bits and Bytes (continued)

Pointers (1A) Young Won Lim 3/5/18

Professor Terje Haukaas University of British Columbia, Vancouver C++ Programming

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Transcription:

Pointers 1

What is a pointer? Says I m not important what s important is over there Points AT or TO something else 2

Memory Array of bytes Each element has a value 0 1 2 3 xffff fffd xffff fffe xffff ffff x00 x04 x1c x02 x96 x00 x30 x04 3

C Variables A variable is a named piece of data Variables in C have A name (specified by the programmer) A value (may be unassigned/unknown) A location in memory (determined by the compiler) A type (size and interpretation) (more to come scope/ storage class/ etc.) Variables must be declared before they are used! 4

Variable Concept Age Memory???????????????????????????????????????????????????????????????????????? First_Initial gpa 5

Variables In Memory Every variable starts at a specific location in memory Type of variable tells how many bytes (spaces) in memory First_Initial age 0 1 2 3 x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff ffff x00 x04 x1c x02 x54 x00 x00 x00 x39 x96 x00 x30 x04 6

Variable Address/Location Where is the value for the variable in memory? The address of First_Initial is x0034 ffe0 First_Initial age 0 1 2 3 x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff ffff x00 x04 x1c x02 x54 x00 x00 x00 x39 x96 x00 x30 x04 7

Address Of (&) operator An ampersand (&) in front of a variable indicates address of char First_Initial= T ; int age=57; printf( First_Initial is in memory at %p\n,&first_initial); First_Initial is in memory at 0x34ffe0 First_Initial age x00 x04 x1c x02 x54 x00 x00 x00 x39 x96 x00 x30 x04 0 1 8 2 3 x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff ffff

Pointers in C Pointers are a special data type The VALUE of a pointer is an address The TYPE of a pointer is pointer to <target_type> pointer to character pointer to integer pointer to float pointer to array of integers 9

Declaring a Pointer Same as normal variable but need asterisk (*) : pointer to char First_Initial= T ; char * fi_ptr=&first_initial; // pointer to char printf( Value of fi_ptr is %p\n,fi_ptr); Value of fi_ptr is 0x34ffe0 First_Initial fi_ptr x00 x04 x1c x02 x54 x00 x34 xff xe0 x96 x00 x30 x04 0 1 10 2 3 x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff ffff

Pointers as References A pointer has a value an address in memory A pointer points to another value the data at that address fi_ptr First_Initial 0x0034ffe0 x54 11

Using a Pointer Same as normal variable but need asterisk (*) : value at char First_Initial= T ; char * fi_ptr=&first_initial; // pointer to char printf( fi_ptr points at %c\n,(*fi_ptr)); fi_ptr points at T First_Initial fi_ptr x00 x04 x1c x02 x54 x00 x34 xff xe0 x96 x00 x30 x04 0 1 12 2 3 x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff ffff

Pointers as Aliases char First_Initial= T ; char * fi_ptr=&first_initial; // pointer to char (*fi_ptr)= A ; printf( First Initial: %c\n,first_initial); First_Initial *fi_ptr First Initial: A x54 13

Abuse of Symbols Ampersand (&) x & y // Bit-wise AND x && y // Logical AND &x // Address Of Asterix (*) x * y // multiplication int * x // pointer to (*x) // value at 14

Using NULL NULL is a special name whose value is 0x0000 0000. Beginning of Memory belongs to the operating system General programs can read at 0, but cannot write at 0 Therefore, we use NULL to indicate pointer to nothing Or pointer that we haven t set yet int *p=null; // p is a pointer to nothing (for now) p=&age; // Now p is a pointer to an integer 15

C Gotcha: Dereferencing a Null Pointer int *p=null; // p is a pointer to nothing (for now) int x=foo(); if (x>0) { p=&x; } (*p) = 5; Segmentation Violation when x<=0

Lab 3 Prog1- Dereference NULL while (i <= argc) { printf("argument %d is : %s\n",i, argv[i++]); } int p1=atoi(argv[1]); $./prog1 Argument 0 is : prog1 Argument 1 is : (null) Segmentation fault (core dumped) 17

Pointers have Types int *x; // x is a pointer to an integer &z Type is: pointer to <type of z> (*myptr) Type is: type which myptr is pointing to e.g. int *myptr=&area; (*myptr)= a ; Unable to assign char to int

Void Pointers void * myptr; // myptr is a pointer to void myptr is a pointer, but I m not going to tell you what it points at Before you use myptr, you must cast it as a pointer to something printf( myptr points to %c\n,*(char *)myptr); void * used as a universal pointer a pointer to any type of data Programmer must know what type of data it s pointing at 19

Resources Programming in C, Chapter 10 Wikepedia Pointers : https://en.wikipedia.org/wiki/pointer_(computer_programming) C Pointer Tutorial : http://www.tutorialspoint.com/cprogramming/c_pointers.htm 20