You are on page 1of 15

Design and Analysis of Algorithms 214.

Introduction to the C Programming


Language.
Pointers, Quick sort Algorithm.

U.U.Samantha Rajapaksha
B.Sc.(Eng.)

Sri Lanka Institute of Information Technology.


Samantha.r@sliit.lk
0112301904

DAA 214 Sri Lanka Institute of Information Technology.


Pointers.
A pointer is a variable whose value is an address.

int thing; /* define a thing */


int *thing_ptr; /* define a pointer to a thing */

 There are things and pointers to things. Knowing the difference between the two is
very important.
 The address of thing is 0x1000. Addresses are automatically assigned by the C
compiler to every variable. Normally, you don't have to worry about the addresses
of variables, but you should understand that they're there.
 Our pointer (thing_ptr) points to the variable thing. Pointers are also called
address variables because they contain the addresses of other variables. In this
case, our pointer contains the address 0x1000.

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Pointer Operator Syntax.
 The operator ampersand (&) returns the address of a thing which is a
pointer. The operator asterisk (*) returns the object to which a pointer
points. These operators can easily cause confusion.

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


These pointer operations.

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Pointers as Function Arguments.
 When you call a function in C and you pass it some arguments, those
arguments are passed by copy.
 This means that the formal arguments in the function definition are actually
copies of the actual arguments in the function call. They live at different
addresses than the originals.
 Pass by copy is also known as:
 pass by value;
 call by copy;
 call by value
 pass by copy means that changing the value of the copy doesn’t change the
value of the original.
 Is there a way to pass an argument so that, in the function, we can change the
value of the formal argument, and that’ll change the value of the actual
argument in the call?
 Yes: pass by reference.

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Pass by Reference.
 Pass by reference means that, instead of passing a copy of the actual
argument, you pass the address of the actual argument.
 If we can pass the address, then we can modify the value of the variable
that lives at that address.
 Eg.

#include <stdio.h>
void inc_count(int *count_ptr)
{
(*count_ptr)++;
}

int main() {
int count = 0; /* number of times through */
while (count < 10)
inc_count(&count);
return (0);
}

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Call of inc_count.

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


const Pointers
 Declaring constant pointers is a little tricky. For example, the
declaration:
const int result = 5; tells C that result is a constant so that:
result = 10; /* Illegal */ is illegal.
 The declaration:
const char *answer_ptr = "Forty-Two";
does not tell C that the variable answer_ptr is a constant. Instead, it
tells C that the data pointed to by answer_ptr is a constant. The data
cannot be changed, but the pointer can.
 In C this is:
answer_ptr = "Fifty-One"; /* Legal (answer_ptr is a variable) */
*answer_ptr = 'X'; /* Illegal (*answer_ptr is a constant) */

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


const Pointers
 If we put the const after the * we tell C that the pointer is constant.
 For example:
char *const name_ptr = "Test";
 What's name_ptr? It is a constant pointer. Can it be changed? No.
What does it point to? A character. Can the data we pointed to by
name_ptr be changed? Yes.

name_ptr = "New"; /* Illegal (name_ptr is constant) */


*name_ptr = 'B'; /* Legal (*name_ptr is a char) */

 Finally, we can put const in both places, creating a pointer that cannot
be changed to a data item that cannot be changed:

const char *const title_ptr = "Title";

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Functions
 A C program is made up of one or more functions, one of which is
main( ).
 Execution always begins with main( ), no matter where it is placed in
the program. By convention, main( ) is located before all other
functions.
 When program control encounters a function name, the function is
called (invoked).
 Program control passes to the function.

 The function is executed.

 Control is passed back to the calling function.

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Sample Function Call
#include <stdio.h>

int main ( ) printf is the name of a predefined


{ function in the stdio library

printf (“Hello World!\n”) ; this statement is


return 0 ; is known as a
} function call
this is a string we are passing
as an argument (parameter) to
the printf function

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Programmer-Defined Functions
 Programmers can write their own functions.
 C function names follow the same naming rules as C variables.
 All non-trivial programs use functions.
 You define all the parts of a programmer-defined function, the
name, the behavior, the parameters (or arguments) and the
return type.

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Sample Programmer-Defined Function
#include <stdio.h>

void printMessage ( void ) ;

int main ( void )


{
printMessage ( ) ;
return 0 ;
}

void printMessage ( void )


{
printf (“A message for you:\n\n”) ;
printf (“Have a nice day!\n”) ;
}

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Examining printMessage
#include <stdio.h>

void printMessage ( void ) ; function prototype

int main ( void )


{
printMessage ( ) ; function call
return 0 ;
}

void printMessage ( void ) function header


{
printf (“A message for you:\n\n”) ; function
printf (“Have a nice day!\n”) ; body
}

function definition
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
Exercise
 Write a program to read a set of numbers and store them on an array.
 Write function named as partition to divide the array into two parts according to the partition
point.
 Call the function from the main program.

PARTITION(A, p, r)

1 x ← A[r]
2i←p-1
3 for j ← p to r - 1
4 do if A[j] ≤ x
5 then i ← i + 1
6 exchange A[i] ↔ A[j]
7 exchange A[i + 1] ↔ A[r]
8 return i + 1
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.

You might also like