Pointers CSE 2451 Rong Shi.

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 CSE1301 Computer Programming Lecture 16 Pointers.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Introduction to C Programming CE
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
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.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
What are Pointers? Different from other normal variables which can store values. pointers are special variables that can hold the address of a variable.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
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.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Review 1 List Data Structure List operations List Implementation Array Linked List.
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.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
+ 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.
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
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.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
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.
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.
Pointers What is the data type of pointer variables?
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
Computer Skills2 for Scientific Colleges
CSE 220 – C Programming Pointers.
Lecture 6 C++ Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Computer Skills2 for Scientific Colleges
POINTER CONCEPT 4/15/2019.
Chapter 9: Pointers and String
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
POINTER CONCEPT 8/3/2019.
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

Pointers CSE 2451 Rong Shi

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

Pointers Values of variables are stored in memory, at a particular location A location is identified and referenced with an address Analogous to identifying a house’s location via an address Pointer  address Address vs. contents Pointer variable Value holds the address of a memory location

Pointers and Addresses A byte-addressable machine has consecutively numbered or addressed memory cells (bytes) which may be manipulated individually or in contiguous groups. char * ip1; char ip1 short int/long ip2 double double * ip2;

Pointers and Addresses a pointer array holding addresses of characters; char * a[2]; A pointer array holding addresses of integers; int * b[2];

Pointer syntax * is used in the declaration of a pointer int *p -> pointer to an integer & (unary operator) gives the “address of” an object p = &c means the address of c is assigned to the variable p

Pointer syntax * (unary operator) is a dereferencing operator when applied to pointers Access the variable the pointer points to * in front of a pointer variable means “get the value at that address” i.e. “contents of” int a = *p Get the value at the address designated by p and assign it to a *p = 1 Assign the value of 1 to the memory location designated by the address of p

Declaring pointers int *a; int* a; Use the first style, second leads to mistakes int* b, c, d int *b, *c, *d char *message = “Hello world!”; char *message message = “Hello world!”;

Pointers and Addresses pointers and references: “&” vs “*” “&” is used on a variable (can be char, int, double, array element) to get the address: int i, *ip1, *ip2, a[8]; char c, *cp; ip1 = &i; ip2 = &a[0]; cp = &c;

Pointers and Addresses pointers and references: “&” vs “*” “&” can not be used on a constant, array name, register variable and expressions: char * ip1; register int a; char b[10]; int * ip2; ip1=&’y’; /* WRONG */ ip1=&”abcdef”; /*WRONG*/ ip1=“abcedf”; /*RIGHT*/ ip2=&7; /* WRONG */ ip2=&a; /*WRONG*/ ip1=b; /*RIGHT */ ip1=&b; /*WRONG */ ip2=&(ip1+2); /*WRONG*/

Pointers and Addresses pointers and references: “&” vs “*” * is used on a pointer to access the variable it points to. int x =1,y[2]={2,3}; int * ip1,*ip2,*ip3; ip1=&x; *ip1=0; //x=0; printf(“%d\t%d\n”,x,y[0]); ip2=&y[0]; *ip2=1; //y[0]=1; printf(“%d\n”,y[0]); ip3=y; //ip3=&y[0]; *ip3=0; //y[0]=0;

Pointers and Addresses pointers and references: “&” vs “*” “* pointer” has a datatype which is the same as that of the variable the pointer points to. int *a; every pointer points to a specific data type except void *; void * is used to hold the address of any data type.

Pointers and Function Arguments Still pass-by-value with pointers Dereferencing the value of the pointer gives a memory address The memory’s content can change Note: Note argument types when passing parameters swap (int*, int *); swap(&x,&y); /*pass addresses of two integer variables*/

Address Arithmetic Pointers can use arithmetic operations int *ip1, *ip2, a[8]; ip1 = &a[7]; ip2 = &a[0]; What about: ip2 – ip1 + 1 ip1 – ip2 ? ip1 == NULL? ip1 > ip2 ?

Pointer example 1 #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j); return 0; }

Pointer example 2 #include <stdio.h> #include <stdlib.h> main() { int x, *p; p = &x; *p = 0; printf("x is %d\n", x); printf("*p is %d\n", *p); *p += 1; (*p)++; return 0; }

Pointer example 3 #include <stdio.h> int main(void) { char ch = 'c'; char *chptr = &ch; int i = 20; int *intptr = &i; float f = 1.20000; float *fptr = &f; char *ptr = "I am a string"; printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr, *intptr, *fptr, *ptr, ptr); return 0; }

Pointers and Function Arguments C passes arguments to functions by value; swap(int x, int y); /* change local copies of x, y*/ swap(int * x, int * y); /*use pointer to change the content of two variables */

Parameter Passing -- Example void swap (int x, int y) { int temp; temp = x; x = y y = temp; } void swap (int *x, int *y) { int temp; temp = *x; *x = *y *y = temp; }