Pointers Discussion 5 Section 0104. Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
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.
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointers Ethan Cerami Fundamentals of Computer New York University.
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
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 CSE1301 Computer Programming Lecture 16 Pointers.
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.
Arrays and Pointers in C Alan L. Cox
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
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”
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Pointers *, &, array similarities, functions, sizeof.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Computer Skills2 for Scientific Colleges
“Studying C programming excluding pointers is meaningless.” d0m3z
CSE 220 – C Programming Pointers.
Lecture 6 C++ Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Programming fundamentals 2 Chapter 2:Pointer
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Computer Skills2 for Scientific Colleges
Outline Defining and using Pointers Operations on pointers
Introduction to Problem Solving and Programming
C++ Pointers and Strings
C Programming Lecture-8 Pointers and Memory Management
C++ Pointers and Strings
Programming fundamentals 2 Chapter 3:Pointer
Introduction to Pointers
Presentation transcript:

Pointers Discussion 5 Section 0104

Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!

Pointers A pointer is a Memory address/ reference Declare a ptr to an int would be int * ip; - Creates a variable called ip which is a pointer to int Every time you write int x; C creates space in memory for it Get the address of something by (&) Ex: int y; int * x = &y; Access the object using (*) Ex: int x = 6; int * y = &x; int k = *y; Called “dereferencing”

* is very special with Pointers On the left hand side of =, it means a ptr type. Ex: int * x; char * y, double * z These are all pointers! On the right hand side of =, it means dereferencing Ex: int x; int * ptr; x = *ptr Here, the * is not referencing to a pointer type, but is accessing the value that ptr points to.

Pointers Are Undeclared What happens if you do: int * x; int y = *x; x points to an unknown location. So, dereferencing x has unknown behavior -Causes segmentation faults -y = some garbage value -Silent Errors -Alignment policies

sizeof() Returns the size in bytes that the variable is taking int x; char array [5]; sizeof(int) returns 4 because an int is 4 bytes sizeof(x) returns 4 because x is an int which is 4 bytes. sizeof(array) returns 6 because it is 5 elements long + \o character sizeof(ptr) returns 8 because the address is stored as 8 bytes.

Common Uses for Pointers FILE * is actually a ptr! Arrays are pointers. If a char * is a parameter, you can also pass in an array of chars Arrays are pointers to the first element of an array (int array [5]) array [0] means the first element in the array array[5] means it goes to the pointer at the first element and then moves over 5 int spaces. Strings are pointers Valid: char str[6]; char * strptr = str; What is the sizeof(str) and sizeof(strptr)? scanf takes a pointer. scanf(“Enter an int: %d”, &x)

Ptr Example 1 int x = 3; int *ptr; ptr = &x; printf(“%d %d”, *ptr, *(&x)); *ptr = 5;

Ptr Example 2 int x = 3; int *ptr; ptr = &x; ptr = NULL; x = *ptr; What will happen?

Pointers to Pointers Can you have pointers to pointers? YES int x = 5; int y = 6; int *ptr1 = &x; int *ptr2 = &y; int ** ptr3 = &ptr1; //double pointer printf(“%d”, **ptr3); //prints 5 *ptr3 = ptr2; printf(“%d”, **ptr3); //prints 6 **ptr3 = 2; printf(“%d”, *ptr1); printf(“%d”, *ptr2);

Pointers to Pointer Example char * argv [] is a pointer to a pointer. It is an array of char * Remember that an array is just a pointer to the first element So, a char array is the same as a char *

Passing Pointers as parameters void swap(int x, int y){ int temp = x; x = y; y = temp; } int main(){ int x = 4; int y = 10; swap(x, y); printf(“%d %d”, x, y); } Does this work?

Passing Pointers as parameters Cont. void swap(int* xptr, int* yptr){ int temp = *xptr; *xptr = *yptr; *yptr = temp; } int main(){ int x = 4; int y = 10; swap(&x, &y); printf(“%d %d”, x, y); }

Pointer Arithmetic Only two operations Distance between two pointers ptr1 – ptr2 Moving a pointer up a certain amount (No Safety) ptr1 + k ptr1 – k Where k is an integer ptr1++