You are on page 1of 45

Computer Programming for

Engineering Applications

ECE 175

Intro to Programming
Lecture Set Overview
Limitations of reference by value

The concept of pointers

Pointers in C

Functions and pointers

02/13/2021 ECE 175 2


Limitations of Reference by Value
What get printed by the program below?

#include <stdio.h>
#define PI 3.14
Reference by value does not
void circle(float r, float a, float c) allow the manipulation of
{ a = PI*r*r; // computation of area variables external to the
c = 2*PI*r; // computation of circumference function!!!
}

int main(void)
{
float r, a=0, c=0;
printf("Enter the radius of the circle:");
scanf("%f",&r);
circle(r, a, c); // arguments passed by value
printf("Area:%.1f Circumference:%.1f\n", a, c);
return(0);
}

02/13/2021 ECE 175 3


The Concept of Pointers

Pointer: a variable containing the address of another variable


A label used to refer (point) to memory space
Very frequently found in real life

138

Page numbers in a book, or powerpoint slides


Page numbers are pointers to the contents of the particular page

02/13/2021 ECE 175 4


The Concept of Pointers

Mailboxes at the post office

Numbers are used to label each mailbox.

The contents (letters) of a particular


mailbox can be retrieved by referring to
the mail box number.

02/13/2021 ECE 175 5


Pointers in C
Labels for referencing memory addresses

1 45

2 678

Address 3 3.36 Contents


4 ‘a’

5 112

6 5.83

02/13/2021 ECE 175 6


Why do we need pointers?
• Faster memory access and usage via memory referencing
Critical when C was developed
Still required for programming resource-constrained devices such as microcontrollers

• Saving memory space


No need to hold local copies of variables inside functions
Can pass values to a function by reference

• Downside
Pointers usually make program tracking/reading more complicated

02/13/2021 ECE 175 7


Declaration of pointers

Syntax: data type *name;

Examples: FILE *inp; // pointer to a file


int *p; // pointer to an integer variable
float *p; // pointer to a float variable

* is used to denote that a variable is a pointer

Though pointers can point to different data types, they are nothing more
than memory addresses
Compiler needs to know the data type for manipulating its contents

02/13/2021 ECE 175 8


Pointers cont.
There are two parts associated with a pointer:
- the address to the memory and
- the contents of the memory

P var

We can change both parts, that is,


- to which memory cell the pointer points (address), and
- the contents of the memory cell (value)

02/13/2021 ECE 175 9


To get the Address of a variable

Use the operator &

Syntax: pointer = &var;

Examples: int *p; // pointer to an integer


int i; // an integer variable
p = &i; // p points to variable i

The type of pointer and type of the variable must match

Example: if i is an integer variable, pointer p has to be of type integer

Pointer: A variable containing the address of another variable.

02/13/2021 ECE 175 10


Access content of a variable pointed to by a pointer

Use the indirection or deference operator *

Syntax: *pointer = value;


variable = *pointer;

Examples: int *p, i, var1; // pointer to an integer


p = &i; // p points to i
*p = 10; // memory cell pointed by p is
// assigned the value 10
var1 = *p; // variable var1 is assigned
//the contents of memory cell
//pointed by p

Again, the pointer type and the value or variable must match

02/13/2021 ECE 175 11


So Far we have used pointers
• To access files
• To scan for keyboard and file input

FILE *inp; // declaration of a pointer of type FILE

inp = fopen("filename", "r"); // returns the address to the memory where the file is stored

scanf("%d", &i); // scanf receives as input the address of variable i, and stores input in i

02/13/2021 ECE 175 12


A Simple Example Using Pointers
#include<stdio.h>

int main(void)
{
int var1 = 123, var2 = 456;
int *p; // declaration of a pointer of type integer
p= &var1; // initialize a pointer to address of var1
*p=10; // initialization of var1 via pointer p
var2 = *p+10; // var2 = var1 + 10

printf("%d %d %d\n", var1, var2, *p); // print the values of var1 and var2 and content
// where the pointer p points to

printf("%p %p %p\n", &var1, &var2, p); // printing addresses and value of p


return (0);
}

