Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .vscode/ipch/142a66616266d2dc/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/142a66616266d2dc/pointers_tests.ipch
Binary file not shown.
Binary file added .vscode/ipch/1496ff0e712cfe1e/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/1496ff0e712cfe1e/strings.ipch
Binary file not shown.
Binary file added .vscode/ipch/2e4a236428e5749a/hello.ipch
Binary file not shown.
Binary file added .vscode/ipch/2e4a236428e5749a/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/b2b94795190d8c52/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/b2b94795190d8c52/structs.ipch
Binary file not shown.
Binary file added .vscode/ipch/bdff6ed63248d8cc/fizzbuzz_tests.ipch
Binary file not shown.
Binary file added .vscode/ipch/bdff6ed63248d8cc/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/c5cd8f815114948c/fizzbuzz.ipch
Binary file not shown.
Binary file added .vscode/ipch/c5cd8f815114948c/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/e0cbb29bafa66c08/malloc.ipch
Binary file not shown.
Binary file added .vscode/ipch/e0cbb29bafa66c08/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/f1ec230148bc3396/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/f1ec230148bc3396/strings_tests.ipch
Binary file not shown.
Binary file added .vscode/ipch/fad35734fec88b80/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/fad35734fec88b80/pointers.ipch
Binary file not shown.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "Disabled"
}
19 changes: 18 additions & 1 deletion fizzbuzz/fizzbuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,26 @@
every time that nothing gets printed and return the counter.
Don't forget to include newlines '\n' in your printf statements!
*/

int fizzbuzz(int n)
{

int i;
int counter = 0;
for (i = 0; i <= n; i++) {

if ((i % 3 == 0) && (i % 5 == 0)) {
printf("FizzBuzz\n");
}
else if (i % 3 == 0) {
printf("Fizz\n");
}
else if (i % 5 == 0) {
printf("Buzz\n");
} else {
counter++;
}
}
return counter;
}

#ifndef TESTING
Expand Down
88 changes: 88 additions & 0 deletions hello/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@


// #include <stdio.h>

// int main(void) {

// printf("Hello, world!\n");

// int x = 12;

// //prints 12
// printf("x is %d\n", x);


// // initialize a pointer p with *
// int *p;

// p = &x;

// // printing the address that p is storing
// printf("p is %p\n", p);
// // printing the value that p is storing
// printf("p is %d\n", *p);

// *p = 99;

// printf("%d\n", x);

// // declaring a pointer
// char *c = "Goodbye";


// // print the actual memory address
// printf("Address of the c pointer: %p\n", c);

// // dereferencing the pointer and printing the value
// printf("Value stored at c pointer: %c\n", *c);

// for (int i = 0; i <7; i++) {
// printf("Address: %p | Value: %c\n", c+i, *(c+i));
// }




// return 0;
// }

#include <stdio.h>

int main(void) {

int a = 99; //int

int* p = &a; // pointer to an int, or an int-pointer

// printf("%d\n", a * 4); // print a * 4


printf("%p\n", p); // printing the pointer to int 'a', memory address


int** q = &p; // int** (pointer to a pointer)

printf("%p\n", q); // printing the pointer to the pointer to int `a`
printf("%d\n", *p); // printing the value of int 'a'
printf("%d\n", q);

char* name = "Austen";

printf("%p\n", &name); // pritning the pointer to the char[] 'name'

printf("%s\n", name); // printing the value of the char[] 'name'
printf("%c\n", *name); // printing the value of the char[] 'name'

// declaring a pointer
char *c = "Goodbye";

// print the actual memory address
printf("Address of c pointer: %p\n", c);

printf("Address of c pointer: %s\n", c);

// dereferencing the pointer and printing the value
printf("Value stored at c pointer: %c\n", *c);

return 0;
}
97 changes: 94 additions & 3 deletions malloc/malloc.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "lib.h"
#include <string.h>
// #include "lib.h"

int string_length(char *s)
{
return strlen(s);
}


