-
Couldn't load subscription status.
- Fork 20
Assignment 7 Bettina Benitez #16
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,24 @@ | ||
| Bettina Benitez | ||
|
|
||
| 1. ++*p reads right to left. Therefore, it increments point p. Same with *++p, the order of operation is right to left. But point p is incremented then the dereferenced. *p++ is read left to right therefore it is incremented then dereferenced. | ||
|
|
||
| 2. No the order of precedence is not guaranteed | ||
|
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. advantages: | ||
| - it points to a specific lovation in memory | ||
| - provides direct access to memory | ||
| - reduces storage and complexity | ||
|
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! and lastly, it allows references to functions! |
||
|
|
||
| 4 | ||
| - 4.1: *char | ||
|
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. 💯 but give explanations! |
||
| - 4.2: invalid | ||
|
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: invalid | ||
|
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 perfectly valid! '\0' is just a NULL terminator and by definition, NULL is equal to 0. So this would evaluate as true, which is 1 in C. |
||
| - 4.4: *int pointer to the 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. the answer is 10 because it's just pointing to the first element of the array! |
||
| - 4.5: *char reference to the 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 | ||
|
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. 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: reference to the 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.
|
||
| - 4.8: incrementing the char 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. Close, but not quite! It's actually``char*. This is because the * in ++argv dereferences the char* argv, leading to char **. The incrementing actually does nothing. In the same way that int x = 1; ++x would still be an int!` |
||
| - 4.9: reference to main | ||
|
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, so the answer is |
||
| - 4.10: part of the fgets fucntion to get the string. | ||
|
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,26 @@ | ||
| //Bettina Benitez | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| void getReverse (char *x) { | ||
| int i; | ||
| int length = strlen(x); //gets the length of the string | ||
| char *first = x; //points to the first character of the string | ||
| char *last = x + length - 1; //points to the last character (not \0) of the string | ||
| char temp; // temp is used as a place holder when switching letters | ||
| for (i = 0; i < length/2; i++) { //length is divided by 2 because it only needs to go half way to swap | ||
| temp = *first; | ||
| *first = *last; | ||
| *last = temp; | ||
| *first ++; | ||
| *last--; | ||
| } | ||
| printf("%s", x); | ||
| } | ||
|
|
||
| int main () { | ||
| printf("Enter a word: "); | ||
| char word [100]; | ||
| fgets(word, sizeof(word), stdin); | ||
| getReverse (word); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| //Bettina Benitez | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| void strCat (char *a, char *b) { | ||
| char *p = a; | ||
| while (*p != '\0') { | ||
| p++; | ||
| } | ||
| strcat(a, b); | ||
| *p--; | ||
| *p = ' '; | ||
| printf("%s", a); | ||
| } | ||
|
|
||
| void strCmp(char *a, char *b) { | ||
| if (strcmp(a, b) == 0) | ||
| printf("Words are the same\n"); | ||
| else | ||
| printf("Words are the same\n"); | ||
| } | ||
|
|
||
| int main () { | ||
| char aUserIn[100], bUserIn[100], strOpt[100]; | ||
| printf("Enter first word: "); | ||
| fgets(aUserIn, sizeof(aUserIn), stdin); | ||
| printf("Enter second word: "); | ||
| fgets(bUserIn, sizeof(bUserIn), stdin); | ||
| printf("Choose to concatenate or compare: "); | ||
| fgets(strOpt, sizeof(strOpt), stdin); | ||
| char con[100] = "concatenate\n"; | ||
| char com[100] = "compare\n"; | ||
| if (strcmp(strOpt, con) == 0) | ||
| strCat (aUserIn, bUserIn); | ||
| else if (strcmp(strOpt, com) == 0) | ||
| strCmp(aUserIn, bUserIn); | ||
| else | ||
| printf("Error: %s is not an option\n", strOpt); | ||
| 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.
💯