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.
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:. Each byte has a unique address in memory. Address. Address. Contents. 0. 1. 2. 3. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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.
Address
00110011
10101010
00000111
11111000
10000100
Address Contents
0
1
2
3
n-1
Addressint i = 9; /* suppose sizeof(int) = 4The address of variable i is 0xFA83
0xFA83
0xFA84
0xFA85
0xFA86
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
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.
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
Pointer Assignment
int i, j, *p, *q;p = *i;q = p;
p
q
i
Pointer Assignment
int i, j, *p, *q;p = &i;q = &j;*q = *p;
p
q
i
Pointer as Arguments
Example 2.c
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”}
Pointers as Return Valuesint *max(int *a, int *b){ if(*a > *b) return a; else return b;}
int *p = max(&a, &b);
Be carefulint *f(void){ int a; …… return &a;}
Pointers and ArraysPointer Arithmetic
int a[10], *p;p = &a[0];
a
p
Pointer Arithmetic
p
p = &a[2];q = p + 3;p += 4;
q
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)
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;