// int string_length(char *s)
// {
// int i = 0;

// // while (s[n] != '\0') {
// // n++;
// // }

// while (*(s+i) != '\0') {
// i++;
// }
// return i;
// }

/*
Duplicates the input string by dynamically allocating memory for
Expand All @@ -12,9 +33,36 @@
*/
char *string_dup(char *src)
{

int len = string_length(src);
char *newthing = malloc(len + 1);
char *start = newthing;

while(*src) {
// printf("%c\n", *src);
// printf("%p\n", src);
*newthing = *src;
src++;
newthing++;
}
*newthing = '\0';
return start;
}

//Model Solution
// char *string_dup(char *src)
// {
// int n = string_length(src);
// char *str = malloc(n + 1);

// for (int i = 0; i < n; i++) {
// *(str+i) = *(src+i);
// }

// *(str+n) = '\0';

// return str;
// }

/*
A generic version of string_copy, mem_copy receives a block of memory
of any type and copies its contents to the destination pointer (dest).
Expand All @@ -24,9 +72,16 @@ char *string_dup(char *src)
*/
void mem_copy(void *dest, const void *src, int n)
{

char *cast_dest = (char *) dest;
char *cast_src = (char *) src;
for (int i=0; i<n; i++) {
*(cast_dest + i) = *(cast_src + i);
}
}




/*
Given a pointer of `malloc`'d memory, this function will
attempt to resize the allocated memory to the new specified
Expand All @@ -38,9 +93,45 @@ void mem_copy(void *dest, const void *src, int n)

Do not use the `realloc` function from the standard libary.
*/
// void *resize_memory(void *ptr, int old_size, int new_size)
// {
// char *dest_ptr = malloc(new_size);
// if (old_size < new_size) {
// mem_copy(dest_ptr, ptr, old_size);
// } else {
// mem_copy(dest_ptr, ptr, new_size);
// }
// return dest_ptr;
// }

// Model Solution
void *resize_memory(void *ptr, int old_size, int new_size)
{
if (new_size == 0) {
free(ptr);
return NULL;
}

else if (!ptr) {
return malloc(new_size);
}

else if (old_size == new_size) {
return ptr;
}

void *new_block = malloc(new_size);

if (new_size < old_size) {
mem_copy(new_block, ptr, new_size);
}

else {
mem_copy(new_block, ptr, old_size);
}

free(ptr);
return new_block;
}

#ifndef TESTING
Expand Down
36 changes: 28 additions & 8 deletions pointers/pointers.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,63 @@
character pointer y, copies the character contents of y over to x. Pointer
arithmetic is necessary here. Also, make sure x points to a null terminator
at its end to terminate it properly.

Example call:

char buffer[1024];

string_copy(buffer, "Hello!");
printf("%s", buffer); // Prints "Hello!"
*/
void string_copy(char *x, char *y)
{

while(*y) {
*x = *y;
x++;
y++;
}
}

/*
Searches the input string `str` for the first instance of the
character `c` (an unsigned char). This function returns a pointer
that points to the first instance of the character `c` in the
input string `str`.

Do not use the `strchr` function from the standard library.
*/
char *find_char(char *str, char c)
{

while (*str != '\0')
{
if (*str == c) {
printf("%s\n", str);
return str;
}
str++;
}
return NULL;
}

/*
Searches the input string `haystack` for the first instance of
the string `needle`. This function returns a pointer that points
to the first instance of the string `needle` in the input
string `haystack`.

Do not use the `strstr` function from the standard library.
*/
char *find_string(char *haystack, char *needle)
{
while(*haystack) {
char *begin = haystack;
char *inside = needle;

while (*haystack && *inside && *haystack == *inside) {
haystack++;
inside++;
}
if (!*inside) {
return begin;
}
haystack = begin + 1;
}
return NULL;
}

#ifndef TESTING
Expand All @@ -55,4 +75,4 @@ int main(void)

return 0;
}
#endif
#endif
Loading