Anda di halaman 1dari 18

Pointers

Krisana CHINNASARN 310211 C Programming & 310222 Computer Programming January 4, 2004

Today
Review Pointers

1/12/04

C Programming

Review
Character Functions
Input & Output? Testing Characters? Mapping Characters? Whats required?

1/12/04

C Programming

Review
String Functions
Length? Manipulation? Comparison? Searching? Whats required?

1/12/04

C Programming

Pointers
Pointer
An address of an object in memory
Does not allocate space for an object
Allocate space or assign address of a non-pointer object

An array name is also a pointer

Format: type *name [= object address]


E.g., char *c;

1/12/04

C Programming

Pointer Examples
char *c; (pointer to char, not assigned) int b = 5; int *bptr = &b; char str[8]; char *s = str; bptr

5 b s str[0] str[7]
6

1/12/04

C Programming

Assigning Addresses to Pointers


Pointer to single object
Format: ptr = &object;

Pointer to first array element


Format: ptr = array

Pointer to array element x


Format: ptr = &array[x]

1/12/04

C Programming

Assigning Values to Pointers


Inherited from a variable
double x = 3.14; *xp = &x;
Value of xp will be 3.14

1/12/04

C Programming

Assigning Values to Pointers


Set pointer value directly
double x; *xp = &x; *xp = 3.14; Make sure pointer has been set to an allocated memory region

Setting pointer value to null


E.g., int *p = 0; int *p = NULL;

1/12/04

C Programming

Seeing the Value of a Pointer


Address
E.g., print(%x,ptr);
Prints a hexadecimal number the address that ptr points to

1/12/04

C Programming

10

Seeing the Value of a Pointer


Value
Single Element
E.g, print(%d,*ptr);
Prints the value stored at the address that ptr points to

Array
E.g, print(%d, ptr[5]);
Prints the 6th element of the array

E.g., ptr = str + 2; print (%c,*ptr);


Prints 3rd element of string

E.g., print (%c,*(str + 2))


Prints 3rd element of string

1/12/04

C Programming

11

Programming Exercise
Write a program that
Declares a constant string Hello Declares a char pointer to this string Prints the string using the pointer

1/12/04

C Programming

12

Pointer Arithmetic
Same as arithmetic on regular variables
Examples
char s[10], *p = s + 5; ++p; p--; p += 3;

Be careful not to go past array boundaries


Maintain variable with array size
1/12/04 C Programming 13

Programming Exercise
Write a program that
Uses pointer arithmetic to determine if the strings abba and abcdba are palindromes

1/12/04

C Programming

14

Using Pointers
String Functions
Use pointers for returned memory addresses E.g., char *p = strstr(s1, s2);
s1 s2 Say hello to my little friend little p
1/12/04 C Programming 15

Using Pointers
Call by Reference
Change the value of function parameters
Eliminate return statement
void increment (int *); void increment (int *num) { (*num)++; } void main () { int num = 0; increment(&num); print(%d\n,num); }
C Programming

1/12/04

16

Programming Exercise
Write a program that
Declares two strings
s1 = Hello, s2 = World

Prints the values of these strings Calls a function swap that changes the value of these two strings in memory such that
s1 = World, s2 = Hello

Prints the value of s1 and s2 in the main program

1/12/04

C Programming

17

Next Time
Reading and writing from/to Files

1/12/04

C Programming

18

Anda mungkin juga menyukai