Let’s try some example problems using pointers!
#include <stdio.h>
int main(void)
{
int x = 7;
int *pointer = &x;
= 10;
x *pointer = 75;
("x = %d\n", x);
printf
return 0;
}
#include <stdio.h>
int main(void)
{
int n = 43;
("%d\n", *&*&*&*&*&*&n);
printf
return 0;
}
#include <stdio.h>
// This function adds 1 to an integer.
void change_number(int n)
{
++;
n}
int main(void)
{
int x = 6, y = 9;
("Before:\n");
printf("x is %d\n", x);
printf("y is %d\n", y);
printf
(x);
change_number(y);
change_number
("After:\n");
printf("x is %d\n", x);
printf("y is %d\n", y);
printf
return 0;
}
#include <stdio.h>
// This function adds 1 to an integer.
void change_number(int *n)
{
*n++;
}
int main(void)
{
int x = 6, y = 9;
("Before:\n");
printf("x is %d\n", x);
printf("y is %d\n", y);
printf
(&x);
change_number(&y);
change_number
("After:\n");
printf("x is %d\n", x);
printf("y is %d\n", y);
printf
return 0;
}
#include <stdio.h>
// This function adds 1 to an integer.
void change_number(int *n)
{
(*n)++;
}
int main(void)
{
int x = 6, y = 9;
("Before:\n");
printf("x is %d\n", x);
printf("y is %d\n", y);
printf
(&x);
change_number(&y);
change_number
("After:\n");
printf("x is %d\n", x);
printf("y is %d\n", y);
printf
return 0;
}
swap()
function to swap the values
referenced by two pointers.#include <stdio.h>
void swap(int *a, int *b)
{
// Your code here
}
int main(void)
{
int num, ber;
= 68;
num = 70;
ber
("%d %d\n", num, ber);
printf(&num, &ber);
swap("%d %d\n", num, ber);
printf
return 0;
}
#include <stdio.h>
int functiomatic(int *n)
{
*n = *n + 1;
return *n + 1;
}
int main(void)
{
int b;
int *c;
= 17;
b = functiomatic(&b);
b = &b;
c
("*c = %d\n", *c);
printf
return 0;
}
#include <stdio.h>
int functiomatic(int n)
{
int m = n + 1;
return n;
}
int main(void)
{
int b;
int *c;
= 17;
b = functiomatic(b);
b = &b;
c
("*c = %d\n", *c);
printf
return 0;
}
array
contain after line 13? What will this
program print if executed? Assume that any uninitialized variables
contain garbage values.#include <stdio.h>
#define LENGTH 10
int main(void)
{
int array[LENGTH];
// Initialize array
for (int i = 0; i < LENGTH; i++)
{
array[i] = i * 10;
}
// What does this array now contain?
// Print array
for (int i = 0; i < LENGTH; i++)
{
printf("%d\n", *(array + i));
}
return 0;
}
array
contain after line 34? What will this
program print if executed? Assume that any uninitialized variables
contain garbage values.#include <stdio.h>
#define LENGTH 10
void initialize_array(int *arr, int length)
{
for (int i = 0; i < length; i++)
{
arr[i] = 50 + i;
}
}
void print_array(int *arr, int length)
{
printf("[");
for (int i = 0; i < length; i++)
{
printf("%d", arr[i]);
if (i < length - 1)
{
printf(", ");
}
}
printf("]\n");
}
int main(void)
{
int array[LENGTH];
initialize_array(array, LENGTH - 2);
// What does this array now contain?
print_array(array, LENGTH);
return 0;
}
Create a declaration of a struct called car
that
contains some information about a car: a string (char pointer) called
make
that indicates its manufacturer, a string called
model
that indicates its model, an integer called
year
that indicates its year of production, and a double
called mpg
that indicates its fuel efficiency in miles per
gallon.
Rewrite the following line of code using the dereference
(*
) and the direct member access (.
)
operator.
->number = 5; my_struct
print_planet()
function? How can you fix it?struct planet
{
char name[30];
double radius_km;
char color[30];
};
void print_planet(struct planet p)
{
("Name: %s\n", p->name);
printf("Radius: %lf\n", p->radius_km);
printf("Color: %s\n", p->color);
printf}
#include <stdio.h>
struct pizza
{
int radius_inches;
double price;
char toppings[100];
};
void initialize_pizza(struct pizza *p)
{
->radius_inches = 0;
p->price = 0;
p->toppings[0] = '\0';
p}
void print_pizza(struct pizza p)
{
("Radius: %d inches\n", p.radius_inches);
printf("Price: $%0.2lf\n", p.price);
printf("Toppings: %s\n", p.toppings);
printf}
int main(void)
{
struct pizza four_meat_pizza;
(&four_meat_pizza);
initialize_pizza(four_meat_pizza);
print_pizza
return 0;
}
#include <stdio.h>
#include <string.h>
struct pizza
{
int radius_inches;
double price;
char toppings[100];
};
void initialize_pizza(struct pizza *p)
{
->radius_inches = 0;
p->price = 0;
p->toppings[0] = '\0';
p}
void print_pizza(struct pizza p)
{
("Radius: %d inches\n", p.radius_inches);
printf("Price: $%0.2lf\n", p.price);
printf("Toppings: %s\n", p.toppings);
printf}
int main(void)
{
struct pizza four_meat_pizza;
(&four_meat_pizza);
initialize_pizza
.radius_inches = 13;
four_meat_pizza.price = 9.99;
four_meat_pizza(four_meat_pizza.toppings, "Pepperoni, beef, sausage, and bacon");
strcpy
(four_meat_pizza);
print_pizza
return 0;
}
struct gradebook
{
char student[128];
int grades[10];
double avg;
};
typedef struct gradebook gradebook;
int main(void)
{
int n = 3;
[n];
gradebook cop3223
// Your code goes here
}
struct gradebook
{
char student[128];
int grades[10];
double avg;
};
typedef struct gradebook gradebook;
void calculate_student_avg(gradebook *class, _____)
{
double sum;
for (int i = 0; i < _____; i++)
{
= _____;
sum for (int j = 0; j _____ 10; j++)
{
+= class[i].grades[_____];
sum }
[i].avg = sum / _____;
class}
}
void print_averages(_____, _____)
{
for (int i = 0; _____; i++)
{
("Average for _____ is %lf\n", _____, _____[i].avg);
printf}
}
int main(void)
{
int n = 3;
[n];
gradebook cop3223
// Filling in student grades with data
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 10; j++)
{
[i].grades[j] = (i + 1) * (j + i);
cop3223}
}
// Add student names
(cop3223[0].student, "Frances E. Allen");
_____(__________________, "Barbara Liskov");
_____
_____________________________________________// Find average
(_____, n);
calculate_student_avg
// Print average
(_____, _____);
print_averages
return 0;
}
deposit
and withdraw
functions don’t work. How can I solve this?struct balance
{
int dollars;
int cents;
double total;
};
void deposit(struct balance *checking, double amount)
{
(&checking).dollars += amount;
}
void withdraw(struct balance *checking, double amount)
{
*checking.dollars -= amount;
}
int main(void)
{
struct balance my_checking;
(&my_checking, -100);
deposit(&my_checking, 1E6);
withdraw
(&my_checking, 145.54);
deposit(&my_checking, 0.01);
withdraw
return 0;
}