forked from bloominstituteoftechnology/Intro-to-C
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PR :: Andrew Jarrett #1
Open
ahrjarrett
wants to merge
17
commits into
master
Choose a base branch
from
andrew-jarrett
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7930710
initial commit
ahrjarrett 0b6015e
complete fizzbuzz
ahrjarrett 2c6a13e
complete strings
ahrjarrett 04e54b5
halfway finished with pointers
ahrjarrett 50217b8
halfway done with pointers
ahrjarrett 5b401e9
complete points: string_compare
ahrjarrett 67ed5e3
add notes: Pointers in C
ahrjarrett bf450de
complete pointers
ahrjarrett f5a60a6
add malloc file
ahrjarrett ad92a5f
string_dup working; still working on mem_copy in malloc
ahrjarrett fd31a4a
start working on quicksort
ahrjarrett b4dbf72
quicksort function passing! :tada:
ahrjarrett a99a2a8
complete typedef struct Person
ahrjarrett 4dc1daf
complete factory function createPerson in structs portion
ahrjarrett a8ac388
complete structs portion
ahrjarrett f2fcc3d
write createQueue factory and enqueue function
ahrjarrett afdd254
complete queue :tada:
ahrjarrett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,4 @@ If you see this error: | |
``` | ||
git config --global core.autocrlf false | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#+TITLE: Pointer Notes | ||
#+AUTHOR: Andrew Jarrett | ||
|
||
Date: <2018-10-28 Sun> | ||
|
||
* Memory Allocation | ||
|
||
** =malloc= | ||
|
||
- Takes a parameter that indicates how much memory the user wants | ||
- Finds a /contiguous/ spot in memory that satisfies the size criteria | ||
|
||
#+BEGIN_SRC c | ||
// create a pointer to a block of memory that | ||
// is big enough to hold 100 integers | ||
int *100_ints = malloc(100 * sizeof(int)); | ||
|
||
// then we can fill the empty block of memory: | ||
for(int i = 0; i < 100; i++) { | ||
,*(100_ints + 1) = rand(); | ||
} | ||
#+END_SRC | ||
|
||
** Passing Points as Parameters | ||
|
||
We're used to passing parameters by value, where: | ||
|
||
- parameter is a copy of value of a specific variable | ||
- must return new value to retain changes outside scope of the function | ||
|
||
This is good in a lot of scenarios, but what if for example the value is very large (takes up a lot of memory)? | ||
|
||
In C we can pass pointers to functions. | ||
|
||
This is called *passing by reference* | ||
|
||
- parameter is a pointer | ||
- cheaper if we are working with really large arrays/strings/objects (objects == structs?) | ||
- *mutated thing retains those updates outside the scope of the function* | ||
|
||
#+BEGIN_SRC c | ||
// example - print out a long string | ||
void print_long_str(int *str, int len) { | ||
for(int i = 0; i < len; i++) { | ||
printf("%c", *(str + 1)); | ||
} | ||
} | ||
#+END_SRC | ||
|
||
*Note:* In the example above, shouldn't the function's first parameter be =char *str=? | ||
|
||
In summation: | ||
|
||
1. We can use /pointer arithmetic/ to iterate through all the values within a block of memory, starting with the first address | ||
2. /Passing pointers to functions/ allows us to manipulate large pieces of data more efficiently while maintaining updates after a function exits | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,10 @@ | |
address it's referring to) or the value at the address it's | ||
pointing at. | ||
*/ | ||
void swap(int* a, int* b) | ||
{ | ||
|
||
void swap(int* a, int* b) { | ||
int temp = *a; | ||
*a = *b; | ||
*b = temp; | ||
} | ||
|
||
/* | ||
|
@@ -19,22 +20,42 @@ void swap(int* a, int* b) | |
|
||
Do not use the `strchr` function from the standard library. | ||
*/ | ||
char *find_char(char *str, int c) | ||
{ | ||
|
||
// Old solution to find_char: | ||
/* char *result = NULL; */ | ||
/* while(*str != '\0') { */ | ||
/* if(*str == c) { */ | ||
/* result = &(*str); */ | ||
/* break; */ | ||
/* } */ | ||
/* str++; */ | ||
/* } */ | ||
/* return result; */ | ||
char *find_char(char *str, int c) { | ||
for (int i = 0; i < strlen(str); i++) { | ||
if(str[i] == c) { | ||
// we return a *pointer*, not str[i] | ||
return str + i; | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
/* | ||
Given an empty (NULL) character pointer x and a character pointer y, | ||
copies the character contents of y over to x. Again, pointer arithmetic | ||
is necessary here. Also, make sure x points to a null character at its | ||
end to terminate it properly. | ||
|
||
Do not just use the `strcpy` function from the standard library. | ||
*/ | ||
void string_copy(char *x, char *y) | ||
{ | ||
|
||
void string_copy(char *x, char *y) { | ||
while(*y != '\0') { | ||
*x = *y; | ||
x++; | ||
y++; | ||
} | ||
*x = '\0'; | ||
} | ||
|
||
/* | ||
|
@@ -51,9 +72,16 @@ void string_copy(char *x, char *y) | |
|
||
Do not just use the `strcmp` function from the standard library. | ||
*/ | ||
int string_compare(char *m, char *n) | ||
{ | ||
|
||
int string_compare(char *m, char *n) { | ||
while(*m == *n) { | ||
if(*m == '\0') { | ||
return 0; | ||
} | ||
m++; | ||
n++; | ||
} | ||
return *m - *n; | ||
} | ||
|
||
/* | ||
|
@@ -64,9 +92,25 @@ int string_compare(char *m, char *n) | |
|
||
Do not use the `strstr` function from the standard library. | ||
*/ | ||
char *find_string(char *haystack, char *needle) | ||
{ | ||
|
||
char *find_string(char *haystack, char *needle) { | ||
for(int i = 0; i < strlen(haystack); i++) { | ||
if(haystack[i] == needle[0]) { | ||
char *start = haystack + i; | ||
int n = 1; | ||
i++; | ||
|
||
while(haystack[i] == needle[n]) { | ||
i++; | ||
n++; | ||
if(needle[n] == '\0') { | ||
return start; | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
return NULL; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm able to break this by testing a string that starts at the beginning of world. hmmmmm.... :) |
||
} | ||
|
||
#ifndef TESTING | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,14 @@ | |
|
||
Do not just use the `strlen` function from the standard libary. | ||
*/ | ||
int string_length(char s[]) | ||
{ | ||
|
||
int string_length(char s[]) { | ||
int count = 0; | ||
while(*s !='\0') { | ||
count++; | ||
s++; | ||
} | ||
|
||
return count; | ||
} | ||
|
||
/* | ||
|
@@ -18,23 +23,29 @@ int string_length(char s[]) | |
enough space for the reversed string. Don't forget to terminate | ||
the reversed string with a null character. Return the rv array. | ||
*/ | ||
char *reverse_string(char rv[], char s[]) | ||
{ | ||
|
||
char *reverse_string(char rv[], char s[]) { | ||
int len = string_length(s); | ||
for(int i = 0; i < len; i++) { | ||
rv[i] = s[len-i-1]; | ||
} | ||
|
||
rv[len] = '\0'; | ||
return rv; | ||
} | ||
|
||
#ifndef TESTING | ||
int main(void) | ||
{ | ||
char quote1[] = "Don't forget to be awesome"; | ||
char quote2[] = "a man a plan a canal panama"; | ||
|
||
char rv[512]; | ||
char quote1[] = "Don't forget to be awesome"; | ||
char quote2[] = "a man a plan a canal panama"; | ||
|
||
printf("The string 'Don't forget to be awesome' has %d characters.\n", string_length(quote1)); | ||
printf("The string 'a man a plan a canal panama' reversed is: '%s'\n", reverse_string(rv, quote2)); | ||
char rv[512]; | ||
|
||
return 0; | ||
printf("The string 'Don't forget to be awesome' has %d characters.\n", string_length(quote1)); | ||
printf("The string 'a man a plan a canal panama' reversed is: '%s'\n", reverse_string(rv, quote2)); | ||
|
||
return 0; | ||
} | ||
#endif | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tested. works 👍 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of while loop. Good job.