Starting from:

$25+

CPSC1021 -LAB2 - Solved

 
Fun with Pointers

 
 
Lab Objective
●       Build familiarity with double pointers ● Understand how strings work in C.  

●       Practice passing data by reference

●       Working with multiple files

Introduction
The purpose of this lab is to continue to build familiarity with pointers. In this lab, we will use a double pointer to hold a first and last name. You will then print how long the first and last name is. Lastly, you will print the first and last name in reverse.  

 

You are required to implement the functions as they are given. While you are working through the various steps, you are free to comment out the functions you have not implemented, so you can compile and test your program as you work your way through the lab.  

 

             

Step 1: Allocate Space for 2D Pointer
 

            To begin this lab, you need to malloc space for a 2D pointer. The size of your 2D pointer will 2 X 100. Each row of columns will hold a name. The first index of the pointer will hold the first name and the second will hold the last name. Once you have properly allocated space for the pointer, ask the user for input and store the input into the arrays.  

 

Match this format:

 

 

 

             

Step 2: Implement the SizeOfName() function.  

 

This function should parse through the text and grab the length of the first name and the last name. You should recall that scanf will add a null character at the end of a captured string.  

 

When you have calculated the length of a string, pass these variables by reference to printSizeOfName() function. Inside this function, you should print how long their first and name is. Match the following output:

 

 

 


Final Step: Implement reverseString() Function 
 

Now that you have the functions above implemented and working, your last step is to implement the final function to print the string in reverse order. Here is an example of what the final program should look like.  

 

 

 

 

Other Requirements: 

 

In this lab you will be introduced to the use of multi-file programs. We have provided you with main.c. Using the information from this file you will produce 3 files, described below:

1.      main.c – This is the driver file. The “main” function will be implemented in this file.

2.      functions.h – You will need to move the function prototypes to this file. You are also required to use header guards in this file. Below I have provided a information about header guards and why you need them.

3.      functions.c – You will implement the functions in this file.   

 

 

Multiple Files and Header Guards 
 

At this point you should have already encountered multi-file programs. For those of you that have not we will give you a quick explanation of multiple files and header guards.  

 

The early years of computer programming, where programs were relatively small compared to programs today, is long gone. Writing all code in the driver file does not exhibit good programming practice. It is important that you get experience developing multi-file programs.  

 

Rather than putting your function prototypes in the driver file above the main, you will need to put the function protypes in the functions.h file. So, where do you think the function implementation should go? If you are thinking functions.c, you would be correct. Now the next question you should be considering, is how does the driver program (main function) or “Mr. Compiler” know where the function prototypes live. Well we are going to tell them using a #include statement. The syntax of this is as follows:  

 

#include “functions.h” 

This statement should be put in the main and any “.c” file that will use the functions listed in the file.

 

Since we are providing the implementation of our functions in the functions.c file we need to also include the functions.h in the functions.c file.  

 

While working with multi-file programs has many advantages, there is one issue that needs to be addressed. It is not uncommon that a file could be included in another file multiple times. As an example, suppose we include the following in main.c:  

 

#include <stdio.h>

#include “functions.h”

#include “anotherFunctions.h”

 

Now suppose anotherFunctions.h includes functions.h

 

By including functions.h and anotherFunctions.h in your main you have inadvertently included functions.h twice. Which could cause a nasty compile error. This can be avoided by using header guards in your “.h” files.

 

From the following website:  

https://www.learncpp.com/cpp-tutorial/header-guards/ 

 

Header guards are conditional compilation directives that take the following form:

#ifndef  SOME_UNIQUE_NAME_HERE

#define SOME_UNIQUE_NAME_HEAR

 

// YOUR PROTOTYPES GO HERE

 

#endif

 

#ifndef stands for if not defined then you should define this file as …. #endif means this is the end of the if statement

 



 


 


 

 

More products