-
Notifications
You must be signed in to change notification settings - Fork 20
Jack Rosen, Assignment 7 #2
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,14 @@ | ||
| Jack Rosen | ||
| 1. ++*p increments the value at point p. *p++ increments p and then returns the value. *++p increments p and then returns the value as well. | ||
| 2. It is only guaranteed for certain operators. The first level of operators goes from left to right, the second goes from right to left, and the rest go from left to right. | ||
|
Contributor
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 simple answer to this question is neither. C doesn’t always evaluate left-to-right or right-to-left. Generally, function calls are evaluated first, followed by complex expressions and then simple expressions. |
||
| 3.The advantages of using pointers is that they are very powerful and help a person work with arrays. It is also a way to access a variable without having to call for it. | ||
|
Contributor
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. Pointers are more efficient in handling arrays and data tables and can be used to return multiple values from a function via function arguments. They also allow references to functions and C to support dynamic memory management. Lastly, Pointers reduce length and complexity of programs! |
||
| 4.1 char * | ||
|
Contributor
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. 💯
Contributor
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. Please note that while you got this right, you didn't explain your answers! |
||
| 4.2 invalid | ||
|
Contributor
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 | ||
|
Contributor
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 10 | ||
|
Contributor
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. Yup! but again, please explain answers as the directions said. |
||
| 4.5 0 | ||
|
Contributor
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 12 | ||
|
Contributor
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 again, explain your answers! |
||
| 4.7 2 | ||
|
Contributor
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 | ||
|
Contributor
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! You're right that it's char, but 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 0 | ||
|
Contributor
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.10 6 | ||
|
Contributor
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. sideof(str) is always 8 because all pointers are 8 bytes no matter what they're pointing to. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| void stringReversal(char *input) | ||
| { | ||
| char *ptrBegin = input, *ptrEnd = ptrBegin + strlen(input) - 1; | ||
| for (; ptrBegin <= ptrEnd; --ptrEnd) | ||
| { | ||
| if (*ptrEnd == '\n') | ||
| { | ||
| continue; | ||
| } | ||
| printf("%c", *ptrEnd); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| int main() | ||
| { | ||
| printf("Write anything you want!\n"); | ||
| char input[1000] = {}; | ||
| int i; | ||
| fgets(input, sizeof(input), stdin); | ||
| for (i = 0; i < strlen(input); i++) | ||
| { | ||
| if (input[i] == '\n' || input[i] == '\0') | ||
| { | ||
| i--; | ||
| break; | ||
| } | ||
| } | ||
| stringReversal(input); | ||
| printf("\n"); | ||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
| void concat(char *ptr1, char *ptr2) | ||
| { | ||
| char *ptr3 = ptr1; | ||
| while (*ptr3 != '\0') | ||
| { | ||
| ptr3++; | ||
| } | ||
| while (*ptr2 != '\0') | ||
| { | ||
| *ptr3 = *ptr2; | ||
| ptr3++; | ||
| ptr2++; | ||
| } | ||
| ptr3++; | ||
| *ptr3 = '\0'; | ||
| } | ||
| int compare(char *ptr1, char *ptr2) | ||
| { | ||
| int flag=0; | ||
|
|
||
| while(*ptr1!='\0' && *ptr2!='\0'){ | ||
| if(*ptr1 != *ptr2){ | ||
| flag=1; | ||
| break; | ||
| } | ||
| ptr1++; | ||
| ptr2++; | ||
| } | ||
| if (flag==0 && *ptr1=='\0' && *ptr2=='\0') | ||
| { | ||
| return 0; | ||
| } | ||
| else if (*ptr1 > *ptr2) | ||
| { | ||
| return 1; | ||
| } | ||
| else if (*ptr2 > *ptr1) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| } | ||
| int main() | ||
| { | ||
| char ptr1[12] = "Hello ", ptr2[] = "World"; | ||
| concat(ptr1, ptr2); | ||
| printf("%d\n", compare(ptr1,ptr2)); | ||
| printf("%s\n", ptr1); | ||
| return 0; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.
Hmm, not quite. 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).