VIEWS: 25 PAGES: 16

• pg 1
```									C++ Pointers and C Strings

CS1 - Pointers    1
Pointers
A pointer is a variable
MEMORY

1
something else.                         2
int foo;               foo                3           123
int *x;                                   4
5

...

...
foo = 123;
x = &foo;                 x       81345                3
81346
81347
CS1 - Pointers                               2
int *x;

• x is a pointer to an integer.
• You can use the integer x-points-to in a
C++ expression like this:
“the int x points to”
y = *x + 17;

*x = *x +1;

CS1 - Pointers                     3
&foo
In C++ you can get the address of a
variable with the “&” operator.
MEMORY
0
1
foo = 123;                       2
x = &foo;                    foo 3            123
4
5

...

...
&foo means “the address of foo”

CS1 - Pointers                          4
Assigning a value to a
dereferenced pointer
A pointer must have a value before you
can dereference it (follow the pointer).
int *x;                                  int foo;
*x=3;                                    int *x;
x = &foo;
ing!!!
OR!!! oint to   anyth                  *x=3;
ERR n’t p
s
x doe                                              is fine foo
this        o
x po ints t
CS1 - Pointers                     5
Pointers to anything
x     some int
int *x;
int **y;     y       some *int        some int

double *z;                 z       some double

CS1 - Pointers                  6
Pointers and Arrays
• An array name is basically a const
pointer.
• You can use the [] operator with a
pointer:

int *x;        x is “the address of a[2] ”
int a[10];
x = &a[2];
for (int i=0;i<3;i++)
x[i]++;          x[i] is the same as a[i+2]
CS1 - Pointers             7
Pointer arithmetic
• Integer math operations can be used with
pointers.
• If you increment a pointer, it will be
increased by the size of whatever it points
to.
int *ptr = a;      *(ptr+2)
*(ptr+4)
*ptr

a[0]    a[1]   a[2]        a[3]   a[4]

int a[5];
CS1 - Pointers                  8
printing an array
void print_array(int a[], int len) {
for (int i=0;i<len;i++)
ion
cout << "[" << i << "] = "        vers
ay
<< a[i] << endl;       arr
}

void print_array(int *a, int len) {
ion
for (int i=0;i<len;i++)                 vers
ter
cout << "[" << i << "] = "   poin
<< *a++ << endl;
}

CS1 - Pointers             9
Passing pointers as
parameters
void swap( int *x, int *y) {
int tmp;

tmp = *x;
*x = *y;
*y = tmp;
}

CS1 - Pointers   10
Pointer Parameters
• Pointers are passed by value (the value
of a pointer is the address it holds).

• If we change what the pointer points to
the caller will see the change.

• If we change the pointer itself, the caller
won't see the change (we get a copy of
the pointer)
CS1 - Pointers           11
C strings
• A C string is a null terminated array of
characters.
– null terminated means there is a character
at the end of the the array that has the
value 0 (null).
• Pointers are often used with strings:
ull)
char *msg = “RPI”;           o (n
msg                                     zer

'R' 'P' 'I'             0

CS1 - Pointers                12
String Manipulation Functions
• C++ includes a library of C string
handling functions:

char * strcpy(char *dst, const char *src)

char * strcat(char *dst, const char *src)

lots more!

CS1 - Pointers        13
String Example - Count the
chars
int count_string( char *s) {
d
int n=0;                              ing pointe
wh  ile the th null
while (*s) {             to by   s is not
n++;                 increment count
s++;
set s to point to the next char
}
return(n);
}
CS1 - Pointers              14
Another way
int count_string( char *s) {
char *ptr = s;
while (*ptr) {
ptr++;
pointer arithmetic!
}
return(ptr - s);
}

CS1 - Pointers           15
C String vs. C++ string
class
• C++ string class is much easier to use
– comparison operator, concatenation
operator, no memory allocation issues...

• There are times when you have to use a
C String (char *), generally you just
convert from a string using c_str().
– we saw this when opening a file...
CS1 - Pointers              16

```
To top