Computer Systems Organization


Start Lecture #8

Lecture given by Prof. Ralph Grishman

Some Basic Pointer Examples

The next few sections present some simple examples using pointers. Several were written in class with the students.

Value Versus Address

#include <stdio.h>
int x, *p;
int main () {
  p = &x;
  x = 12;
  printf ("p = %d\n", p);
  p++;
  printf ("p = %d\n", p);
  printf ("*p = %d\n", *p);
}

The example on the right illustrates well the difference between a variable, in this case x, and its address &x. The first value printed is the address of x. This is not 12. Instead it is (probably) some large number.

A compiler warning occurs since in (modern) C, integers (%d) and pointers (&x) are not the same type.

Incrementing p does not increment x. Instead, the result is that p points to the next integer after x. In this program there is no further integer after x, so the result is unpredictable. If, instead of x, we had p point to A[7] for some integer array A, then the last line would have printed the value of A[8] and the penultimate line would have printed the address of A[8].

include <stdio.h>
int mystrlen (char *s);
int main () {
  char stg[] = "hello";
  printf ("The string %s has %d characters\n",
          stg, mystrlen(stg));
}
int mystrlen (char *s) {
  int i;
  for (i = 0; s[i] != '\0'; i++);
  return i;
}

#include <stdio.h> int mystrlen (char s[]); int main () {} // unchanged int mystrlen (char s[]) { int i = 0; while (*s++ != '\0') i++; return i; }

String Length with Arrays and Pointers

On the right we show two versions of the string length function: The first version uses array notation for the string; the second uses pointer notation. The main() program is identical in the two versions so is shown only once. Note how very close the two string length functions are; another illustration of the similarity of arrays and pointers in C.

In the second version we encounter a common C idiom *s++. First note that the precedence of the operators is such that *s++ is the same as *(s++). That is, we are moving (incrementing) the pointer and examining what it now points at. We are not incrementing a part of the string. Specifically, we are not executing (*s)++;

Note that the two declarations

    int mystrlen (char *s)
    int mystrlen (char s[])
  
are the same. They are used 4 times in the two versions of string length.

Changing any of them to the other form does not change the meaning of the program.

#include <stdio.h>
int changeltox (char*);
int main () {
  char stg[] = "hello";
  changeltox (stg);
  printf ("The string is now %s\n", stg);
}
int changeltox (char *s) {
  while (*s != '\0') {
    if (*s == 'l')
      *s = 'x';
    s++;
  }
}

Simple Substitution

The program on the right simply loops through the input string and replaces each occurence of l with x.

The while loop and increment of s could have been combined into a for loop. This version is written in pointer style.

Homework: Rewrite changeltox() to use array style and a for loop.