Download presentation
Presentation is loading. Please wait.
1
1 TDBA66, vt-04, Lecture Ch8 Pointers & pointer arithmetics All variables are bound to a certain address in memory Ex. int i; the address of i is &i the value of a pointer variable is an address int *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 p p+1 is the next address. Note! Pointer arithmetic *(p+1) is the value of the ”next integer”
2
2 TDBA66, vt-04, Lecture Ch8 Pointers as parameters The value of an argument is copied to the formal parameter I.e. Let two variables exchange values void byt(int *a, int *b) { int t; /* local variable */ t = *a; *a = *b; *b = t; } int v1,v2; byt(&v1,&v2); Call
3
3 TDBA66, vt-04, Lecture Ch8 One-dimensional arrays & pointers The array name is a pointer to the first element in the array int list[25]; now list &list[0] and list[0] *list and list[3] *(list+3) …but the array name can’t change value
4
4 TDBA66, vt-04, Lecture Ch8 Arguments to function main Declaration as int main(int argc, char *argv[]) Makes it possible to supply command line arguments at run time Ex../a.out 100 filein.txt This command makes argc == 3 (number of ”words” in command) and argv[i] points to the strings given at run time argv[0] ”./a.out” argv[1] ”100” argv[2] ”filein.txt”
5
5 TDBA66, vt-04, Lecture Ch8 peppar:~/c/Ckod> cat stat_slump.c #include int main(int argc, char *argv[]) { int i, n, die_1, die_2, two_sum; double prob; int diec_res[13]; srand(time(NULL)); /* intialize random number generator */ /* Simulate throwing two dice a number of times. */ /* How many throws? */ if (argc == 1){/* No arguments */ printf("How many throws: "); scanf("%d", &n); fflush(stdin); } else{ sscanf(argv[1],"%d", &n); printf("\nValue of string pointed to by argv[1] %s\n", argv[1]); }
6
6 TDBA66, vt-04, Lecture Ch8 printf("\nValue of n= %d\n", n); for (i=2; i<13; i++) /* zero all summation variables */ diec_res[i]=0; for (i = 0; i < n; ++i) {/* simulate n trows with two diec */ die_1= (int) floor(rand()/(double)RAND_MAX*6.0)+1; die_2= (int) floor(rand()/(double)RAND_MAX*6.0)+1; two_sum = die_1+ die_2; diec_res[two_sum]++; } printf("\n\n"); printf("Relative frequence Probability\n"); for (i=2; i<13; i++){ printf("%15.3f", diec_res[i]/(float)n); if (i < 8) prob = (i-1)/36.0; else prob = (13-i)/36.0; printf("%15.3f\n", prob); } return 0; }
7
7 TDBA66, vt-04, Lecture Ch8 peppar:~/c/Ckod>./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/Ckod>./a.out 15000 Value of string pointed to by argv[1] 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
8
8 TDBA66, vt-04, Lecture Ch8 bubbel again void bubbel(int list[],int n) { int i,last; int bytt; /* logical */ last = n-1; /* last index in list */ do {/* place largest element in last position */ bytt = 0; /* logical false */ for (i=0;i <= last-1; i++) { if (list[i] > list[i+1]) { byt(&list[i],&list[i+1]); bytt = 1; /* logical true */ } last = last - 1; }while (bytt && last!=1); } /* bubbel */
9
9 TDBA66, vt-04, Lecture Ch8 In bubble function Comparisons : if (list[i] > list[i+1]) if (*(list+i) > *(list+i+1)) Calls : byt(&list[i],&list[i+1]); byt(list+i,list+i+1);
10
10 TDBA66, vt-04, Lecture Ch8 Two-dimensional arrays Normally int a[4][3];/* memory for 12 integers is allocated */ and the base address is &a[0][0] which means a[i][j] *(a+i*3+j) #values/row row number
11
11 TDBA66, vt-04, Lecture Ch8 peppar:~/c/Ckod> cat writeMatrix2.c #define MAXROW 10 #define MAXCOL 20 void writeMatrix2(double matrix[][MAXCOL], int m, int n) { int i,j; for (i=0;i<m;i++) { for (j=0;j<n;j++) printf("%7.1lf",matrix[i][j]); putchar('\n'); }; }/* writeMatrix2 */
12
12 TDBA66, vt-04, Lecture Ch8 Picture a a[0] a[1] a[2] [0][1][2] a[3] Possible to create structure like this
13
13 TDBA66, vt-04, Lecture Ch8 double **a; int i,j; a = calloc(4,sizeof(double *)); for (i=0;i<4;i++) a[i] = calloc(3,sizeof(double));
14
14 TDBA66, vt-04, Lecture Ch8 Matrix write void writeMatrix(double **matrix, int m, int n) { int i,j; for (i=0;i<m;i++)/* i control row no. */ { for (j=0;j<n;j++)/* j control column no. */ printf("%7.1lf",matrix[i][j]); putchar('\n');/* new line on screen */ }; }/* writeMatrix */
15
15 TDBA66, vt-04, Lecture Ch8 peppar:~/c/Ckod> cat testWriteMatrix.c #include #define MAXROW 10 #define MAXCOL 20 void writeMatrix(double **matrix, int, int); void writeMatrix2(double matrix[][MAXCOL], int, int); int main(void) { double **a; double B[MAXROW][MAXCOL]; int i,j; a = calloc(4,sizeof(double *)); for (i=0;i<4;i++) a[i] = calloc(3,sizeof(double)); srand(time(NULL)); /* slumptalsfrö */ for (i=0;i<4;i++) for (j=0;j<3;j++){ a[i][j] = rand()%100 - 50.0; B[i][j] = a[i][j]; } writeMatrix(a,4,3); putchar('\n'); writeMatrix2(B,4,3); return 0; }/* main */
16
16 TDBA66, vt-04, Lecture Ch8 peppar:~/c/Ckod>./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 Run
17
17 TDBA66, vt-04, Lecture Ch8 Conway’s 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”)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.