Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers...

19
Pointers CSE 2451 Rong Shi

Transcript of Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers...

Page 1: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

Pointers

CSE 2451Rong Shi

Page 2: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

Language comparison

• C has pointers

• Java has references

• C++ has pointers and references

Page 3: 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

Page 4: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

A byte-addressable machine has consecutively numbered or addressed memory cells (bytes) which may be manipulated individually or in contiguous groups.

Pointers and Addresses

char

short

int/long

ip1

ip2

double

char * ip1;

double * ip2;

Page 5: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

Pointers and Addresses

• a pointer array holding addresses of characters;– char * a[2];

A pointer array holding addresses of integers; int * b[2];

Page 6: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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

Page 7: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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

Page 8: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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!”;

Page 9: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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;

Page 10: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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*/

Page 11: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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]);

Page 12: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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.

Page 13: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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*/

Pointers and Function Arguments

Page 14: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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 ?

Page 15: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

Pointer example 1

#include<stdio.h> int main() {

float i=10, *j;void *k;k=&i; j=k; printf("%f\n", *j);return 0;

}

Page 16: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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;

}

Page 17: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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;

}

Page 18: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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 */

Page 19: Pointers CSE 2451 Rong Shi. Language comparison C has pointers Java has references C++ has pointers and references.

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;}