- 
                Notifications
    You must be signed in to change notification settings 
- Fork 20
Julia Tan - Assignment 7 #17
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| Julia Tan | ||
|  | ||
| 1. The order of operations of prefix ++ and * is the same. It is read from right to left. The order of operations of postfix ++ is higher than both * and prefix ++. It is read from left to right. | ||
|  | ||
| ++*p would be read as ++(*p). (dereferences p then increments it) | ||
| *p++ would be read as *(p++). (increments p then dereferences it) | ||
| *++p would be read as *(p++) as well. (incremeents p then dereferences it) | ||
|  | ||
| 2. No it is not guaranteed, as sometimes expressions are read left to right, and other times from right to left, as seen on the example above. | ||
| 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. 💯 | ||
|  | ||
| 3. Pointers are useful for handling large amounts of data efficiently. They are also used to access variables in other functions without using global variables, as global variables can be confusing and difficult to debug. Other times, pointers are essential, such as in complex data structures. | ||
| 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. right! they also allow references to functions! | ||
|  | ||
| 4. | ||
| 4.1 char * (char * are strings) | ||
| 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. 💯 | ||
| 4.2 invalid (if "xyz" is an array, you cannot subtract letters in each element) | ||
| 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. This is actually valid! "xyz" is just an array of characters, so the "xyz"[1] is just accessing "y". Then you just subtract 'y' from it to get 0. This is because, if you can recall, a char is just a value mapped to a character! | ||
| 4.3 1 ('/0' is equal to 0. when tested if its equal to 0, it is true and therefore returns 1) | ||
| 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. 💯 | ||
| 4.4 10 (first value in array) | ||
| 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. 💯 | ||
| 4.5 10 (first value in array) | ||
| 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. 
 | ||
| 4.6 int * (*p is an int pointer) | ||
| 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. Close! Since a is a pointer to the first element of the array, the increment by 2 would move the pointer two spots over to 12. so 12 is the answer. | ||
| 4.7 int ** (this makes a pointer for pointer p) | ||
| 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. 💯 | ||
| 4.8 char * (dereference of the double pointer 'argv') | ||
| 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. 💯 | ||
| 4.9 int * (makes a pointer for the main function) | ||
| 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. 👍 you need to also include the parameters, but good job! | ||
| 4.10 5 (0 to 5, as \0 is also an element in strings) | ||
| 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. Like I said in class, this is just a definition question, so the answer is 8 because pointers always allocate 8 bytes. | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|  | ||
| int main() { | ||
| printf("Enter 2 names to sort:\n"); | ||
| char fn[2][100]; | ||
| char ln[2][50]; | ||
| char full[2][100]; | ||
| char temp[50]; | ||
| int b = 0; | ||
| char *ptr = fn[b]; | ||
| for (int a = 0; a < 2; a ++){ | ||
| ptr = fn[b]; | ||
| printf("Person %d:\n", (a+1)); | ||
| printf("First name: "); | ||
| scanf("%s", fn[a]); | ||
| printf("Last name: "); | ||
| scanf("%s", ln[a]); | ||
| while (*ptr != '\0'){ | ||
| ptr++; | ||
| } | ||
| *ptr = ' '; | ||
| *ptr ++; | ||
| *ptr = '\0'; | ||
| strcat(fn[a], ln[a]); | ||
| printf("Full name: %s\n\n", fn[a]); | ||
| b ++; | ||
| } | ||
| printf("Here are the names in alphabetical order:\n"); | ||
| int compare = strcmp(fn[0], fn[1]); | ||
| if (compare > 0) | ||
| printf("%s\n%s\n",fn[1], fn[0]); | ||
| else if (compare < 0) | ||
| printf("%s\n%s\n", fn[0], fn[1]); | ||
| return 0; | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Julia Tan | ||
| // reverse.c | ||
|  | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|  | ||
| void reverse(char* a, char* b, char *c, char *d){ | ||
| for (int i = 0; i < (strlen(c) - 1); i ++){ | ||
| *b = *a; | ||
| a --; // move back 1 position in input string | ||
| b ++; // move forward 1 position in reverse string | ||
| } | ||
| } | ||
|  | ||
| int main (){ | ||
| char word[100]; | ||
| char *word1 = word; // pointer to entire input string | ||
| char* ptr1 = word; // pointer to each letter in input string | ||
| printf("Enter a word: \n"); | ||
| fgets(word, sizeof(word), stdin); | ||
| char revWord[sizeof(word)]; // reverse string | ||
| char* ptr2 = revWord; // pointer to each letter in reverse string | ||
| char *word2 = revWord; // pointer to entire reverse string | ||
| while (*ptr1 != '\0'){ // finds end of string | ||
| ptr1 ++; | ||
| } | ||
| ptr1 -= 2; | ||
| reverse(ptr1, ptr2, word1, word2); | ||
| printf("The reverse string is:\n%s\n", word2); | ||
| return 0; | ||
| } | 
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.
Perfect answer! 💯