-
Couldn't load subscription status.
- Fork 20
Assignment 7, totally on time, Eli Kalish #21
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,21 @@ | ||
| 1. Explain the difference between ++*p, *p++ and *++p, if there is any. | ||
| ++*p increments the value that p is pointing to and stores that new value at p. *p++ | ||
| Stores the value of p, then moves the pointer. I don't know what the last one does. | ||
| 2. Is the left to right or right to left order guaranteed for operator precedence? | ||
| Not neccesarily, it depends what operators are being used. If a postfix is being used | ||
|
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! 💯 |
||
| then i's right to left, but otherwise it is right to left. | ||
| 3. What are the advantages of using pointers? | ||
| Although shockingly, mind-bogglingly annoying, pointers can be helpful and more efficient | ||
|
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. lawl |
||
| when used with arrays for the purposes of returning arrays from functions or just | ||
|
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 right! they also allow pointers to functions! |
||
| managing arrays in general. | ||
| 4. | ||
| 4.1 "abc" char name[] | ||
|
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! we're looking for the specific data type, so it's |
||
| 4.2 "xyz"[1] - ’y’ ? | ||
|
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 ’\0’ == 0 0 | ||
|
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. '\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 *a 10 | ||
|
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 &a[0] 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.
|
||
| 4.6 *p 12 | ||
|
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.7 &p 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.
|
||
| 4.8 *++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. 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 &main ? | ||
| 4.10 sizeof(str) 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. right! but 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,25 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| void sReverse(char a[]) | ||
| { | ||
| char *ptrStart = a; | ||
| char *ptrEnd = ptrStart + strlen(a) - 1; | ||
| int i; | ||
| for (i = 0; *ptrStart != *ptrEnd; i++) | ||
| { | ||
| printf("%c", *ptrEnd); | ||
| ptrEnd--; | ||
| } | ||
| printf("%c", a[0]); | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| printf("Enter a string. Through some shenanigans I don't understand, it should be reversed.\n"); | ||
| char word[100]; | ||
| fgets(word, sizeof(word), stdin); | ||
| sReverse(word); | ||
| 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.
The expression ++*p has two operators of same precedence, so compiler looks for associativity. Associativity of operators is right to left. Therefore the expression is treated as ++(*p). The expression *p++ is treated as *(p++) as the precedence of postfix ++ is higher than *. The expression *++p has two operators of same precedence, so compiler looks for associativity. Associativity of operators is right to left. Therefore the expression is treated as *(++p).