Pointer Defined - Introduction to Low Level Program | CMSC 212, Study notes of Computer Science

Material Type: Notes; Professor: Hollingsworth; Class: INTRO TO LOW-LEVEL PROG; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-jzm
koofers-user-jzm 🇺🇸

9 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
1
CMSC 212 –F07 (lect 8)
Announcements
Project #2
Available on the web
Reading
Chapter 6 (today)
Chapter 11 (Thursday)
2
CMSC 212 –F07 (lect 8)
Pointers Defined
Can declare a pointer to any type of variable
are similar to references in Java
use * before variable name to create a pointer to the type
int *foo;
myTypeDef *myPtr;
float *bar;
The type determines both:
what is pointed to
the size of the object being pointed to
Initialization
creating a pointer doesn't create the space it points to
int *foo; /*these two lines will often cause an error here */
*foo = 3; /* because you haven't specified what space it points to*/
int *bar=3; /*won’t cause an error here but likely will later*/
int a[5], b; /* these two lines are the way initialization */
int *p = a, *q=&b; /* should be done */
pf3
pf4
pf5

Partial preview of the text

Download Pointer Defined - Introduction to Low Level Program | CMSC 212 and more Study notes Computer Science in PDF only on Docsity!

CMSC 212 –F07 (lect 8)^1

Announcements

 Project

  • Available on the web

 Reading

  • Chapter 6 (today)
  • Chapter 11 (Thursday)

CMSC 212 –F07 (lect 8)^2

Pointers Defined

 Can declare a pointer to any type of variable

  • are similar to references in Java
  • use * before variable name to create a pointer to the type
    • int *foo;
    • myTypeDef *myPtr;
    • float *bar;
  • The type determines both:
    • what is pointed to
    • the size of the object being pointed to

 Initialization

  • creating a pointer doesn't create the space it points to
  • int foo; /these two lines will often cause an error here / foo = 3; / because you haven't specified what space it points to/
  • int bar=3; /won’t cause an error here but likely will later*/
  • int a[5], b; /* these two lines are the way initialization */ int *p = a, q=&b; / should be done */

CMSC 212 –F07 (lect 8)^3

Pointers

 Mixing types in declaration:

  • int *p, q;
  • declares p as a pointer to an integer and q as an integer

 Can declare a pointer to a pointer

  • int *p; / p pointer to a variable that points to an int */
  • useful if you want to modify a pointer in a subroutine

 Typedefs of pointers improve readability

  • typedef int *intPtr;
  • intPtr p, q;

 Function return type can be a pointer

  • Be careful of Dangling Pointers

int *foo() {

int x = 42;

return (&x);

CMSC 212 –F07 (lect 8)^4

Other Pointer Declarations

 Can declare a pointer to a pointer

  • int *p; / p pointer to a variable that points to an int */
  • useful if you want to modify a pointer in a subroutine
  • see pntrpntr.c

 What declaration:

  • int *p, q;
  • declares p as a pointer to an integer and q as an integer

 Typedefs of pointers improve readability

  • typedef int *intPtr;
  • intPtr p, q;

CMSC 212 –F07 (lect 8)^7

Type Conversion

 C lets you assign variables of different types

  • called type conversion
  • useful when dealing with
    • external devices
      • int *fooDev = (int *) 100;
      • we know the foo device is at location 100
    • when type information read from a file is based on file

contents.

 WARNING: This can be very dangerous

 (type *) variable:

  • int *x;
  • float *y;
  • x = (int *) y;

CMSC 212 –F07 (lect 8)^8

Generic Pointers

 void *

  • Declare a pointer that can point to any type
  • Any use are assignment requires a cast:
    • void *p;
    • int *x;
    • int *y;
    • p = (void *) x;
    • y = (int *) p;
  • Allows writing generic code
    • Need to ensure only appropriate things happen:
      • int *x;
      • float *y;
      • void *p;
      • p = (void *) x;
      • y = (float *) p;

CMSC 212 –F07 (lect 8)^9

NULL Pointer

 NULL is a pointer to nothing

  • usually integer value 0
  • (void *) is its type
  • defined in stdlib.h

 Can be used to initialize pointers

  • int *p;
  • p = NULL;

 Can be used in comparison

  • if (p != NULL) {
  • }

 Often returned by functions to indicate error or

completion

CMSC 212 –F07 (lect 8)^10

Const Keyword and Pointers

 const int *p;

  • pointer to an integer that doesn't change:
  • const x = 42;
  • p = &x; /* legal */
  • p = 20; / illegal - changing the constant */

 int *const p;

  • constant pointer to an integer;
  • int *const p = &foo;
  • p = 42; / ok: just changing the value */
  • p = &bar; /* illegal: changing the pointer */