Module 3 - Notes
Module 3 - Notes
Strings
Pointers
Functions
Programming in C III
Strings Strings
Character arrays
String initialization
• Strings are 1D arrays of characters
By reading in a value
• Example:
• Strings must be terminated by the null character '\0'
which is (naturally) called the end-of-string char name[34];
character. scanf("%s",name);
• Strings must be declared before they are used like
any other variables in C.
• Unlike other 1D arrays the number of elements set
for a string set during declaration is only an upper
limit.
• Initializing a string can be done in three ways: 1) at
declaration, 2) by reading in a value for the string,
and 3) by using the strcpy function. 3 4
Strings Strings
String initialization
String I/O special functions
By strcpy function – gets( string_name );
• To use the strcpy function be sure to include the – puts( string_name );
string.h header file
• Example: – Example:
char a_string[100];
#include <string.h>
printf("Please enter a sentence\n");
main ()
gets(a_string);
{
puts(a_string);
char job[50];
– gets function reads in a string from the
strcpy(job,"Professor");
keyboard
printf("You are a %s \n",job);
5
– puts function displays a string on the monitor 6
}
Strings Pointers
More string functions
A pointer is a variable that stores the memory
address of another variable as its value.
A pointer variable points to a data type (like int)
of the same type, and is created with the *
operator.
Pointers contain Memory Addresses, Not Data
Values
7 8
Pointers Pointers
Pointers enable us to
Declaring a simple variable, like
– effectively represent sophisticated data structures int i;
9 10
Pointers Pointers
You can find out the memory address of a variable by
Like all other C variables, pointers must be
simply using the address operator &. declared before they are used.
Example: &v
Example: Pointer declaration
The above expression should be read as “address of v”,
and it returns the memory address of the variable v. int *p;
Example: float *y;
#include <stdio.h>
The prefix * defines the variable to a pointer.
main() {
float v;
In the above example, p is the type “pointer to
integer” and y is the type “pointer to float”.
v=5.17;
printf("The value of v is %f\n",v);
printf("The address of v is %X\n",&v); }
11 12
Pointers Pointers
Once a pointer has been declared, it can be assigned
Consider the following example which returns the
an address. This is usually done with the address contents of the address stored in a pointer variable.
operator.
Example: Using a Pointer
Example: Pointer declaration #include <stdio.h>
int *p; main() {
13 14
Functions Functions
A function in C is a small “sub-program” that
To use functions, the programmer must do three
performs a particular task things
It is a block of code which only runs when it is – Define the function
called – Declare the function
Why functions? – Use the function in the main code.
15 16
Functions Functions
Function definitions have the following syntax:
Example: a function that calculates n!
int factorial (int n)
{
int i,product=1;
for (i=2; i<=n; ++i)
– return_type in the function header tells the type of the product *= i;
value returned by the function (default is int)
return product;}
– data type variable name list tells what arguments the
function needs when it is called (and what their types are)
– local declarations in the function body are local
constants and variables the function needs for its
calculations.
17 18
Functions Functions
Some functions will not actually return a value or need any
A function returns a value to the calling program with the use of
arguments. For these functions the keyword void is used. the keyword return, followed by a data variable or constant
value.
Example:
void write_header(void) {
Example:
printf("Last Modified: "); return 3;
printf("12/04/95\n"); return n;
} return ++a;
return (a*b);
The 1st void keyword indicates that no value will be returned
The data type of the return expression must match that of the
The 2nd void keyword indicates that no arguments are needed
declared return_type for the function.
for the function.
This makes sense because all this function does is print out a
header statement.
19 20
Functions Functions
Using a function is known as calling or invoking a function
On implementing functions in C, two variables may be used;
Local variables and Global variables
Example 1: Using our previous factorial program
ans = factorial(9);
Local variables
– They exist and their names have meaning only while the function is being
Example 2: To invoke our write_header function executed.
write_header(); – They are unknown to other functions.
When your program encounters a function invocation, control – When the function is exited, the values of automatic variables are not
passes to the function. retained.
When the function is completed, control passes back to the main – If a local variable has the same name as a global variable, only the local
variable is changed while in the function. Once the function is exited, the
program. global variable has the same value as when the function started.
Global variables
– Is declared at the beginning of a program outside all functions
– Can be accessed and changed by any function in the program
21 22
Functions Functions
Example : Function in a program
Function prototypes are used to declare a function so that it can be used in a
program before the function is actually defined.
#include <stdio.h>
int factorial (int n)
Example 1: Consider the previous program using a function prototype
#include <stdio.h>
{
int factorial (int n); // A function prototype
int i,product=1;
int main() {
for (i=2; i<=n; ++i) int x,ans;
product *= i; printf (“Enter a number: ”);
} product *= i;
23 24
return product;}
Functions Functions
Recursion
Function call
– Recursion is the process in which a function repeatedly calls – Functions can be invoked in two ways: Call by Value or Call
itself to perform calculations. by Reference.
– Example: Consider the previous factorial function using – Actual parameters are the values passed to a function during
recursion a function call, whereas formal parameters are the variables
int factorial(int n) { declared in the function definition that receive these values
int result; – Also, they may be referred to as arguments and parameters.
if (n<=1) Arguments meaning actual parameters and parameters
result=1; meaning formal parameters.
else
result = n * factorial(n-1);
return result;
}
25 26
Functions Functions
Difference between the Call by Value and Call by Reference
Example: Call by Value
#include <stdio.h>
int main()
// Pass by Values
return 0; }
int t;
t = x;
x = y;
y = t;
27 28
Functions Functions
In call by value method of parameter passing, the values of
Example: Call by Reference
actual parameters are copied to the function’s formal parameters. #include <stdio.h>
– One is the original copy and the other is the function copy. int a = 10, b = 20;
// Pass Reference
– Any changes made inside functions are not reflected in the swapx(&a, &b); // Actual Parameters
return 0; }
int t;
t = *x;
*x = *y;
*y = t;
29 30
Functions
In call by reference method of parameter passing, the address of
the actual parameters is passed to the function as the formal
parameters.
– Both the actual and formal parameters refer to the same
locations.
– Any changes made inside the function are actually reflected in
the actual parameters of the caller.
31