Docsity

Pointers Part 1-Introduction to Computer Programming-Lecture Slides, Slides of Computer Programming

Partial preview of the text

Download Pointers Part 1-Introduction to Computer Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

CS 192Lecture 12Fall 2011

November 30, 2011 – December 7,

Ghufran Ahmed

Pointer

•^

Points to an item

-^

Holds the memory address of the item

-^

Has its own memory storage

-^

Occupies space in memory

-^

Hence can itself be accessed too

-^

Allows C/C++ to support dynamic memory allocation

-^

If

x^

contains the address of

y

,^ x

is said to “point to”

y

•^

Pointer variables are declared as

*type *var-name;* e.g.

int *p;

pointer p contains the memory address of an int variable

float *fl;

points to a float variable

address of

value at address

&a

address of variable a

*p

contents of location pointed to by variable p

Pointer Operators

  • & (address operator)
    • a unary operator that returns the memory address of its

operand.

int balance = 350;int *balptr;balptr = &balance;

-^

This address is the location of the variable in memory, ithas nothing to do with the value of balance.

Pointer Operators

Pointers

•^

int

a=1,

b=2,

*p;

-^

Picture in memory at this point:

•^

p=&a;

//p

is

assigned

the

address

of

a

•^

b=p;*

//b

is

assigned

the

value

pointed

to

by

p

As

p

points to

a

, the statement

b=p*

is equivalent to

b=a

a^

b^

p

a^

b^

p

a^

b^

p

int main(){

*int balance;int balptr;int value;balance

balptr

= &balance;

value

*= balptr;

cout << "balance is: " << value << '\n';cout << "Memory address where balance is stored is: ”

<< balptr << endl; return 0;

}

Pointer Operators

value = *balptr;

  • The compiler transfers the proper number of

bytes according to base type.

int *p;double f;// ...p = &f; // ERROR

Base Type

int p;int x = 12;p=&x;cout << x << endl;//Assign a value to the location pointed to by pp = 101;cout << x << endl;

//what is value of x?

cout << *p << endl;cout << p << endl;cout << &x << endl;

Assigning Values Through a Pointer

(*p)++;

//increment value to the location pointed to by p

int main(){

*int p, num;p^

= # p = 454;cout << num << ' ';(p)++; /*

*parentheses are necessary because * operatorhas lower precedence than ++operator /

cout << num << ' ';(p) - -;cout << num << '\n';return 0;*

}^

//Output?

Assigning Values Through a Pointer

//Output is 454 455 454

Assigning Values Through a Pointer

  • Let p1 be a char pointer which contains the

address 4000

  • p1++;

// now p1 will be 4001

  • Each time p1 is incremented, it shall point to the

next character.

  • p1--; will cause p1 to be 3999 if initially it was
  • Pointer of type other than char shall increase or

decrease by length of base type.

Pointer Arithmetic

  • You cannot add two pointers• You can subtract two pointers (if they are of same

base type).

  • Other than
    • addition or subtraction of a pointer and an integer, OR– csubtraction of two pointers,no other arithmetic operations can be performedon pointers.
      • You cannot add or subtract float or double values.

Pointer Arithmetic

  • In C++, there is a close relationship between

pointers and arrays.

  • Two examples

Pointers and Arrays

int main(){

int *iptr;int iarray[4] = { 5, 6, 7, 8};iptr = iarray;cout << iptr << "

" << iarray << “\n”;

cout << iptr[0] << "

" << iarray[0] << “\n”;

cout << *(iptr+1) << "

" << iarray[1] <<“\n”;

return 0;

} //Output?

Pointers and Arrays