Monash University > CSSE > CSE1303 > Part A > Lectures > Lecture A02 notes

CSE1303 Computer Science
Semester 3 (summer), 2003
Part A
Lecture A02 notes: Pointers (revision)

In this lecture

Reading: What is a pointer? The operation & The operation * The operation -> Pointers and function arguments Pointer Arithmetic Arrays

Code in this lecture

#include <stdio.h>

void fakeSwap(int a, int b)
{
   int tmp;

   tmp = a;
   a = b;
   b = tmp;
}

int main()
{
   int x = 1;
   int y = 2;

   fakeSwap(x, y);

   printf("%d %d\n", x, y);
}

#include <stdio.h>

void trueSwap(int* a, int* b)
{
   int tmp;

   tmp = *a;
   *a = *b;
   *b = tmp;
}

int main()
{
   int x = 1;
   int y = 2;

   trueSwap(&x, &y);
   printf("%d %d\n", x, y);
}

/* strcpy: copy t to s; array subscript version */
char* strcpy(char* s, char* t)
{
   int i;

   i = 0;
   while (t[i] != 0)
   {
     s[i] = t[i];
     i++;
   }
   s[i]=0;
   return s;
}

/* strcpy: copy t to s; pointer version */
char* strcpy(char* s, char* t)
{
   char* p = s;

   while (*p++ = *t++)
   {
     ;
   }
   return s;
}

[ Top | Home ]
Last Updated: Tuesday 02 December 2003 22:29:28