diff --git a/.vscode/ipch/142a66616266d2dc/mmap_address.bin b/.vscode/ipch/142a66616266d2dc/mmap_address.bin new file mode 100644 index 000000000..f8ee5b757 Binary files /dev/null and b/.vscode/ipch/142a66616266d2dc/mmap_address.bin differ diff --git a/.vscode/ipch/142a66616266d2dc/pointers_tests.ipch b/.vscode/ipch/142a66616266d2dc/pointers_tests.ipch new file mode 100644 index 000000000..2ad4db059 Binary files /dev/null and b/.vscode/ipch/142a66616266d2dc/pointers_tests.ipch differ diff --git a/.vscode/ipch/1496ff0e712cfe1e/mmap_address.bin b/.vscode/ipch/1496ff0e712cfe1e/mmap_address.bin new file mode 100644 index 000000000..f8ee5b757 Binary files /dev/null and b/.vscode/ipch/1496ff0e712cfe1e/mmap_address.bin differ diff --git a/.vscode/ipch/1496ff0e712cfe1e/strings.ipch b/.vscode/ipch/1496ff0e712cfe1e/strings.ipch new file mode 100644 index 000000000..4a7c80aa0 Binary files /dev/null and b/.vscode/ipch/1496ff0e712cfe1e/strings.ipch differ diff --git a/.vscode/ipch/2e4a236428e5749a/hello.ipch b/.vscode/ipch/2e4a236428e5749a/hello.ipch new file mode 100644 index 000000000..1bb3a8724 Binary files /dev/null and b/.vscode/ipch/2e4a236428e5749a/hello.ipch differ diff --git a/.vscode/ipch/2e4a236428e5749a/mmap_address.bin b/.vscode/ipch/2e4a236428e5749a/mmap_address.bin new file mode 100644 index 000000000..f8ee5b757 Binary files /dev/null and b/.vscode/ipch/2e4a236428e5749a/mmap_address.bin differ diff --git a/.vscode/ipch/b2b94795190d8c52/mmap_address.bin b/.vscode/ipch/b2b94795190d8c52/mmap_address.bin new file mode 100644 index 000000000..d11730d5c Binary files /dev/null and b/.vscode/ipch/b2b94795190d8c52/mmap_address.bin differ diff --git a/.vscode/ipch/b2b94795190d8c52/structs.ipch b/.vscode/ipch/b2b94795190d8c52/structs.ipch new file mode 100644 index 000000000..441ff0f53 Binary files /dev/null and b/.vscode/ipch/b2b94795190d8c52/structs.ipch differ diff --git a/.vscode/ipch/bdff6ed63248d8cc/fizzbuzz_tests.ipch b/.vscode/ipch/bdff6ed63248d8cc/fizzbuzz_tests.ipch new file mode 100644 index 000000000..32f53d6da Binary files /dev/null and b/.vscode/ipch/bdff6ed63248d8cc/fizzbuzz_tests.ipch differ diff --git a/.vscode/ipch/bdff6ed63248d8cc/mmap_address.bin b/.vscode/ipch/bdff6ed63248d8cc/mmap_address.bin new file mode 100644 index 000000000..f8ee5b757 Binary files /dev/null and b/.vscode/ipch/bdff6ed63248d8cc/mmap_address.bin differ diff --git a/.vscode/ipch/c5cd8f815114948c/fizzbuzz.ipch b/.vscode/ipch/c5cd8f815114948c/fizzbuzz.ipch new file mode 100644 index 000000000..8712fc6c2 Binary files /dev/null and b/.vscode/ipch/c5cd8f815114948c/fizzbuzz.ipch differ diff --git a/.vscode/ipch/c5cd8f815114948c/mmap_address.bin b/.vscode/ipch/c5cd8f815114948c/mmap_address.bin new file mode 100644 index 000000000..f8ee5b757 Binary files /dev/null and b/.vscode/ipch/c5cd8f815114948c/mmap_address.bin differ diff --git a/.vscode/ipch/e0cbb29bafa66c08/malloc.ipch b/.vscode/ipch/e0cbb29bafa66c08/malloc.ipch new file mode 100644 index 000000000..70fc6538d Binary files /dev/null and b/.vscode/ipch/e0cbb29bafa66c08/malloc.ipch differ diff --git a/.vscode/ipch/e0cbb29bafa66c08/mmap_address.bin b/.vscode/ipch/e0cbb29bafa66c08/mmap_address.bin new file mode 100644 index 000000000..f8ee5b757 Binary files /dev/null and b/.vscode/ipch/e0cbb29bafa66c08/mmap_address.bin differ diff --git a/.vscode/ipch/f1ec230148bc3396/mmap_address.bin b/.vscode/ipch/f1ec230148bc3396/mmap_address.bin new file mode 100644 index 000000000..f8ee5b757 Binary files /dev/null and b/.vscode/ipch/f1ec230148bc3396/mmap_address.bin differ diff --git a/.vscode/ipch/f1ec230148bc3396/strings_tests.ipch b/.vscode/ipch/f1ec230148bc3396/strings_tests.ipch new file mode 100644 index 000000000..f2eeb1bba Binary files /dev/null and b/.vscode/ipch/f1ec230148bc3396/strings_tests.ipch differ diff --git a/.vscode/ipch/fad35734fec88b80/mmap_address.bin b/.vscode/ipch/fad35734fec88b80/mmap_address.bin new file mode 100644 index 000000000..f8ee5b757 Binary files /dev/null and b/.vscode/ipch/fad35734fec88b80/mmap_address.bin differ diff --git a/.vscode/ipch/fad35734fec88b80/pointers.ipch b/.vscode/ipch/fad35734fec88b80/pointers.ipch new file mode 100644 index 000000000..454e9b29c Binary files /dev/null and b/.vscode/ipch/fad35734fec88b80/pointers.ipch differ diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..691a8f68f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.errorSquiggles": "Disabled" +} \ No newline at end of file diff --git a/fizzbuzz/fizzbuzz.c b/fizzbuzz/fizzbuzz.c index 6cbb52518..249a3da30 100644 --- a/fizzbuzz/fizzbuzz.c +++ b/fizzbuzz/fizzbuzz.c @@ -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 diff --git a/hello/hello.c b/hello/hello.c new file mode 100644 index 000000000..4ac1a5af2 --- /dev/null +++ b/hello/hello.c @@ -0,0 +1,88 @@ + + +// #include + +// 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 + +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; +} \ No newline at end of file diff --git a/malloc/malloc.c b/malloc/malloc.c index 82cc0aea6..abb348b23 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1,7 +1,28 @@ #include #include #include -#include "lib.h" +#include +// #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 @@ -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). @@ -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= 0; i--) { + rv[newIndex] = s[i]; + newIndex++; + } + rv[reverseReverse] = '\0'; + return rv; +} + +// char *reverse_string(char rv[], char s[]) +// { +// int count = 0; +// int n = string_length(s); + +// for (int i = n - 1; i >= 0; i--) { +// rv[count++] = s[i]; +// } + +// rv[n] = '\0'; +// return rv; +// } + +char *reverse_string(char *rv, char *s) +{ + int count = string_length(s); + int i = 0; + while (count >= 1) { + count--; + *(rv + i) = *(s + count); + i++; + } + *(rv + i) = '\0'; + printf("%s\n", rv); + return rv; } +// char *reverse_string(char *rv, char *s) +// { +// char temp; +// int n = string_length(s); + +// for (int i = 0; i < n/2; i++) { +// temp = s[i]; +// s[i] = s[n-i-1]; +// s[n-i-1] = temp; +// } +// return temp; +// } + + #ifndef TESTING int main(void) { @@ -36,5 +94,4 @@ int main(void) return 0; } -#endif - +#endif \ No newline at end of file diff --git a/structs/structs.c b/structs/structs.c index afad3cf93..e3b9933fa 100644 --- a/structs/structs.c +++ b/structs/structs.c @@ -2,13 +2,19 @@ #include #include "lib.h" + + + /* Define the Person struct by specifying the fields that make up the Person type. Don't forget to specify the type of each field. A Person should have the fields `name`, `age`, `height`, and `weight`. */ typedef struct Person { - + char *name; + int age; + int height; + int weight; } Person; /* @@ -21,6 +27,14 @@ typedef struct Person { */ Person *createPerson(char *name, int age, int height, int weight) { + struct Person *who = malloc(sizeof(struct Person)); + + who->name = string_dup(name); + who->age = age; + who->height = height; + who->weight = weight; + + return who; } @@ -30,7 +44,8 @@ Person *createPerson(char *name, int age, int height, int weight) */ void destroyPerson(Person *who) { - + free(who->name); + free(who); } #ifndef TESTING