02/13/2021 ECE 175 13


At Declaration
int var1 = 123, var2 = 456;
int *p; // declaration of a pointer of type integer

p: 0x0110FABC var1 var2


123 456
Address: 0x0110FEC4 Address: 0x0110FEC8

38
Note: you will NOT get 0x0110FABC, 0x0110FEC4 or 0x0110FEC8 when you run your
program. These number are used only for this illustration.
02/13/2021 ECE 175 14
Changing the address of point

p= &var1; // initialization of pointer to address of var1

p: 0x0110FEC4 var1 var2


123 456
Address: 0x0110FEC4 Address: 0x0110FEC8

p now points to var1. It keeps the address of var1


The value of var1 has not changed!

02/13/2021 ECE 175 15


Changing the value pointed by point
*p=10; // initialization of var1 via pointer p

p: 0x0110FEC4 var1 var2


10 456

The value of var1 is changed to 10


Equivalent to var1 = 10;
May seem redundant, but we will see it is actually very useful

02/13/2021 ECE 175 16


Initialization of a variable via p
var2 = *p+10; // var2 = var1 + 10

p: 0x0110FEC4 var1 var2


10 20

02/13/2021 ECE 175 17


Printing the contents
printf("%d %d %d\n", var1, var2, *p);

*p prints the value pointed by the pointer p

Outcome (display): 10 20 10

p: 0x0110FEC4 var1 var2


10 20
02/13/2021 ECE 175 18
Printing the addresses
printf("%p %p %p\n", &var1, &var2, p); // printing addresses

Outcome (display): 0110FEC4 0110FEC8 0110FEC4

=> Prints the addresses of the variables var1 and var2 and the address where p points to
=> Observe value printed from &var1 and p
&var1 = 0x0110FEC4 and p = 0x0110FEC4

Int variables reserve 4 bytes (32-bit addressing).

p: 0x0110FEC4 var1 var2


123 456
Address: 0x0110FEC4 Address: 0x0110FEC8
02/13/2021 ECE 175 19
Looking at the memory

0x0110FEC4 10 var1

0x0110FEC8 20 var2

p
0x0110FABC 0x0110FEC4

02/13/2021 ECE 175 20


Activity 1 (10 mins): Fill in the blanks below (Soln)

Address Memory cell double num = 152.8, num1, num2;


10000 152.8 num
654 double *fp, *fp1, *fp2;
10008 fp = &num; //fp = 10000
79856
10010 fp1 = &num1; //fp1 = 10020
12243 fp2 = &num2; //fp2 = 10040
10018
87.65 num1
10020 *fp1 = 10.0; //num1 = 10.0
9532
10028 345.6 *fp2 = 16; //num2 = 16.0
10030 7890.5
num2 = *fp1 * num2; //*fp2 = 160.0
10038 65.21 num2 *fp = num2/4; // num = 40.0
10040 45.76
10048 34.21 fp = &num1;
fp1 = &num2;
10050
// fp = 10020 , *fp = 10.0
// fp1 = 10040 , *fp1 = 160.0
// fp2 = 10040 , *fp2 = 160.0

21
Exercise 1 (5 mins): Write Output of the following Program (Soln)

#include<stdio.h>

int main(void)
i = 40, j = 60, k = 120
{
int *ptr, i = 10, j = 20, k =30;

ptr= &i;
*ptr = 40;

ptr=&j;
*ptr += i;

ptr = &k;
*ptr = *ptr +k + j;

printf("i = %d, j = %d, k = %d", i, j, *ptr);


return (0);
}

02/13/2021 ECE 175 22


Common Programming Errors with Pointers
Find the errors in the following code segments:

#include <stdio.h> #include <stdio.h>


