6 Pointers Function Passbypointer WSoln
6 Pointers Function Passbypointer WSoln
Engineering Applications
ECE 175
Intro to Programming
Lecture Set Overview
Limitations of reference by value
Pointers in C
#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);
}
138
1 45
2 678
5 112
6 5.83
• Downside
Pointers usually make program tracking/reading more complicated
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
P var
Again, the pointer type and the value or variable must match
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
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
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
Outcome (display): 10 20 10
=> 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
0x0110FEC4 10 var1
0x0110FEC8 20 var2
p
0x0110FABC 0x0110FEC4
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;
while (i <= 10){ // rewrite the loop using ptr (in place of i)
printf("%d ",i);
i++;
}
return 0;
}
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);
}
#include<stdio.h>
#define PI 3.14
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);
}
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;
}
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);
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
“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.
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;
}
This function receives the address of numalphabet and address of letter (pl) and
“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.
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;
}
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
*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 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);
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);