Programming in C and C++
Programming in C and C++
2 / 25
Example
int **ppi
char *pc
int *pi
char c
int i
00 00 52 00
00 00 1c 00 41 Little
... ... ...
00 00 42 00 41 Big
38 4c 05 62
0x2c
0x30
0x34
0x38
0x4c
0x50
0x60
3 / 25
Manipulating pointers
4 / 25
Example
1 #include <stdio.h>
2
3 int main(void) {
4 int x=1,y=2;
5 int *pi;
6 int **ppi;
7
5 / 25
Pointers and arrays
6 / 25
Pointer arithmetic
7 / 25
Example
1 #include <stdio.h>
2
3 int main(void) {
4 char str[] = "A string.";
5 char *pc = str;
6
7 printf("%c %c %c\n",str[0],*pc,pc[3]);
8 pc += 2;
9 printf("%c %c %c\n",*pc, pc[2], pc[5]);
10
11 return 0;
12 }
8 / 25
Pointers as function arguments
9 / 25
Example
10 / 25
Arrays of pointers
11 / 25
Example
argv[1] firstarg\0
argc: 3
argv[2] secondarg\0
argv[3] NULL
12 / 25
Multi-dimensional arrays
13 / 25
Pointers to functions
14 / 25
Example
15 / 25
Example
1 #include <stdio.h>
2 #include "example8.h"
3
4 int main(void) {
5 int a[] = {1,4,3,2,5};
6 unsigned int len = 5;
7 sort(a,len,inc); //or sort(a,len,&inc);
8
15 return 0;
16 }
16 / 25
The void * pointer
17 / 25
Structure declaration
18 / 25
Structure definition
19 / 25
Member access
20 / 25
Self-referential structures
1 struct tree {
1 struct link {
2 int val;
2 int val;
3 struct tree *left;
3 struct link *next;
4 struct tree *right;
4 }
5 }
21 / 25
Unions
22 / 25
Bit fields
23 / 25
Example (adapted from K&R)
24 / 25
Exercises
25 / 25