int main(void){ int main(void){
int *p; float *p;
printf("%d", *p); int i = 18;
return(0);
}
p = &i;
*p = 10.5;
return(0);
#include <stdio.h> }
int main(void){
int *p; Solution:
*p = 10; Two on the left: Segmentation Faults - Pointer p is
return(0); Uninitialized (not point to any variable yet)
}
One on the right:
Mismatch between pointer type and variable type

02/13/2021 ECE 175 23


Exercise 2 (6 mins) #include<stdio.h>
• Declare a pointer ptr that points to i and int main(void){
int i = 1;
• rewrite the loop using the pointer ptr // declare and initialize ptr
int *ptr;
ptr = &i;
#include<stdio.h> while (*ptr <= 10){
printf("%d ", *ptr);
(*ptr)++;
int main(void){ }
return 0;
int i = 1; }

// declare and initialize ptr

while (i <= 10){ // rewrite the loop using ptr (in place of i)
printf("%d ",i);
i++;
}
return 0;
}

02/13/2021 ECE 175 24


Re-visiting the circle function
Modify the program below such that the circle function can return c and a
Argument: the radius of a circle r
Output: circumference c and area a
#include <stdio.h>
#define PI 3.14
void circle(float r, float a, float c)
{ a = PI*r*r; // computation of area
c = 2*PI*r; // computation of circumference
}

int main(void)
{
float r, a=0, c=0;
printf("Enter the radius of the circle:");
scanf("%f",&r);
circle(r, a, c);
printf("Area:%.1f Circumference:%.1f\n", a, c);
return(0);
}

02/13/2021 ECE 175 25


Re-visiting the circle function (Soln)

#include<stdio.h>
#define PI 3.14

void circle(float r, float *p_a, float *p_c)


{ *p_a = PI*r*r; // computation of area
*p_c = 2*PI*r; // computation of circumference
}

int main(void)
{
float r, a=0, c=0;
printf("Enter the radius of the circle:");
scanf("%f", &r);
circle(r, &a, &c); // pass by pointer
printf("Area:%.1f Circumference:%.1f\n", a, c);
return(0);
}

02/13/2021 ECE 175 26


Exercise 1: What will get printed by the following program?
#include<stdio.h>
 
(Soln)
void fun1(int x, int *y)
{
x = x+(*y);
*y = 5*(*y);
x = (*y)*x;
}

int main(void)
{
int x = 2, y = 8;
fun1(x, &y);
printf("%d\t%d\n", x, y); 2 40
return (0);
}
02/13/2021 ECE 175 27
Activity 3:
• Part 1 (6 mins): Write a prototype for a function sum_n_avg that has three
double-type input parameters and two output parameters which are the
sum and average of the three input parameters

x1 sump
x2 sum_n_avg
x3 avgp

void sum_avg( )
{

void sum_avg(double x1, double x2, double x3, double *sump, double *avgp)
{
*sump = x1 + x2 + x3;
*avgp = (*sump) / 3;
}

How to call this function (i.e. from a main function)

02/13/2021 ECE 175 28


Activity 3 (cont.): How to call this function (i.e. from main function)
x1 sump
x2 sum_n_avg
x3 avgp
void sum_avg(double x1, double x2, double x3, double *sump, double *avgp)
{
*sump = x1 + x2 + x3;
*avgp= (*sump)/3;
} Sample code execution:
Enter 3 numbers: 25.6 47 99.5
sum and average are 172.10 and 57.37

int main(void){
double z1, z2, z3, sumz, avgz;
printf("Enter 3 numbers: ");
scanf("%lf%lf%lf", &z1, &z2, &z3);
sum_avg(z1, z2, z3, &sumz, &avgz);

printf("sum and average are %.2lf and %.2lf", sumz, avgz);

return 0;
}
02/13/2021 ECE 175 29
Activity 4: File I/O and function
Part a) (7 mins) Work as a group and write it on the whiteboard/worksheet

Given the “inputtext.txt” file, write a program that


- opens the text file,
- reads in (one character at a time) and display the statements given in the text file.
- Close the text file when finished

