Question: Part 1 : Memory Addresses and Pointers - Create the makefile: Create your makefile ( lab 7 / makefile ) in a similar fashion as

Part 1: Memory Addresses and Pointers
- Create the makefile:
Create your makefile (lab7/makefile) in a similar fashion as previous makefiles you've created. Experiment with implicit rules and MACROS. Note: if you set a makefile macro "CFLAGS" to have the -I compiler flag for finding lab7.h in the include directory, then the implicit rules for making a .o from a .c will work.
```
CFLMGE =-I ./include
lab].a: lab7.c ./include/lab7.h \leftarrow just include this
* don't neod to explicitly specify the action
```
- In all your source code, including makefiles:
- Include your name, class section \# and current semester as lines as a comment at the top of your files.
- Include a clean target in your makefile which removes the appropriate files.
- In all .c files, display allerror message to stderr (standard error) rather then stdout. - Hint: use fprintf(stderr,"*",...) or perror(\({}^{\text {mII }}\)) which will print the message provided and the current value of errno.
- Create include/lab7.h
An "include file" which has declarations for external functions used in lab7.c. Add the external declarations and any pre-processor macros as needed.
- Create lab7.c - Contains the main(), calls various functions
Include a main function in lab7.c which accepts a single integer on the command line.
In this function
- Accept the command line parameter (a string in argv[]) and convert it to an int (see man of atoi) and store the converted value in an integer named num. (Hint: int num = atoil(argv[1]);)
- Include a check that at least one command line argument was provided and successfully converts to an int via atoi()
- Create an array of ints, initialize the array elements to: 10,34,56,65,32
- Before performing each of the 3 parts of this Lab, print the following with 2 leading new lines to separate the output from each section, where \(\mathbf{n >}\) is the Part number
Part =mez=e=e==
- Create part1.c fille:
Includes 1 function:
void callByRef (int *intPtr, int n , int intArray[1)
Parameters:
- intPtr - a pointer to an int,
-\(\mathbf{n}\)- the number of elements in the array of integers
- intArray - the array of integers This function should do the following:
1. Update the first parameter's value (intPtr) to 99
2. Update the \(3^{\text {3nd }}\) element in the array (intArray[]) to 99
3. Run through the intArray backwards (i.e. from the last element to the first), using an int pointer to address each element. Print the following for each element in the array:
Array element is is the array position for the element, and is the value of the element in the array at that position. Indent each line with 1 tab
- Back in your main() function: call the function callByRef() with the three parameters: \&num, array size" and intArray
- Before calling callByRef(), print the following line to the screen:
Before callByRef(), num is and Array is: list of intArray elements>
- After returning from the call to callByRef(), print the following line:
After callByRef(), num is and Array is: list of intArray elements>
"Hint, you can hardcode the array size since we are hardcoding the elements of intArray
- Example Output for Part 1:
```
Part l ==========
Before callByRef(), number is 55 and Array is: 1034566532
Array element 4 i.s 32
Array element $ is &5
Mrray element 2 i.s 99
Array element 1 i.s 34
Array element 0 is 10
Mfter callByRefll, number is 99 and Mrray is: 1034996532
```
Part 2: Dynamic Memory Allocation - add the use of dynamic memory allocation
Create a new function called "allocit[0" which takes 2 integer values and does not return a value.
Within allocit(), allocate an amount of memory equal to the smaller of the 2 provided numbers to store data of type char.
Use a loop to fill the allocated data with the letter "2"(Hint, think of the allocated memory as an array of chars.
After filling the array, print the char stored in the beginning of the allocated memory using the printf format string: "The first char is \%\(\mathbf{c}\backslash \mathbf{n}^{\prime \prime}\)
Then, use the realloc() function to reallocate the amount of memory equal to the larger of the 2 provided numbers.
Part 3: Function Pointers - Add the use of function pointers.
Create part3.c file which contains two functions, each function accepts an int and a char as parameters and returns an int. Name them fun1 and fun2.
Have fun10) print the following message to stderr:
In fun1(), got and 'echar param>'
Have fun2() print the following message to stderr:
In fun2(), got and ''
In your main(), call fun1(num,'\( E^{\prime \prime}\)) if the value passed in on the command line is even, and call fun2(num,'O') if it is odd. L.e. you will call either fun1() or fun2(). You will have to determine if the number provided is even or odd.
Use function pointers and a Ternary operator to determine which to call. Have fun10 return 1 and fun2() return 2.
Have main print the following line after calling the appropriate function:
Function returned
where cretVal> is the return value from fun10/fun 2, whichever was called
Do not add a newline to that line, let
Part 1 : Memory Addresses and Pointers - Create

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!