.. | ||
assets | ||
src | ||
.clang-format | ||
.gitignore | ||
Assignment.pdf | ||
CMakeLists.txt | ||
README.org |
- Assignment 1
- Source Code
- Running the Programs
- Program Outputs and Question Solutions (C's)
- C1 (Creation of a new file)
- C2 (Opening and closing an existing file)
- C3 (Writing to a file)
- C4 (Reading from file)
- C5 (Moving to a specific location in a file (end of file))
- C6 (Computes string's length and copies one string into another string)
- C7 (Concatenates(joins) two strings)
- C8 (Compares two strings)
- C9 (Converts String to lowercase)
- C10 (Converts string to uppercase)
- C11 (Dynamic Memory with Malloc)
- C12 (Dynamic Memory with Calloc)
- C13 (Describe the difference between
malloc
andcalloc
) - C14 (Explain pointers and pointers to pointers using code)
Assignment 1
ABC123: zfp106
Name: Price Hiller
Course: CS2124
Section: 0C3
Semester: Spring 2024
Source Code
The full source code for this project can be found at https://git.orion-technologies.io/Price/college/src/branch/Development/Spring-2023/CS-2124/Assignment-1
Running the Programs
- Install cmake version 3.25 or greater.
- Ensure you have recent version of
make
at the time of writing. This project successfully compiles withGNU make
version4.4.1
. - Go the directory with
CMakeLists.txt
and runcmake .
to generate a Makefile. - Run
make all
to compile all the programs. - Go into the newly created
bin
directory where all the compiled programs will be output to. - Run the programs in sequence, from
C1
toC12
to ensure the file operations work as expected.
Program Outputs and Question Solutions (C's)
C1 (Creation of a new file)
C2 (Opening and closing an existing file)
C3 (Writing to a file)
C4 (Reading from file)
C5 (Moving to a specific location in a file (end of file))
C6 (Computes string's length and copies one string into another string)
C7 (Concatenates(joins) two strings)
C8 (Compares two strings)
C9 (Converts String to lowercase)
C10 (Converts string to uppercase)
C11 (Dynamic Memory with Malloc)
C12 (Dynamic Memory with Calloc)
C13 (Describe the difference between malloc
and calloc
)
malloc
and calloc
both return a pointer to allocated memory for some purpose. The major
difference is that calloc
, by default, will initialize all elements in that allocated memory
to the value 0
whereas malloc
does not modify anything in that block of memory before
returning a pointer.
You can achieve a poor man's calloc
by using malloc
with memset
to initialize the
newly returned block of memory to 0
yourself. This will almost certainly be slower than most
calloc
usages as calloc
"knows" the kernel will return something known as a virtual zero page and
thus will be receiving (on most systems) a chunk of memory that has all its values initialized
to 0
by default. Thus calloc
can "know" when it needs to allocate those 0
's itself or if
it can skip the process and thereby be faster than malloc
+ memset
.
C14 (Explain pointers and pointers to pointers using code)
Write code for pointer and use that code to explain pointers in your own words
#include <stdio.h>
int main() {
char *str = "Hello";
while (*str != *"\0") {
printf("Pointer Location: %p, Remaining Letters: %s\n", str, str);
str++;
}
}
If you run the code above it will output something similar to the following:
Pointer Location: 0x402008, Remaining Letters: Hello
Pointer Location: 0x402009, Remaining Letters: ello
Pointer Location: 0x40200a, Remaining Letters: llo
Pointer Location: 0x40200b, Remaining Letters: lo
Pointer Location: 0x40200c, Remaining Letters: o
As you can see we're progressively incrementing the pointer location by one and outputting the
remaining letters from the current pointer location to the end of the string until we hit the
NULL
character.
The line:
char *str = "Hello";
is really a pointer to some location that starts with the letter H
and ends with a NULL
character, \0
.
By incrementing the pointer by one, we're moving the location it's pointing at by one. After
the first iteration through the while loop the *str
pointer points at the letter e
. After
the second iteration, *str
points at l
, after the third iteration, l
, and so on.
In reality a pointer is something that points to a place in memory. It doesn't contain a
value itself, it merely points to where a value is stored. Hence why the output has the weird
hex codes for Pointer Location
like 0x402008
. That hex code is a location in memory known
as a memory address that *str
, the pointer, points to.
Write a code for pointer to pointer and use that code to explain pointer to pointer in your own words
A pointer to pointer can be seen in the following code:
#include <stdio.h>
int main() {
int some_integer = 106;
int *pointer = &some_integer;
int **pointer_to_pointer = &pointer;
printf("Value: %d\nPointer: %p\nPointer to Pointer: %p\n", some_integer, pointer, pointer_to_pointer);
}
When we run the above it will output (pointer values may vary):
Value: 106
Pointer: 0x7ffc87ea34d4
Pointer to Pointer: 0x7ffc87ea34c8
Notice that the pointer
variable has a different address it points to than
pointer_to_pointer
.
The pointer
variable points to the memory location of some_integer
and the
pointer_to_pointer
variable points to the memory location of where the pointer
variable is
stored.
With the above in mind, we can then think of a pointer to a pointer as a double indirection. See the following diagram for what this effectively "looks" like:
Pointer to Pointer ----> Pointer ----> Value
Thus, if we only had the variable pointer_to_pointer
we could access the value all the
pointers ultimately point to with a multiple dereference like so:
#include <stdio.h>
int main() {
int some_integer = 106;
int *pointer = &some_integer;
int **pointer_to_pointer = &pointer;
printf("Deref'd Value from Pointer to Pointer: %d\n", **pointer_to_pointer);
}
Which will output:
Deref'd Value from Pointer to Pointer: 106