“inputtext.txt”
Every Day, We Change the World, but to Change the World in a Way that Means Anything, that
Takes More Time than Most People Have. It Never Happens all at once. It's SLOW. It's Methodical.
It's Exhausting. We don't All Have the Stomach for it.

02/13/2021 ECE 175 30


Solution
#include <stdio.h>

int main(void) {
FILE *inp;
char alp;
int total = 0;
inp = fopen("inputtext.txt", "r");
if (inp == NULL)
printf("no file found \n");
else {
while (fscanf(inp, "%c", &alp) != EOF) {
printf("%c", alp);
}
}
fclose(inp);

return 0;
}

02/13/2021 ECE 175 31


Activity 4
Part b) (10 mins) Work as a group and write the following user-defined function
on the white board and worksheet

void conversion(int *numalphabet, char *pl)

This function receives the address of numalphabet and address of letter (pl) and

- increments numalphabet by 1 if the letter is A-Z or a-z

- convert the letter from lowercase to UPPERcase or


from UPPERcase to lowercase

02/13/2021 ECE 175 32


Solution
void conversion(int *numletter, char *pl)
{
if (*pl >= 'A' && *pl <= 'Z') {
(*numletter)++;
*pl = *pl + 32;
}
else if (*pl >= 'a' && *pl <= 'z') {
(*numletter)++;
*pl = *pl - 32;
}
}

02/13/2021 ECE 175 33


Activity 4
Part c) (10 mins) Work as a group and write it on the whiteboard and worksheet
• From the “inputtext.txt” file, write a C program using your user-defined function
void conversion(int *numalphabet, char *pl)

- To count numbers of alphabets A- Z, a –z


- To convert lowercase to UPPERcase or UPPERcase to lowercase
- Display the statement and total number of alphabets.
Note: your program will read in one character at a time from a text file.

“inputtext.txt”
Every Day, We Change the World, but to Change the World in a Way that Means
Anything, that Takes More Time than Most People Have. It Never Happens all at once. It's
SLOW. It's Methodical. It's Exhausting. We don't All Have the Stomach for it.
Code execution
eVERY dAY, wE cHANGE THE wORLD, BUT TO cHANGE THE wORLD IN A wAY
THAT mEANS aNYTHING, THAT tAKES mORE tIME THAN mOST pEOPLE hAVE. iT
nEVER hAPPENS ALL AT ONCE.
iT'S slow. iT'S mETHODICAL. iT'S eXHAUSTING. wE DON'T aLL hAVE THE
sTOMACH FOR IT.

There are 185 alphabets in the text file.


02/13/2021 ECE 175 34
Solution
#include <stdio.h>

void conversion(int *numletter, char *pl);

int main(void) {

FILE *inp;
char alp;
int total=0;
inp = fopen("inputtext.txt","r");
if (inp == NULL)
printf("no file found \n");
else{
while (fscanf(inp, "%c", &alp) != EOF){
conversion(&total, &alp);
printf("%c", alp);
}
printf("There are %d alphabets in the text file\n", total);
}
fclose(inp);

return 0;
}

02/13/2021 ECE 175 35


Create a Common Fraction Calculator
 𝑛 1 𝑛2 𝑛
𝑜𝑝 =
𝑑1 𝑑2 𝑑
Problem Inputs:
Fraction 1: n1, d1
Operator: +, -, *,/
Fraction 2: n2, d2

Problem Outputs: n, d in a reduced form


Algorithms:
Addition: n = n1 * d2 + n2* d1, d = d1*d2
Subtraction: n = n1 * d2 - n2* d1, d = d1*d2
Multiplication: n = n1 * n2, d = d1*d2
Division: n = n1 * d2, d = d1*n2
Reduction: n = n /gcd(n, d), d = d/gcd(n, d)

02/13/2021 ECE 175 36


Sample Execution

Enter the nominator and the denominator of the fraction:1 8

Enter the operator:+

Enter the nominator and the denominator of the fraction:3 8

1/8 + 3/8 = 1/2

02/13/2021 ECE 175 37


