Excercises
Concepts Questions
-
Pointer Declaration and Interpretation: Explain what each of the following pointer declarations means and what type of data each pointer can reference:
int* ptrA;
char* ptrB;
struct Student* ptrC; -
Address-of Operator: Given the following code:
int age = 25;
int* ptrAge = &age;Using words and diagrams, explain what
&agerepresents and why we need the address-of operator when assigning to a pointer variable. -
Pass by Value vs. Pass by Address: Explain the difference between pass by value and pass by address. Why is pass by address preferred when passing structs to functions?
-
Arrow Operator: Given the following struct and pointer:
struct Book
{
char title[100];
char author[50];
int pages;
float rating
};
...
struct Book mybook;a) modify the above code to initialize mybook with information about your favorite book b) declare a function named
printBookthat will receive the address of a Book struct and returns nothing. c) define the function so that it will print the book being passed to the function using the following format, replacing<datamember>with the data member indicated:<author> - <title> - <pages>: <rating>d) show how to call printBook so that mybook will be printed
-
Dereference Operator: Explain what the dereference operator (
*) does and provide an example of how it is used to modify a variable through a pointer. -
Providing Multiple results: Explain why pointers are useful for providing multiple results from a function. Give an example of a situation where this would be necessary.
-
const with Pointers: Explain the purpose of using
constwhen declaring pointer parameters in a function. What doesconst struct Student* studenttell you about how the function will use the pointer?
Walkthrough
a) Using tables to track your variables and function calls, show how you would go through the execution of this program. b) What is the exact output of the following program?
#include <stdio.h>
struct Plant
{
int daisy;
int rose;
};
void grow(struct Plant* p, int dew)
{
if (p->daisy > 10)
{
p->daisy = p->daisy + dew;
p->rose = p->rose + 1;
}
}
int main(void)
{
struct Plant flower = {15, 0};
int i;
printf("Initial: daisy=%d, rose=%d\n", flower.daisy,
flower.rose);
for (i = 0; i < 3; i++)
{
grow(&flower, 5);
}
printf("Final: daisy=%d, rose=%d\n", flower.daisy,
flower.rose);
return 0;
}
Debugging
The following program is supposed to swap two integer values using the swap function. However, it contains a number of errors.
#include <stdio.h>
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
int main(void)
{
int x = 10;
int y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(x, y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
- Identify the logical error in the above code.
- Explain why the swap does not work as intended.
- Rewrite the corrected program using pointers to properly swap the values.
Programming
Problem 1: Working with Structs
a) Define a struct named Rectangle that stores:
- a width (floating point)
- a height (floating point)
Write the following functions:
b) Define a function named area. This function is passed the address of a rectangle and returns it's arrea
c) Define a function named scale. This function is passed the address of a rectangle and scales the rectangle by multiplying its height and width by factor. Nothing is returned by the function
Problem 2: Height Converter
Write a function named convertHeightthat accepts the height of a person in centimeters (a floating point value) and passes back their height in feet and inches. inches should be expressed to the nearest inch (rounding down).
Here is the information for convertion:
1 inch == 2.54 cm
12 inch == 1 foot