C Programming
Class 3
Objective
c3.tar
Pointers
ex1.c
Pointers are a very powerful feature of the C programming language, they
are also somewhat tricky to deal with as well. Don't worry if you don't
understand pointers after this class especially if you don't really have
any programming experience.
-
What is a pointer? A pointer is a variable that contains a memory address
as its value. Consider the variable declaration:
The * in the second declaration indicates that the variable
aPtr
is
a pointer to an integer value.
Pointer Operators: & and *
The & is called the address operator and when placed
in front of a variable returns its memory address. Notice that this is
used in the scanf() function.
Example:
int a = 7;
aPtr = &a;
This statement assigns the address of a to the integer
pointer
aPtr. The variable aPtr is now said to point
toa.
The * is called the dereferencing operator and when
placed in front of a pointer returns the value that the pointer points
to.
Example: (Assume all previous statements.)
printf("%d", *aPtr); This statement will output
7.
Arrays and Pointers
ex2.c
In C, there is a strong relationship between pointers and arrays, any operation
which can be achieved by array subscripting can also be done with pointers.
The declaration int a[10]; defines an array
a of size
10, that is a block of 10 consecutive objects named
a[0], a[1], ...
a[9]. The notation a[i] means the element of the array
i
positions from the beginning.
If pa is a pointer to an integer, declared as
int *pa;
then the assignment
pa = & a[0];
sets pa to point to the zeroth element of a;
that is, pa contains the address of a[0] . Now
the assignment
int x = *pa ;
will copy the contents of a[0] into x;
If pa points to a particular element of an array
a,
then by definition, pa + 1 points to the next element, and in
general pa - i points
i elements before pa.
Thus, if pa points to a[0], then *(pa + 1) refers
to the contents of a[1].
Question: What is the difference
between *(pa + 1) and (*pa + 1) ?
The array name a is actually a pointer to the first element
of the array, so a is equvilent to &a[0]. We could
also have the statement:
pa = a;
The following statements are equivalent:
-
a[2]
-
*(a + 2)
-
pa[2]
-
*(pa + 2)
The second and fourth statement offset the pointer by 2 and then use the
dereferencing operator to get its value. (Note: offsetting a pointer will
not change its initial value.)
Memory Allocation
ex3.c
Space is automatically set aside for variables when they defined, but sometimes
you do not know beforehand how many variables you'll need or just how long
an array might need to be. The malloc command creates space, returning
a pointer to this new area.
Example (allocating space for an one dimensional array):
int i, row;
float *sum;
printf("Enter the row number: ");
scanf("%d", &row);
sum = (float *)malloc(row * sizeof(float));
for (i = 0; i < row; i++) {
sum[i] = i;
}
Example (allocating space for a two dimensional array):
int row, col;
float **zrand;
printf("Enter the row number: ");
scanf("%d", &row);
printf("Enter the Col number: ");
scanf("%d",&col);
zrand=(float **)malloc(row*sizeof(float *));
for (i=0; i< row; i++) {
zrand[i]=(float*)malloc(col*sizeof(float));
}
What is a function?
ex4.c
So far we have already seen some prepackaged functions
in C such as the printf(), scanf(), exp() and main() just to name
a few. C also allows us to write our own functions to define other specific
tasks.
Having functions allows the programmer to break
up a large computer problem into smaller more manageable pieces (or modules).
This allows for easier debugging, better manageability of your program
and easier reuse.
Example:
Consider the program in the previous class.
Function Prototypes
int comp(int src, int des);
This line from the beginning of the program are called function
prototypes. A function prototype tells the compiler the number
of arguments a specific function gets and what kind of value to expect
when that function is done executing. The compiler also uses to function
prototype to check that all calls to a specific function contain the correct
number of arguments and the correct argument types.
Calling Functions
When a program reaches the name of a function that function
is called or invoked. At this point program control is passed to that function.
After the function is done, either by reaching the end of all of
its statements or reaching a return,program
control is then passed back to the line occurring right after the call
to that function.
Pass by value and simulated pass by reference (very
important!!!)
All arguments that are passed to a function in C are
passed by value, that is a copy of the argument's value is made and passed
to the function. Using pass by value does not allow
the function to change the value of the original variable. Although
C allows arguments to be passed only by value we can simulate what is called
pass by reference. Pass by reference does allow for
a function to modify the value of the original variable. Simulated
pass by reference is done by using pointers. Note that in the program the
function modifies the original array sum.
Note: These web pages were written with the reference
text C How to Program by Deitel and Deitel.