Title: C Course Lecture 4
1C Course Lecture 4
- This lecture we'll talk about
- Multi-dimensional arrays.
- Pointer arithmetic.
- Pointers to structures.
- Multi-file programming.
- What is the halting problem?
2Multi-dimensional Arrays in C
- Last week we dealt with 1 dimensional arrays in
C. - C can also deal with 2 dimensional arrays
(although this is rarely done)
int array36 / Declare a 2D array / int
i,j array00 7 /Set an element of it
/ printf ("element 1,1 is d\n",array11) for
(i 0 i lt 3 i) for (j 0 j lt 6 j)
arrayij 0 / Blank the array/
3We can pass 2D arrays to and from functions
- However, to do this, we must provide a size for
them - Example prototype for function
void process_array (int 36)
Call the array with this (in main or other
function)
int thearray36 process_array(thearray)
And write the function with
void process_array (int array36) / Do
stuff to the array /
4Pointer Arithmetic
- Recall from the previous lectures that we declare
a pointer with a use an to get the "address
of" (and convert a variable to a pointer) and use
a to get the value "pointed at"
int p int q 5 p q p 6 printf ("q is now
d\n",q) printf ("p is now d\n",p)
5Pointer Arithmetic 2
- A pointer is another type of array and we can mix
between them in certain arrays - If we define an array we can use pointers to
access it
int i7 / An array of 7 ints / int j /
A pointer to an int/ j i / j points at
the start of i/ j 3 / Same as i0 3
/ j i0 / Same as j i / j j1 /
Move j to point at i1/
6Pointer Arithmetic Test
int i7 int j j i / J points at i0 /
j j1 / Moves pointer / j 3 j i
/ Same as j i0 / j 10 j14 j
j5 j1 5 i i4 / This is an error / j
j7 / J points off end of array / j 4 /
This is an error /
7Multiple file programming - why do it?
- It means that more than one person can work on a
program at once - It means that when your program gets big, you
don't have to scout through lots of functions - It means that when you change 1 file of your huge
program, you don't have to change it all - (But on the other hand, it is more complicated)
8Multi-file programming - how
- Create a "header file" (a file ending in .h)
which contains all the prototypes, enums,
defines, structs, typedefs etc for your code - Create a number of C source code files - they
should all include your header file. - Source files NOT header files contain the
functions. - One (and only one) of your C source code files
contains main. - In VC you have to add source files to a
workspace to get them to compile properly.
9Example structure
pay.h Header file - enums, structs, prototypes
update.cpp include "pay.h" Type in a new
file for a new lecturer Change file for existing
lecturer
pay.cpp include "pay.h" int main() Get user
input Call appropriate routine
printout.cpp include "pay.h" Print a cheque run
for all lecturers Print records of individual
lecturers for inspection
fileio.cpp include "pay.h" Read records when
called Writes records Make backup copies
10Things to note
- If a bit of source uses a function, it must have
access to its prototype - This is why prototypes are in the header
- Similarly for enums, defines and struct
definitions. - Forgetting to include the header file can
seriously cause problems - The functions go in the .cpp file not the .h
file. - It is traditional to use "" instead of ltgt to
indicate a header file you wrote yourself
include ltstdio.hgt
include "myprog.h"
11The extern statement
- If we want to use global variables, in multi-file
programming? - If we define them in the header then there will
be multiple copies - If we define them in one file, how will all files
see them? - The key is to define them in one file and declare
them as "extern" in other files
12Extern statement
- Only used with global variables in multi-file
projects. - Says to compiler "don't worry, this is dealt with
elsewhere"
file1.cpp int glob_array100
file2.cpp extern int glob_array100
13Pointers to struct example
typedef struct great_mathematician char
name80 int year_of_birth int
year_of_death char nationality80
MATHEMATICIAN . . MATHEMATICIAN cantor cantor
(MATHEMATICIAN )malloc (sizeof
(MATHEMATICIAN)) / Check the allocation here
/ (cantor).year_of_birth 1845 (cantor).year_o
f_death 1918 / Remember to close comments with
a diagonal slash / strcpy ((cantor).name,
"Georg Cantor") strcpy ((cantor).nationality,
"German") free(cantor) / Don't forget to free
the memory /
Consider the peculiar syntax here
14Pointers to struct (2)
- C allows name-gtbit as a shorthand for (name).bit
MATHEMATICIAN cauchy cauchy (MATHEMATICIAN
) malloc (sizeof (MATHEMATICIAN)) /
The sizeof Cauchy was quite large
/ cauchy-gtyear_of_birth 1789 cauchy-gtyear_of_de
ath 1857 strcpy (cauchy-gtname, "Augustin Louis
Cauchy") strcpy (cauchy-gtnationality,
"French") . . . free(cauchy)
This shorthand is always used by C programmers
15Passing it to a function
int main() MATHEMATICIAN turing
turing (MATHEMATICIAN ) malloc(sizeof(MATHEMAT
ICIAN)) set_up_turing(turing) / Do
stuff with the variable / free(turing) vo
id set_up_turing (MATHEMATICIAN turing)
turing-gtyear_of_birth1912
turing-gtyear_of_death1954 / In
tragic circumstances / strcpy
(turing-gtname,"Alan Mathison Turing") strcpy
(turing-gtnationality,"British")
16What is the Halting Problem?
- Isn't it annoying when a program goes into an
infinite loop? - Wouldn't it be great if your compiler could tell
if your program was going to stop before you ran
it? - Why don't we write a program which will look at
the source code and check if it will stop or
carry on forever.
17How could we tell if a program would stop?
int thiscodestops (char sourcecode) / Given
source code return 1 if code will stop and 0 if
it won't / FILE fptr fptr fopen
(sourcecode, "r") if (fptr NULL) .
. / Lots of VERY complex code here /
. return 1
18Then we could run our debugging script
int main() if (thiscodestops("testfile.c"))
printf ("Congrats, your code's
fine\n") else printf ("Your code
has a bug!\n") return 0
19Why can't it work?
Consider this program doihalt.c int main()
while (thiscodestops("doihalt.c"))
printf ("Nope, I'm not stopping\n")
printf ("Actually I decided to stop!\n")
return 0