Lecture 06
Lecture 06
C++
Lecture 6:
Pointers, arrays and files.
Chapter 10:
Managing memory and low-level data structures
Christian W. Probst 1
Agenda
C++
Pointers and arrays
Pointers, Pointers to functions, Arrays,
Pointer arithmetic, Indexing, Array
initialization
Examples for arrays
String literals, array initialization, arguments
p x
int *p, q;
int* p, q;
int (*p), q;
fp = &next;
fp = next;
NOT exist
int tabel[10][10];
for(int i=0, i < 10, i++)
for (int j=0; j <10, j++)
tabel[i][j] = (i+1)*(j+1);
The extra character is the null character (i.e. '\0’ or binary zero)
// given a numeric grade, find and return the associated letter grade
for (size_t i = 0; i < ngrades; ++i) {
if (grade >= numbers[i])
return letters[i];
}
return "?\?\?";
}
int main()
{
ifstream infile("in");
ofstream outfile("out");
string s;
int * pointer_to_static(){
static int x; return &x; //Now better, but …..
}
T * p = new T[n];
delete[] p;
void list::prepend(char c) {
listelem* temp = new listelem; //create new elem
temp -> next = h;
temp -> data = c;
h = temp; //update head of list
}
void del ()
{
listelem* temp = h;
h = h -> next;
delete temp;
}
insertmid(’e’,3);
At first we add a new function insertmid
(char c, int pos), that takes a
character and an integer as arguments. The
new list element is inserted before the object
at position pos.
Starting point
First step
Last step