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

Similar documents
/* EXAMPLE 1 */ #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j);

More about BOOLEAN issues

Arrays and Pointers. CSE 2031 Fall November 11, 2013

DECLARAING AND INITIALIZING POINTERS

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

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

Fundamental of Programming (C)

Arrays and Pointers (part 1)

Slides adopted from T. Ferguson Spring 2016

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

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

Course organization. Course introduction ( Week 1)

[0569] p 0318 garbage

Computer Programming Lecture 12 Pointers

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

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

2/28/2018. Overview. The C Programming Language Part 4. Pointers. Pointers. Pointers. Pointers

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

Arrays and Pointers (part 1)

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

Decimal Representation

Variables, Pointers, and Arrays

Pointers. 10/5/07 Pointers 1

Arrays and Pointers in C. Alan L. Cox

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

A brief intro to pointers for the purposes of passing reference parameters

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

2.2 Pointers. Department of CSE

Variation of Pointers

SYSC 2006 C Winter 2012

Arrays, Pointers and Memory Management

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Dynamic Memory Allocation

Procedural programming with C

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

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

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

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

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

CSC 2400: Computer Systems. Arrays and Strings in C

MYcsvtu Notes LECTURE 34. POINTERS

Array Initialization

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

More Pointers Week 11

CSC 2400: Computer Systems. Arrays and Strings in C

Pointer. Pointer variable

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

PROGRAMMAZIONE I A.A. 2017/2018

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

Dynamic memory allocation (malloc)

... Lecture 12. Pointers

Intermediate Programming, Spring 2017*

Lesson 7. Reading and Writing a.k.a. Input and Output

Lecture 9 - C Functions

BSM540 Basics of C Language

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

Introduction to Scientific Computing and Problem Solving

Arrays and Pointers. Lecture Plan.

Pointers, Dynamic Data, and Reference Types

Functions. Arash Rafiey. September 26, 2017

Multidimension array, array of strings

Arrays and Pointers (part 2) Be extra careful with pointers!

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

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

Chapter 7 C Pointers

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

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

At the end of this module, the student should be able to:

MODULE 5: Pointers, Preprocessor Directives and Data Structures

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

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Pointers, command line arguments, Process control, and functions

Outline. Pointers arithme.c and others Func.ons & pointers

Basic and Practice in Programming Lab7

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

Principles of C and Memory Management

Computer Programming Unit 3

by Pearson Education, Inc. All Rights Reserved.

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

In this chapter, you will learn about: Pointers. Dynamic Arrays. Introduction Computer Science 1 CS 23021

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Pointers as Arguments


Content. In this chapter, you will learn:

Subject: Fundamental of Computer Programming 2068

Chapter IV Introduction to C for Java programmers

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

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

COMP1917 Computing 1 Written Exam Sample Questions

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Arrays and Pointers (part 2) Be extra careful with pointers!

Example: Pointer Basics

Transcription:

Pointers CSE 2451

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 short int/long ip1 ip2 char * ip1; 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; printf( %d\n,y[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; printf("x is %d\n", x); (*p)++; printf("x is %d\n", x); 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; }