-
Couldn't load subscription status.
- Fork 20
Add files via upload #19
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,30 @@ | ||
| Caleb Gershengorn | ||
|
|
||
| 1: | ||
| ++*p:advances the pointer one space in the array in memory, than checks what is in that spot. | ||
| *p++: checks the space in memory at that spot, then advances one space. | ||
| *++p" does the same as the first, because the placement of the '++' makes the difference, not the '*' | ||
|
|
||
| 2: | ||
| Left to right garuntees operator precedence. | ||
|
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: | ||
| Using pointers allows you to more easily look through lists and access things in memory than having to reference them directly. | ||
|
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 and reduce complexity |
||
|
|
||
| 4: | ||
| int *p=12 | ||
| *p=2 | ||
| 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. you didn't evaluate these questions! |
||
| 4.1--valid | ||
| 4.2--invalid(dash is not a thing) | ||
|
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--valid(just false) | ||
|
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--valid(dereference operator) | ||
|
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--valid(stores a value at a[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.
|
||
| 4.6--valid(dereference operator) | ||
|
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--valid(stores value at 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--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. 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--invalid(main is a special use 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 can have pointers to a function. |
||
| 4.10--valid | ||
|
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. |
||
|
|
||
|
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. They're all valid! |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| void swap(char*word,int sizeWord){ | ||
| int i; | ||
| int j; | ||
| char * wordPtr=word; | ||
| if (sizeWord%2==0) | ||
| { | ||
| for (i=0;i<sizeWord/2;i++) | ||
| { | ||
| char tmp = wordPtr[i]; | ||
| wordPtr[i] = wordPtr[sizeWord-i]; | ||
| wordPtr[sizeWord-i] = tmp; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| for (i=0;i<(sizeWord-1)/2;i++) | ||
| { | ||
| char tmp = wordPtr[i]; | ||
| wordPtr[i] = wordPtr[sizeWord-i]; | ||
| wordPtr[sizeWord-i]=tmp; | ||
| } | ||
| } | ||
| for (j=0;j<sizeWord;j++) | ||
| { | ||
| printf("%c\n",wordPtr[j]); | ||
| } | ||
| } | ||
|
|
||
| int main(){ | ||
| char* arr1; | ||
| printf("Enter the word you want to flip: \n"); | ||
| fgets(arr1,sizeof(arr1),stdin); | ||
| swap(arr1,sizeof(arr1)); | ||
| } | ||
|
|
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.
💯