Program read fraction 1
Structure (main)

read operator

read fraction 2

+ /
operator

add - * divide

subtract multiply

reduction
print
02/13/2021 ECE 175 38
Program
read fraction 1 1
Structure (main)
6
read operator

read fraction 2 **Call mul fn with


*Call add fn with
correct inputs
correct inputs
Into the function
Into the function + /
operator

*Call 2 - * **Call
add fn mul fn
*Call **Call 3
add fn mul fn

reduction 4 5
print
02/13/2021 ECE 175 39
Functions Declarations

#include<stdio.h>

void scan_fraction(int *p_n, int *p_d);

int gcd(int x,int y);

void add_fractions(int n1, int d1, int n2, int d2, int *p_n, int *p_d);

void multiply_fractions(int n1, int d1, int n2, int d2, int *p_n, int *p_d);

void reduce_fraction(int *p_n, int *p_d);

void print_fractions(int n, int d);

02/13/2021 ECE 175 40


Read Fraction function
Solution
void scan_fraction(int *p_num, int *p_den)
{
printf("Enter the nominator and the denominator of the fraction:");
scanf("%d%d", p_num, p_den);
}

Addition Function
void add_fractions(int n1, int d1, int n2, int d2, int *p_n, int *p_d)
{
*p_n = (n1 * d2) + (n2 * d1);
*p_d = d1 * d2;

}
 𝑛 𝑛 1 𝑛 2 𝑛 1 𝑑 2+ 𝑛 2𝑑 1
= + =
𝑑 𝑑1 𝑑2 𝑑1𝑑 2
02/13/2021 ECE 175 41
Multiplication
void multiply_fractions(int n1, int d1, int n2, int d2, int *p_n, int *p_d)
{
*p_n = n1 * n2;
*p_d = d1 * d2;  𝑛𝑛1 𝑛2 𝑛1𝑛2
} = ∗ =
𝑑 𝑑1 𝑑 2 𝑑1𝑑 2
Reduction
void reduce_fraction(int *p_n, int *p_d)
{ Solution
int gd = gcd(*p_n, *p_d);
*p_n = (*p_n) / gd;
*p_d = (*p_d) / gd;

}
Reduction: n = n /gcd(n, d), d = d/gcd(n, d)
Function prototype for gcd: int gcd(int x, int y);

02/13/2021 ECE 175 42


Greatest Common Denominator (GCD) Function
int gcd(int x, int y)
{
int rem = 1, gcd;
while (rem!=0) // when rem = 0, the loop terminates
{
if (y == 0) {
gcd = x;
rem = 0;
}
else{
gcd = y;
rem = x%y;
x = y;
y = rem;
}
}
return gcd;
}

02/13/2021 ECE 175 43


Print Function
void print_fractions(int n, int d)
{
if (n == 0)
printf("0");
else if (d == 0)
printf("NaN");
else if (d == 1)
printf("%d", n);
else
printf("%d/%d",n, d);

02/13/2021 ECE 175 44


int main(void){
int n1, d1, n2,d2, n, d;
char op; Main function
scan_frac(&n1, &d1); // read the first fraction
printf("Enter the operator:");
scanf("%*c%c", &op);
scan_frac(&n2, &d2); // read the second fraction
switch(op){
Solution
case '+':
add_fractions(n1, d1, n2, d2, &n, &d);
break;
case '-':
add_fractions(n1, d1, -n2, d2, &n, &d);
break;
case '/':
multiply_fractions(n1, d1, n2, d2, &n, &d);
break;
case '*':
multiply_fractions(n1, d1, d2, n2, &n, &d);
break;
default:
printf("This is not a valid operator");
}
reduce_fraction(&n, &d); // reduce the fraction
print_fractions(n1, d1); // print fraction
printf(" %c ", op);
print_fractions(n2, d2); // print fraction
printf(" = ");
print_fractions(n, d); // print fraction
printf("\n");
02/13/2021
return(0); ECE 175 45
}

You might also like