Professional Documents
Culture Documents
Lawrence Angrave
C in 3 slides: Pointers
& 'address-of' (reference operator)
* 'contents-of' (dereference operator)
Automatic variables are temporary and stored
in the stack
char* p; p is a pointer to a character.
*p =0; contents-of p set to 0. (Kaboom!)
After declaring a pointer, initialize it to
something before using it. (Doh!)
2
Copyright ©: Angrave, Abdelzaher,Nahrstedt
3
Copyright ©: Angrave, Abdelzaher,Nahrstedt
4
Copyright ©: Angrave, Abdelzaher,Nahrstedt
7
Copyright ©: Angrave, Abdelzaher,Nahrstedt
8
Copyright ©: Angrave, Abdelzaher,Nahrstedt
9
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Compiler
gcc
Preprocessor
Compiler
Linker
See manual “man” for options: man gcc
"Ansi-C" standards C89 versus C99
C99: Mix variable declarations and code (for int i=…)
C++ inline comments //a comment
make – a compilation utility
Google 'makefile'
13
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Programming in C
C = Variables + Instructions
14
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Programming in C
C = Variables + Instructions
char
int
float
pointer
array
… string 15
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Programming in C
C = Variables + Instructions
char assignment
int printf/scanf
float if
pointer for
array while
… string … switch 16
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Variables
Name
10,000
x Value1
10,002
y
Value2
Memory
Address Value
10,008
z Value3
10,010
p Value4
10,012
d Value5
…
17
Copyright ©: Angrave, Abdelzaher,Nahrstedt
10,000
&y x Value1
10,002
y
Value2
Value
10,008
z Value3
10,010
p Value4
10,012
d Value5
…
18
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Pointers
Name
A pointer is a variable
10,000 whose value is the
x Value1
address of another
10,002
y
Value2
Value
10,008
z Value3
10,010
p 10,002
10,012
d Value5
…
19
Copyright ©: Angrave, Abdelzaher,Nahrstedt
p=&x; x @*%^
*p=x+1;
q=x;
printf (“Q = %d\n“, q);
}
22
Copyright ©: Angrave, Abdelzaher,Nahrstedt
p=&x; x 10
*p=x+1;
q=x;
printf (“Q = %d\n“, q);
}
23
Copyright ©: Angrave, Abdelzaher,Nahrstedt
p=&x; x 10
*p=x+1;
q=x;
printf (“Q = %d\n“, q);
}
24
Copyright ©: Angrave, Abdelzaher,Nahrstedt
p=&x; x 11
*p=x+1;
q=x;
printf (“Q = %d\n“, q);
}
25
Copyright ©: Angrave, Abdelzaher,Nahrstedt
27
Copyright ©: Angrave, Abdelzaher,Nahrstedt
p #@*%!
??
Pointing somewhere
random
28
Copyright ©: Angrave, Abdelzaher,Nahrstedt
p #@*%!
10
#@*%!
29
Copyright ©: Angrave, Abdelzaher,Nahrstedt
30
Copyright ©: Angrave, Abdelzaher,Nahrstedt
int *p;
int x;
…
p=&x;
31
Copyright ©: Angrave, Abdelzaher,Nahrstedt
int *p;
…
p=(int*) malloc (sizeof (int));
32
Copyright ©: Angrave, Abdelzaher,Nahrstedt
int p[10];
Same as:
int *p;
p=(int*) malloc (10*sizeof (int));
33
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Arrays
int p[5];
p
p[0]
p[1]
Name of array (is a pointer)
p[2]
p[3]
p[4]
Shorthand:
*(p+1) is called p[1]
*(p+2) is called p[2]
etc..
34
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Example
int y[4];
y[1]=6; y
y[2]=2; y[0]
y[1] 6
y[2] 2
y[3]
35
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Example 1:
int z[8];
int *q;
q=z;
Example 2:
int z[8];
int *q;
q=&z[0];
36
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Example 1:
int z[8];
int *q; NOTHING!!
q=z;
x (the array name) is a pointer
Example 2:
to the beginning of the array,
int z[8];
int *q; which is &x[0]
q=&z[0];
37
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Example:
How much is y at the end:
int y, x, *p;
x = 20;
*p = 10;
y = x + *p;
38
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Example:
How much is y at the end:
int y, x, *p;
BAD!!
x = 20; Dereferencing an unitialized pointer
*p = 10; will likely segfault or overwrite
something!
y = x + *p;
Segfault = unauthorized memory
access
39
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Question:
What’s the difference between
int* q;
int q[5];
Question:
What is the value of b[2] at the end?
int b[3];
int* q;
41
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Strings
(Null-terminated Arrays of Char)
Example
char s[5];
Strings are arrays that contain the string
Conventions
Strings
“string”
“c”
Character
‘c’
43
Copyright ©: Angrave, Abdelzaher,Nahrstedt
String Operations
strcpy
strlen
strcat
strcmp
44
Copyright ©: Angrave, Abdelzaher,Nahrstedt
strcpy, strlen
Syntax:
strcpy(ptr1, ptr2);
where ptr1 and ptr2 are pointers to char
value = strlen(ptr);
where value is an integer and
ptr is a pointer to char
Example:
int len;
char str[15];
strcpy (str, "Hello, world!");
len = strlen(str); 45
Copyright ©: Angrave, Abdelzaher,Nahrstedt
strcpy, strlen
What’s wrong with
char str[5];
strcpy (str, "Hello");
46
Copyright ©: Angrave, Abdelzaher,Nahrstedt
strncpy
Syntax:
strncpy(ptr1, ptr2, num);
where ptr1 and ptr2 are pointers to char
num is the number of characters to be copied
Example:
int len;
char str1[15], str2[15];
strcpy (str1, "Hello, world!");
strncpy (str2, str1, 5); 47
Copyright ©: Angrave, Abdelzaher,Nahrstedt
strncpy
Syntax:
strncpy(ptr1, ptr2, num);
where ptr1 and ptr2 are pointers to char
num is the number of characters to be copied
Caution: strncpy blindly copies the
Example: characters. It does not voluntarily append
int len; the string-terminating null character.
char str1[15], str2[15];
strcpy (str1, "Hello, world!");
strncpy (str2, str1, 5); 48
Copyright ©: Angrave, Abdelzaher,Nahrstedt
strcat
Syntax: strcat(ptr1, ptr2);
where ptr1 and ptr2 are pointers to char
strcat
Example
50
Copyright ©: Angrave, Abdelzaher,Nahrstedt
strcmp
Syntax: diff = strcmp(ptr1, ptr2);
where diff is an integer and
ptr1 and ptr2 are pointers to char
Returns zero if strings are identical
int diff;
char s1[25] = "pat";
char s2[25] = "pet";
diff = strcmp(s1, s2);
51
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Example 2:
int x, y;
y=10; w=2;
x=y++;
z=w--; 52
Copyright ©: Angrave, Abdelzaher,Nahrstedt
z=--w;
Example 2:
int x, y;
First assign then increment/decrement
y=10; w=2; x is 10, z is 2
x=y++;
z=w--; 53
Copyright ©: Angrave, Abdelzaher,Nahrstedt
54
Copyright ©: Angrave, Abdelzaher,Nahrstedt
55
Copyright ©: Angrave, Abdelzaher,Nahrstedt
56
Copyright ©: Angrave, Abdelzaher,Nahrstedt
Logic Example
if (a == b) printf (“Equal.”);
else printf (“Not Equal.”);