Title: Computational Science I
1Computational Science I
- Lecture 7
- CAAM 420, Fall 2004
- Instructor Tim Warburton
2struct
- It is sometimes convenient to gather more than
one variable type in a user defined type of
variable, known as a struct - For example if I wish to beef up an array by
keeping some information with it, perhaps its
length then I can create my own array struct
3Example of a struct
4Here I define theivec struct
Here I create an ivec named ivecA
Here I assign the contents of ivecA
Here I fill up the contentsof the array pointed
atfrom ivecA
Here I access the contents of the array pointed
at from ivecA
5Creating a Pointer to a Variable
- We can use the operator to find out what the
memory address of a variable is - We can use to dereference a pointer to examine
the memory at that pointer address - We can also use 0 to do the same thing.
6Using and 0
7Pointer to a struct
- Suppose we dereference ivecA with
- i.e. we could create a pointer to where ivecA
starts in memory - ivec ivecptrA ivecA
- Then we can access the contents with -gt
- Accessing ivecptrA-gtiNentries
- And ivecptrA0.iNentries
- Are the same as ivecA.iNentries
8Array of structs
- We can also create an array containing multiple
versions of a struct - Again we can use calloc.
- So for an array of 10 ivec structs we can use
- ivec ivecptrs (ivec) calloc(10, sizeof(ivec)
) - each of the 10 ivecs and their contents can be
accessed - ivecptrs3.iNentries gives the value of
iNentries in the 4th ivec in the allocated
memory. - We could also have used
- (ivecptrs3)-gtiNentries
9Here I create space for 10 ivecs
Here I initialize each of the 10 ivecAs
Here I derefernce the pointers in two different
ways
10cont
- When we run we see that both methods of
dereferencing give the same answer.
11Functions
- The C math library includes a large number of
math functions. - But first we should examine how a function
operates in C. - A function is an encapsulated piece of code which
has its own scope.
12Example of a Function
- Here I define a function myfunction which takes
an integer argument and returns an integer value. - myfunction will be visible (i.e. callable) by all
functions lower than it in the file
13Scope of Function
- The scope of a function is loosely defined as
the set of variables visible inside the function.
This includes - variables passed into the function as arguments
- data pointed to by pointers passed in as
arguments - variables created inside the function
- variables created by calling other functions from
inside the function (e.g. calloc, realloc) - variables defined in header files (ok, but not
great) - variables which are defined above this function
in the file but not in a function (dont do this) - variables added to the scope with the extern
command (do not do this)
14Example of Scope
- Here we define a function which takes as
arguments an integer as well as a pointer and
length of the data pointed to. - We can not only use the int value passed in but
also the contents of the int array passed in by
pointer.
15Example of Scope cont
- To call that function, we create the input data
- This involves initializing the input variable and
the input array.
16Important Note
- When you call a function with
- ib myfunction(ia)
- myfunction actually receives a copy of ia, not
the original. - If myfunction changes ia, it will not change in
the function calling myfunction
17However
- If you pass a pointer into a function
- myfunction1(ia)
- Then myfunction1 can access the variable pointed
to by the pointer. E.g. - void myfunction1(int iptrA)
- iptrA 2
-
- main()
- int iA 3
- myfunction1(iA)
-
- will change the value of the iA from 3 to 2.
18Class Exercise J
- Write a function which accepts an int as an
argument variable and adds one to the variable - Write a function which accepts a pointer to an
int as an argument, and adds one to the variable
pointed to by the pointer. - Compare the before and after values of the
variable you are trying to change using printf. - For entertainment, in function for 1) print out a
pointer to the argument before the function and
in the function.
19Extending the Visibility of a Function
- Normally a function is only visible to functions
below it in the source code. - However, we can introduce a prototype which
describes the calling convention of the function.
- If the prototype is introduced between functions,
then all subsequent functions in the file will be
able to use this function. - If the prototype is introduced inside a function
then all lines of code until the next will be
able to call the function which is prototyped.
20Prototype
This is a prototype formyfunction
This is a call tomyfunction
This is a definition formyfunction
21Passing struct to a Function
- We can also pass a user defined struct to a
function - For instance, we can create a function which
elongates our ivec array.
22Recall the ivec struct
- Here we need to pass in a pointer to the struct
as we are going to change its contents. - Notice how I update the affected members of the
struct when I change the vector length.
23cont
- Here I show how I passed in the pointer to the
original ivecA
24cont
- What happened at ivecA.iptr10 ???
25Class Exercise K
- Take myextendarray.c from the SourceCode
directory online. - Change the code so you pass in ivecA as a
variable and not as a pointer. - Access and change the contents of the input ivec
using ivecA.iptr and ivecA.iNentries - Before and after the ivecextend function call
print out the contents of ivecA - What is going on?