CH 16
CH 16
CH 16
2. Then, on to pointers
Pointers Arrays
CSE 240
Short-circuit Evaluation
What about this?
int func(int x) { printf(%d, x); return x; } int main() { int x = 0; int y = 1; if (func(x) && func(y)) { } }
Static Variables
and what about this?
int func(int x) { static int sum = 0; sum = sum + x; return sum; } int sum = 0; int func(int x) { sum = sum + x; return sum; }
int main() One difference: sum should be { accessible only from func() printf(%d , func(5)); printf(%d, func(3)); }
Displays 5 8
3 Why? static variables act much like global variables Its value persists across calls Actually stored in global segment (accessed by R4) Difference: only accessible within function 4
CSE 240
CSE 240
address
value
x3107 x2819 x0110 x0310 x0100 x1110 x11B1 x0019
x3100 x3101 x3102 x3103 x3104 x3105 x3106 x3107
Array
A list of values arranged sequentially in memory Expression a[4] refers to the 5th element of the array a Example: video memory in Snake (2D)
CSE 240
R2 is a pointer
It contains the address of data (Its also an array, but more on that later)
CSE 240
after call
3 temp These values changed...
R6
3 4 4 3
first second R6 b a
main
4 3 4 3
Pointers in C
C lets us talk about and manipulate pointers as variables and in expressions. Declaration int *p;
Example
int i; int *ptr;
store the value 4 into the memory location associated with i xEFF9 xEFFA xEFFB xEFFC xEFFD xEFFE
xEFFC
ptr i
/* p is a pointer to an int */
A pointer in C is always a pointer to a particular data type: int*, double*, char*, etc.
store the address of i i = 4; 45 into the memory location ptr = &i; associated with ptr *ptr = *ptr + 1; printf(%d\n, i); read the contents of memory at the address stored in ptr
Operators *p -- returns the value pointed to by p &z -- returns the address of variable z
CSE 240
CSE 240
10
Pointers as Arguments
Passing a pointer into a function allows the function to read/change memory outside its activation record void swap(int *first, int *second) { int temp = *first; *first = *second; *second = temp; Arguments are }
How would you do this in Java? All arguments in C are pass-by-value. Also true in Java, but Java has reference types 11
CSE 240
integer pointers. Caller passes addresses of variables that it wants function to change
12
R6
temp
R6
xEFF9 xEFFA xEFFB xEFFC xEFFD xEFFE
xEFFA xEFF9 43 34
first second b a
CSE 240
14
Null Pointer
Sometimes we want a pointer that points to nothing. In other words, we declare a pointer, but were not ready to actually point to something yet. int *p; p = NULL;
/* p is a null pointer */
This solves the mystery of the & for calling scanf(): scanf("%d %d", &data1, &data2);
NULL is a predefined macro that contains a value that a non-null pointer should never hold.
Often, NULL = 0, because Address 0 is not a legal address for most programs on most platforms Dereferencing a NULL pointer: program crash! ! int *p = NULL; printf(%d, *p); // CRASH! 15
CSE 240
16
Pointer Problems
What does this do?
int *x; *x = 10;
Declaring Pointers
The * operator binds to the variable name, not the type All the same:
int* x, y; int *x, y; int *x; int y;
CSE 240
Arrays
How do we allocate a group of memory locations?
Character string Table of numbers int int int int num0; num1; num2; num3;
Array Syntax
Declaration type variable[num_elements];
all array elements are of the same type number of elements must be known at compile-time
What if there are 100 numbers? How do we write a loop to process each number?
20
R6
; R0 = 5 ; R1 = &grid[0] ; grid[6] = R0
x grid[0] grid[1] grid[2] grid[3] grid[4] grid[5] grid[6] grid[7] grid[8] grid[9]
CSE 240
21
CSE 240
22
R6
x grid[0] grid[1] grid[2] grid[3] grid[4] grid[5] grid[6] grid[7] grid[8] grid[9]
CSE 240
23
24
int average(int values[], int size) { int index, sum; for (index = 0; index < size; index++) { sum = sum + values[index]; } return (sum / size); }
CSE 240
25
CSE 240
26
CSE 240
27
CSE 240
28
C does size calculations under the covers, depending on size of item being pointed to: double x[10]; allocates 20 words (2 per element) double *y = x; *(y + 3) = 100;
CSE 240
29
30
31
CSE 240
32
CSE 240
33
CSE 240
34
36
CSE 240
37
CSE 240
38
C String Library
C has a limited string library
All based on null-terminated strings #include <string.h> to use them
Functions include
int strlen(char* str) void strcpy(char* dest, char* src) int strcmp(char* s1, char* s2) ! Returns 0 on equal, -1 or 1 if greater or less ! Remember, 0 is false, so equal returns false! strcat(char* dest, char* src) ! string concatenation (appending two strings) strncpy(char* dest, char* src, int max_length) strncmp(char* s1, char* s2, int max_length) strncat(char* dest, char* src, int max_length) Plus some more
CSE 240
Answer:
char amessage[] = "message" // single array
m e s s a g e \0
char *pmessage = "message" // pointer and array
m e s s a g e \0
39
CSE 240
40
Main(), revisited
Main supports command line parameters
Much like Javas public static void main(String[] args)
Main supports command line parameters: int main(int argc, char *argv[]) { int i; for (i = 0; i<argc; i++) { printf(%s\n, argv[i]); } } Displays each command-line argument
Zero-parameter is the program name
CSE 240
An array of strings
41