Chapter7 - Administrative Issues TA for ECE 3331 is Fouad...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Administrative Issues Extended TA Office hours: TA for ECE 3331 is Fouad Shehab E-mail: fouad.shehab@hotmail.com n Thursday , he will be available from 10:00 AM - 2:25 PM and from 4:05 PM - 6:55 PM and from 8:30 PM - 9:00 PM On Friday , he will be available from 10:00 AM - 6 PM
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2
Background image of page 2
3 Pointer variables Definition: A pointer variable holds the address of a cell Pointer has its own address. The content of pointer is the address of another cell, e.g., the address of x. Address of x Address of ptr x 9999 ptr 9999 9996 123
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Applications of pointers 2 ways to access a variable: I. int x = 123; /* access x through its name */ II ptr = &x; /* the address of variable x assign to ptr. */ *ptr=123 Note: A variable that holds an address, such as ptr, is called the pointer variable ( simply call pointer ). x 9999 ptr 9999 9996 Address of x Address of ptr 123
Background image of page 4
5 To dereference the pointer ptr is to use the expression *ptr. *ptr -- to access the contents of the cell whose address is stored in ptr. ptr = &x; *ptr = 123; Notice that x and *ptr both have the same effect, that is, both store the value 123 in x. 123 x 9999 ptr 9999 9996 Assignment using a pointer.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Pointer types A pointer holding address of an integer: int *pt A pointer holding address of a character char *ptr
Background image of page 6
7 3 ways to define pointers int *pt int* pt int * pt (not a popular way) defines pt as a pointer variable of type int* int *pt1, *pt2 /*both are pointers*/ int* pt1, pt2 /*pt1 is a pointer; pt2 is an integer*/
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 void f( int* p, char* s ) void f( int *p, char *s ) { { ... } } void* pointer type data type for a generic pointer variable a pointer to void may be converted to any other data type and vice versa
Background image of page 8
9 int *iptr; void *vptr; float real = 123; iptr = ℜ /*** Error to hold the address of float ***/ iptr = ( int* ) ℜ /* It is ok because of cast operation. */ vptr = ℜ /*** It is ok *** / iptr = vptr; /*** It is ok ***/
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 void *f( void ); /* f is a pointer holding the address determined by the return value*/ char *g( void ); int *ret; ret = f( ); /*** OK ***/ ret = g( ); /*** ERROR: cast needed ***/ ret = ( int* ) g( ); /*** OK ***/
Background image of page 10
11 /* program swap.c */ #include <stdio.h> #include <stdlib.h> main( ) { char char1 = ‘A’; char char2 = ‘B’; char temp; char *char_ptr; char_ptr = &char1; temp = *char_ptr; *char_ptr = char2; /* the contents of the variable whose address is in char_ptr */ char2 = temp; }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
char char1 = ‘A’; char char2 = ‘B’; char temp; char1 1256 char2 2468 temp 5566 ‘A’ ‘B’ ? Allocation and initialization. char *char_ptr; char1 1256 char2 2468 temp 5566 ‘A’ ‘B’ ? Allocation of a cell to hold an address. char_ptr
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/26/2011 for the course ECE 3331 taught by Professor Staff during the Fall '08 term at University of Houston.

Page1 / 63

Chapter7 - Administrative Issues TA for ECE 3331 is Fouad...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online