Title: Pointers
1Pointers pointer arithmetics
- All variables are bound to a certain address in
memory - Ex. int ithe address of i is i
- the value of a pointer variable is an addressint
p i / pointer to an integer / - NULL as the value of a pointer variable
indicates points to nothing - Possible to reach other memory cell using the
pointer pp1 is the next address. Note! Pointer
arithmetic(p1) is the value of the next
integer
2Pointers as parameters
- The value of an argument is copied to the formal
parameter - I.e. Let two variables exchange valuesvoid
byt(int a, int b) int t / local
variable / t a a b b t
3One-dimensional arrays pointers
- The array name is a pointer to the first element
in the arrayint list25now lista
list0and list0 listand list3
(list3) - but the array name cant change value
4Arguments to function main
Declaration as int main(int argc, char
argv) Makes it possible to supply command line
arguments at run Ex. ./a.out 100 filein.txt This
command makes argc 3 (number of words in
command) and argvi points to the strings given
at run argv0 ./a.out argv1
100 argv2 filein.txt
5peppar/c/Ckodgt cat stat_slump.c include
ltstdio.hgt include ltstdlib.hgt include
ltmath.hgt include lttime.hgt include
ltlimits.hgt int main(int argc, char argv)
int i, n, die_1, die_2, two_sum
double prob int diec_res13
srand(time(NULL)) / intialize random number
generator / / Simulate throwing two dice a
number of times. / / How many throws? /
if (argc 1) printf("How many
throws ") scanf("d", n)
else sscanf(argv1,"d", n)
printf("\nValue of string pointed to by
argv1 s\n", argv1)
6printf("\nValue of n d\n", n)
fflush(stdin) for (i2 ilt13 i) / zero
all summation variables / diec_resi0
for (i 0 i lt n i)
die_1 (int) floor(rand()/(double)RAND_MAX6.0)1
die_2 (int) floor(rand()/(double
)RAND_MAX6.0)1 two_sum
die_1 die_2 diec_restwo_sum
printf("\n\n") printf("Relative
frequence Probability\n") for (i2 ilt13
i) printf("15.3f", diec_resi/(float)n)
if (i lt 8) prob (i-1)/36.0
else prob (13-i)/36.0
printf("15.3f\n", prob) return 0
7peppar/c/Ckodgt ./a.out How many throws
12000 Value of n 12000 Relative frequence
Probability 0.028 0.028
0.056 0.056 0.084
0.083 0.114 0.111
0.139 0.139 0.164
0.167 0.139 0.139
0.108 0.111 0.084
0.083 0.054 0.056
0.030 0.028 peppar/c/Ckodgt ./a.out
15000 Value of string pointed to by argv1
15000 Value of n 15000 Relative frequence
Probability 0.027 0.028
0.053 0.056 0.084
0.083 0.113 0.111
0.139 0.139 0.170
0.167 0.137 0.139
0.111 0.111 0.087
0.083 0.055 0.056
0.024 0.028
8bubbel again
- void bubbel(int list,int n)
-
- int i,last
- int bytt / logical /
- last n-1 / last index in list /
- do
-
- bytt 0 / logical false /
- for (i0i lt last-1 i)
-
- if (listi gt listi1)
-
- byt(listi,listi1)
- bytt 1 / logical true /
-
-
- last last - 1
9In bubble function
- Comparisons if (listi gt listi1) if
((listi) gt (listi1)) - Calls byt(listi,listi1)
byt(listi,listi1)
10Two-dimensional arrays
- The array name can be seen as a pointer to the
first element in the arrayint a43Note a
a0 is a pointer to an array of 4 ints and
the base address is a00which menas aij
(ai3j) - memory for 12 integers is allocated
row number
values/row
11Picture
0
1
2
a
a0
a1
a2
a3
12Matrix write
- void writeMatrix(double matrix, int m, int n)
-
- int i,j
-
- for (i0iltmi)
-
- for (j0jltnj)
- printf("7.1lf",matrixij)
- putchar('\n')
-
- / writeMatrix /
13peppar/c/Ckodgt cat writeMatrix2.c define
MAXROW 10 define MAXCOL 20 void
writeMatrix2(double matrixMAXCOL, int m, int
n) int i,j for (i0iltmi)
for (j0jltnj)
printf("7.1lf",matrixij)
putchar('\n') / writeMatrix2 /
14peppar/c/Ckodgt cat testWriteMatrix.c include
ltstdlib.hgt include lttime.hgt define MAXROW
10 define MAXCOL 20 void writeMatrix(double
matrix, int, int) void writeMatrix2(double
matrixMAXCOL, int, int) int main(void)
double a double BMAXROWMAXCOL int
i,j a calloc(4,sizeof(double ))
for (i0ilt4i) ai
calloc(3,sizeof(double)) srand(time(NULL))
/ slumptalsfrö / for (i0ilt4i)
for (j0jlt3j) aij
rand()100 - 50.0 Bij
aij writeMatrix(a,4,3)
putchar('\n') writeMatrix2(B,4,3) return 0
/ main /
15Run
peppar/c/Ckodgt ./a.out -29.0 -50.0 -43.0
10.0 -24.0 29.0 13.0 -2.0 -13.0 22.0
-12.0 16.0 -29.0 -50.0 -43.0 10.0
-24.0 29.0 13.0 -2.0 -13.0 22.0 -12.0
16.0
16Conways Game of Life
Programming project no. 3 in Ch. 8 in text book
(p. 397) is about how to simulate the Game of
Life. See separate papers (used functions can be
found on the homepage of the course, see the link
C-code demos)