C programming---Pointers The first step: visualizing what pointers represent at the machine level....
-
Upload
bertram-golden -
Category
Documents
-
view
215 -
download
0
description
Transcript of C programming---Pointers The first step: visualizing what pointers represent at the machine level....
![Page 1: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/1.jpg)
C programming---Pointers
The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into bytes.Each byte can hold 8 bits of information:
1 0 1 0 0 0 1 1
Each byte has a unique address in memory.
![Page 2: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/2.jpg)
Address
00110011
10101010
00000111
11111000
10000100
Address Contents
0
1
2
3
n-1
![Page 3: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/3.jpg)
Addressint i = 9; /* suppose sizeof(int) = 4The address of variable i is 0xFA83
0xFA83
0xFA84
0xFA85
0xFA86
![Page 4: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/4.jpg)
Pointer VariablesUse a pointer variable p to store the address of a variable i, and we say p “points to” i
0xFA83
0xFA83
0xFA84
0xFA85
0xFA86
p
![Page 5: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/5.jpg)
Declaring Pointer Variables
int *p; // p is a pointer variable capable of pointing to objects of type intchar *str;double *q;
C requires that every pointer variable point to objects of a particular type(the referenced type)
There are no restrictions on what referenced type may be. In fact, a pointer variable can even point to another pointer.
![Page 6: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/6.jpg)
The Address and Indirection Operators
int i, *p;…….p = &i;
int i;int *p = &i;
int i, p = &i;
![Page 7: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/7.jpg)
The Address and Indirection Operators
printf(“%d\n”, *p);
j = *&i;
![Page 8: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/8.jpg)
The Address and Indirection Operators
int i, *p = &i;i = 1;printf(“i = %d\n”, i);printf(“*p = %d\n”, *p);*p = 4;printf(“i = %d\n”, i);printf(“*p = %d\n”, *p);
![Page 9: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/9.jpg)
Something to remember
Never apply the indirection operator to an uninitialized pointer variable.int *p;printf(“%d”, *p);
Unless you know where a pointer points to, do not make an assignment to the pointer
![Page 10: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/10.jpg)
Pointer Assignment
int i, j, *p, *q;p = *i;q = p;
p
qi
![Page 11: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/11.jpg)
Pointer Assignment
int i, j, *p, *q;p = &i;q = &j;*q = *p;
p
qi
![Page 12: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/12.jpg)
Pointer as ArgumentsExample 2.c
![Page 13: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/13.jpg)
Using const to Protect ArgumentsWhen we call a function and pass it a pointer to a variable, we normally assume that the function will modify the variable. Sometimes we just want to examine the value of a variable, not change it.Using pointer might be efficient: time and memory space
void f(const int *p){ *p = 0; // wrong: p is a pointer to a “constant integer”}
![Page 14: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/14.jpg)
Pointers as Return Valuesint *max(int *a, int *b){ if(*a > *b) return a; else return b;}
int *p = max(&a, &b);
![Page 15: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/15.jpg)
Be carefulint *f(void){ int a; …… return &a;}
![Page 16: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/16.jpg)
Pointers and ArraysPointer Arithmetic
int a[10], *p;p = &a[0];
a
p
![Page 17: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/17.jpg)
Pointer Arithmetic
p
p = &a[2];q = p + 3;p += 4;
q
![Page 18: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/18.jpg)
Comparing Pointers
Using the relational operators( <, <=, >, >=) and the equality operator(== and !=)
int *p = &a[5];int *q = &a[1];
The value of p <= q is 0(false)The value of p >= q is 1(true)
![Page 19: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/19.jpg)
Using Pointers for Array Processing#define N 10
int a[N], sum, *p;sum = 0;for(p = &a[0]; p < &a[N]; p++) sum += *p;
![Page 20: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/20.jpg)
Combining the * and ++ Operators*p++ or *(p++)(*p)++*++p or *(++p)++*p or ++(*p)
The postfix version of ++ takes precedence over *
See 3.c
![Page 21: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/21.jpg)
Using an Array Name as a Pointer
int a[10];*a = 7; // modify a[0]*(a + 2) = 13; // modify a[2]
while( *a != 0 ) a++; // wrong
![Page 22: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/22.jpg)
Array Arguments
int find_largest(int a[], int n){}
int find_largest(int *a, int n){}
![Page 23: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/23.jpg)
Using a Pointer as an Array Name
#define N 10…..int a[N], i, sum = 0, *p = a;…..for(i =0; i < N; i++) sum += p[i];
![Page 24: C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.](https://reader035.fdocuments.in/reader035/viewer/2022062504/5a4d1b6b7f8b9ab0599b3664/html5/thumbnails/24.jpg)
Pointers and Multidimensional Arrays
int a[NUM_ROWS][NUM_COLS];int row, col;for(row = 0; row < NUM_ROWS; row++) for(row = 0; row < NUM_ROWS; row++) a[row][col] = 0;
int *p;for(p = &a[0][0]; p <= &a[NUM_ROWS-1][NUM_COLS]; p++) *p = 0;